一、MySQL的连接
1、使用之前记得要在工程的build path中添加 MySQL-connector-Java 的 jar 包
2、使用DriverManager 连接数据库;
3、使用Class.forName"(com.mysql.jdbc.Driver")加载驱动;
4、登录连接数据库(三种方法)
5、向数据库发送SQL语句,处理结果
6、关闭数据库连接
4、登录连接数据库(三种方法)
(1)String URL ="jdbc:mysql://localhost:3306/test?user=root&password=123"; Connection conn = DriverManager.getConnection(URL); |
(2)String URL ="jdbc:mysql://localhost:3306/test"; String user ="root"; String password ="123"; Connection conn = DriverManager.getConnection(URL, user, password); |
(3)String URL ="jdbc:mysql://localhost:3306/test"; Properties connectionProps =newProperties(); connectionProps.put("user",this.userName); connectionProps.put("password",this.password); Connection conn = DriverManager.getConnection(URL, connectionProps); |
5、向数据库发送SQL语句(三种创建语句:Statement 、PreparedStatement 、CallableStatement )
| Connection.createStatement() ——创建普通的语句,它通常不需要提供参数。 |
| Connection.prepareStatement(String stringSQL)——创建预编译语句,通常要提供一个带有占位符的字符串 SQL 语句。 |
| Connection.prepareCall()——创建存储过程。 |
Statement:
private static int addStudent2(student stu) throws Exception{
Connection con = jdbcTest.getcon();
String sql = "insert into t_student values ("+stu.getId()+"','"+stu.getStuName()+"','"+stu.getAge()+"',"+stu.getGradeName()+")";
Statement stm = con.createStatement();
int result=stm.executeUpdate(sql);
jdbcTest.close(stm, con);
return result;
}
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和execute。使用哪一个方法由 SQL 语句所产生的内容决定。
方法 | 执行语句 |
executeQuery | 用于产生单个结果集的语句,例如 SELECT 语句 |
executeUpdate | 执行 INSERT、UPDATE 或 DELETE 语句以及 SQLDDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE |
execute | 用于执行返回多个结果集、多个更新计数或二者组合的语句。 |
PreparedStatement(Statement的子类)
插入数据 | 修改数据 |
public boolean Insert(User user){ boolean flag=true; Connection conn=null; PreparedStatement ps=null; //创建PreparedStatement 对象 String sql= "insert into user (name,pwd) values(?,?)"; //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。 conn=JDBCTest.getConn(); try { ps=conn.prepareStatement(sql); ps.setString(1, user.getName()); //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。 ps.setString(2, user.getPwd()); int i=ps.executeUpdate(); if(i==0){ flag=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCTest.closeAll(null, ps, conn); } return flag;
}
|
public boolean Update(User user){ boolean flag=true; Connection conn=null; PreparedStatement ps=null; String sql="update user set pwd=? where name=?"; conn=JDBCTest.getConn(); try { ps=conn.prepareStatement(sql); ps.setString(1, user.getPwd()); ps.setString(2, user.getName()); int i= ps.executeUpdate(); if(i==0){ flag=false; }
} catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCTest.closeAll(null, ps, conn); } return flag; }
|
删除数据 | 查看数据 |
public boolean Delete(int id){ boolean flag=true; Connection conn=null; PreparedStatement ps=null; String sql="delete from user where id=?"; conn=JDBCTest .getConn(); try { ps=conn.prepareStatement(sql); ps.setInt(1, id); int i=ps.executeUpdate(); if(i==0){ flag=false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCTest.closel(ps, conn); }
return flag; }
|
public List<User> Select(){ List<User> list= new ArrayList<User>(); Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; String sql="select * from user"; conn=JDBCTest .getConn(); try { ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ User user= new User(); user.setName(rs.getString("name")); list.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCTest.close(rs, ps, conn); }
return list; }
|
继承了 Statement 接口中所有方法的 PreparedStatement 接口都有自己的executeQuery、executeUpdate 和 execute 方法。Statement 对象本身不包含 SQL语句,因而必须给 Statement.execute 方法提供 SQL 语句作为参数。PreparedStatement 对象并 不将SQL 语句作为参数提供给这些方法,因为它们已经包含预编译 SQL 语句。CallableStatement 对象继承这些方法的PreparedStatement 形式。