java和mysql 调用代码重点记录
使用开发工具:idea
使用平台:ubuntu 14.10
java部分
// JDBC 驱动名及数据库 URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/student_info";
// 连接mysql
Connection conn=null;
try{
Class.forName(JDBC_DRIVER);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
try{
conn= DriverManager.getConnection(DB_URL,DBUSER,DBPASS);
}catch(SQLException e){
e.printStackTrace();
}//使用select语言,并防注入
public boolean verify(String user,String password)
{
boolean result=false;
String sql = "select * from info where user=? and password=?";
Connection con = new SQL_Main().getConn();
try {
ps = con.prepareStatement(sql);
ps.setString(1, user);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {//验证成功
result=true;
System.out.println(result);
System.out.println("用户: " + user);
System.out.println("密码: " + password);
}
else {
System.out.println("password error or null");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}mysql部分:
使用到的命令:
SHOW DATABASES; //显示数据库
USE mysql; //使用mysql表
SELECT * FROM mysql; //列出mysql所有字段
SELECT id,user FROM mysql; //列出id,和user
SELECT * FROM mysql where id=1; //列出id=1一行
DESC mysql; //列出表结构
DELETE FROM mysql WHERE id=1; //删除id=1的一行
INSERT INTO mysql (id,passwd) VALUES (1,123456); //插入一条数据
ALTER TABLE `mysql` ADD unique(`id`);
//插入数据库标准
CREATE TABLE `info` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL DEFAULT ‘‘ COMMENT ‘名称‘,
`url` varchar(255) NOT NULL DEFAULT ‘‘,
`sort` int(11) NOT NULL DEFAULT ‘0‘ COMMENT ‘排名‘,
`country` char(10) NOT NULL DEFAULT ‘‘ COMMENT ‘国家‘, PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
本文出自 “linux学习笔记” 博客,谢绝转载!
原文:http://rockycai.blog.51cto.com/8871643/1872832