Statement执行更新操作
Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。Statement对象,用于执行不带参数的简单SQL语句。
通过JDBC向指定的数据表中插入一条记录,需要注意下面的几点:
* 1.Statement:用于执行SQL语句的对象 * 1).通过COnnection的createStatement()方法来获取 * 2).通过excuteUpdate(sql)可以执行SQL语句 * 3).传入的SQL可以是insert,update或者delete,但是不能是select * 2.Connection、Statement都是应用程序和数据库服务器的连接 资源,使用后一定要关闭 * 需要在finally中关闭Connection和Statement对象 * 异常可以不处理,但是连接一定要关闭 * 3.关闭的顺序:先关闭后获取的,即先关闭Statement,后关闭Connection
具体的代码实现:
1 public void testStatement() throws Exception{ 2 //1.获取数据库连接 3 // Connection conn=getConnection(); 4 Connection conn=null; 5 //4.执行插入 6 //1).获取操作SQL语句的Statement对象:调用Connection的createStatement()方法来获取 7 //注意Statement这里是java.sql包中的,而不是java.mysql.jdbc中的 8 // Statement statement=conn.createStatement(); 9 Statement statement=null; 10 try { 11 //3.准备插入的SQL语句 12 conn=getConnection(); 13 String sql=null; 14 //sql的插入操作 15 // sql="insert into customers(NAME,email,birth) values('xyz','xyz@atguigu.com','1988-7-1')"; 16 //删除操作 17 // sql="delete from customers where id =1"; 18 //修改操作 19 sql="update customers set name='Tom' where id =2"; 20 statement = conn.createStatement(); 21 //2).调用Statement对象的excuteUpdate(sql),执行SQL语句进行插入 22 statement.execute(sql); 23 //5.关闭Statement对象 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } finally { 27 try { 28 if (statement != null) { 29 statement.close(); 30 } 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } finally { 34 35 // 2.关闭连接 36 if (conn != null) { 37 38 conn.close(); 39 } 40 } 41 } 42 }
【提示】:代码中的getConnction方法是在笔记一中定义的,可以看到我们可以对数据库中的记录进行插入(insert),更新(update),删除(delete)操作,使用Connection对象的createStatement( )方法创建一个statement对象,并且调用Statement对象的excuteUpdate(sql),执行SQL语句进行插入;
我们的getConnection方法和关闭statement以及conn的操作稍显复杂,我们可以定义一个工具类,里面包含一些通用的方法,实现我们的插入、删除、更新数据的操作
具体代码:
1 public class JDBCTools { 2 // 关闭conn和statement的操作 3 public static void release(Statement statement, Connection conn) { 4 if (statement != null) { 5 try { 6 statement.close(); 7 8 } catch (Exception e2) { 9 // TODO: handle exception 10 } 11 } 12 if (conn != null) { 13 try { 14 conn.close(); 15 } catch (SQLException e) { 16 e.printStackTrace(); 17 } 18 } 19 } 20 21 /** 22 * 1。获取连接的方法 通过读取配置文件从数据库服务器获取一个连接 23 * 24 * @author Administrator 25 * 26 */ 27 public static Connection getConnection() throws Exception { 28 String driverClass = null; 29 String jdbcUrl = null; 30 String user = null; 31 String password = null; 32 // 读取类路径下的jdbc.properties文件 33 InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream( 34 "jdbc.properties"); 35 Properties properties = new Properties(); 36 properties.load(in); 37 driverClass = properties.getProperty("driver"); 38 jdbcUrl = properties.getProperty("jdbcUrl"); 39 user = properties.getProperty("user"); 40 password = properties.getProperty("password"); 41 // 通过反射创建Driver对象 42 Driver driver = (Driver) Class.forName(driverClass).newInstance(); 43 Properties info = new Properties(); 44 info.put("user", user); 45 info.put("password", password); 46 Connection connection = driver.connect(jdbcUrl, info); 47 return connection; 48 } 49 }
我们更新数据的操作可以写成这样:这里update就是这个通用的方法;
1 public void update(String sql){ 2 Connection conn=null; 3 Statement statement=null; 4 try { 5 //用到了我们写的一个工具类JDBCTools 6 conn=JDBCTools.getConnection(); 7 statement=conn.createStatement(); 8 statement.execute(sql); 9 } catch (Exception e) { 10 // TODO: handle exception 11 }finally{ 12 JDBCTools.release(statement, conn); 13 } 14 }
传入不同的sql,执行相应的操作;
通过ResultSet执行查询操作
ResultSet:
/** * ResultSet:结果集,封装了使用JDBC进行查询的结果 * 1.调用Statement对象的excuteQuery(sql)方法可以得到结果集 * 2.ResultSet返回的实际上就是一张数据表,有一个指针 * 指向数据表的第一样的前面,可以调用next()方法检测下一行是否有效,若有效则返回true * ,并且指针下移,相当于迭代器对象的hasNext()和next()的结合体 * 3.当指针对位到确定的一行时,可以通过调用getXxx(index)或者getXxx(columnName) * 获取每一列的值,例如:getInt(1),getString("name") * 4.ResultSet当然也需要进行关闭 */
ResultSet的返回结果:
具体的代码实现:
1 @Test 2 public void testResultSet(){ 3 //获取id=2的customers数据表的记录,并打印 4 //面向接口的编程 5 Connection conn=null; 6 Statement statement=null; 7 ResultSet rs=null; 8 try { 9 //1.获取Connection 10 conn=JDBCTools.getConnection(); 11 System.out.println(conn); 12 //2.获取Statement 13 statement=conn.createStatement(); 14 System.out.println(statement); 15 //3.准备SQL 16 String sql="select id,name,email,birth from customers"; 17 //4.执行查询,得到ResultSet 18 rs=statement.executeQuery(sql); 19 System.out.println(rs); 20 //5.处理ResultSet 21 while(rs.next()){ 22 int id=rs.getInt(1); 23 String name=rs.getString("name"); 24 String email=rs.getString(3); 25 Date birth=rs.getDate(4); 26 System.out.println(id); 27 System.out.println(name); 28 System.out.println(email); 29 System.out.println(birth); 30 System.out.println("--------------"); 31 } 32 //6.关闭数据库资源 33 34 } catch (Exception e) { 35 e.printStackTrace(); 36 }finally{ 37 JDBCTools.release(rs, statement, conn); 38 } 39 }
到目前为止的完整代码:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
JDBCTools:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)