皮皮java笔记——Statement、ResultSet

来源:互联网 发布:手机能安装windows xp 编辑:程序博客网 时间:2024/06/04 03:59

一、Statement

/** * 通过 JDBC 向指定的数据表中插入一条记录.  *  * 1. Statement: 用于执行 SQL 语句的对象 * 1). 通过 Connection 的 createStatement() 方法来获取 * 2). 通过 executeUpdate(sql) 可以执行 SQL 语句. * 3). 传入的 SQL 可以是 INSRET, UPDATE 或 DELETE. 但不能是 SELECT *  * 2. Connection、Statement 都是应用程序和数据库服务器的连接资源. 使用后一定要关闭. * 需要在 finally 中关闭 Connection 和 Statement 对象.  *  * 3. 关闭的顺序是: 先关闭后获取的. 即先关闭 Statement 后关闭 Connection */@Testpublic void testStatement() throws Exception{//1. 获取数据库连接Connection conn = null;Statement statement = null;try {conn = getConnection2();//3. 准备插入的 SQL 语句String sql = null;//sql = "INSERT INTO customers (NAME, EMAIL, BIRTH) " +//"VALUES('XYZ', 'xyz@qq.com', '1990-12-12')";//sql = "DELETE FROM customers WHERE id = 1";sql = "UPDATE customers SET name = 'TOM' " +"WHERE id = 1";System.out.println(sql);//4. 执行插入. //1). 获取操作 SQL 语句的 Statement 对象: //调用 Connection 的 createStatement() 方法来获取statement = conn.createStatement();//2). 调用 Statement 对象的 executeUpdate(sql) 执行 SQL 语句进行插入statement.executeUpdate(sql);} catch (Exception e) {e.printStackTrace();} finally{try {//5. 关闭 Statement 对象.if(statement != null)statement.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{//2. 关闭连接if(conn != null)conn.close();}}}

二、ResultSet

/** * ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果.  * 1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集. * 2. ResultSet 返回的实际上就是一张数据表. 有一个指针指向数据表的第一行的前面. * 可以调用 next() 方法检测下一行是否有效. 若有效该方法返回 true, 且指针下移. 相当于 * Iterator 对象的 hasNext() 和 next() 方法的结合体 * 3. 当指针对应到一行时, 可以通过调用 getXxx(index) 或 getXxx(columnName) * 获取每一列的值. 例如: getInt(1), getString("name") * 4. ResultSet 当然也需要进行关闭.  */@Testpublic void testResultSet(){Connection conn = null;Statement statement = null;ResultSet rs = null;try {//1. 获取 Connectionconn = JDBCTools.getConnection();System.out.println(conn);//2. 获取 Statementstatement = conn.createStatement();System.out.println(statement);//3. 准备 SQLString sql = "SELECT id, name, email, birth " +"FROM customers";//4. 执行查询, 得到 ResultSetrs = statement.executeQuery(sql);System.out.println(rs);//5. 处理 ResultSetwhile(rs.next()){             //获取多个纪录int id = rs.getInt(1);String name = rs.getString("name");String email = rs.getString(3);Date birth = rs.getDate(4);System.out.println(id);System.out.println(name);System.out.println(email);System.out.println(birth);}} catch (Exception e) {e.printStackTrace();} finally{//6. 关闭数据库资源. JDBCTools.release(rs, statement, conn);}}

上面将获取Connection,关闭Connection,Statement,ResultSet的方法封装到了一个类里面

JDBCTools.java

public class JDBCTools {public static void release(ResultSet rs, Statement statement, Connection conn) {if(rs != null){try {rs.close();} catch (SQLException e) {e.printStackTrace();}}if (statement != null) {try {statement.close();} catch (Exception e2) {e2.printStackTrace();}}if (conn != null) {try {conn.close();} catch (Exception e2) {e2.printStackTrace();}}}/** * 关闭 Statement 和 Connection * @param statement * @param conn */public static void release(Statement statement, Connection conn) {if (statement != null) {try {statement.close();} catch (Exception e2) {e2.printStackTrace();}}if (conn != null) {try {conn.close();} catch (Exception e2) {e2.printStackTrace();}}}/** * 1. 获取连接的方法. 通过读取配置文件从数据库服务器获取一个连接. *  * @return * @throws Exception */public static Connection getConnection() throws Exception {// 1. 准备连接数据库的 4 个字符串.// 1). 创建 Properties 对象Properties properties = new Properties();// 2). 获取 jdbc.properties 对应的输入流InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");// 3). 加载 2) 对应的输入流properties.load(in);// 4). 具体决定 user, password 等4 个字符串.String user = properties.getProperty("user");String password = properties.getProperty("password");String jdbcUrl = properties.getProperty("jdbcUrl");String driver = properties.getProperty("driver");// 2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)Class.forName(driver);// 3. 通过 DriverManager 的 getConnection() 方法获取数据库连接.return DriverManager.getConnection(jdbcUrl, user, password);}}


0 0
原创粉丝点击