JDBCDemo2.java
package com.zhangbz.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; import com.zhangbz.util.JDBCUtils; public class JDBCDemo2 { @Test public void delete() { Connection conn = null; Statement stat = null; try { conn = JDBCUtils.getConn(); stat = conn.createStatement(); stat.executeUpdate("delete from user where name = 'zhaoliu'"); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(null, stat, conn); } } @Test public void find() { Connection conn = null; Statement stat = null; ResultSet rs = null; try { conn = JDBCUtils.getConn(); stat = conn.createStatement(); rs = stat.executeQuery("select* from user where name = 'zhaoliu'"); while(rs.next()) { String name = rs.getString("name"); String password = rs.getString("password"); System.out.println(name + ":" + password); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, stat, conn); } } @Test public void update() { Connection conn = null; Statement stat = null; try { conn = JDBCUtils.getConn(); stat = conn.createStatement(); stat.executeUpdate("update user set password=999 where name = 'zhaoliu'"); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.close(null, stat, conn); } } @Test public void add() { Connection conn = null; Statement stat = null; try { //1.注册数据库驱动 //2.获取连接 conn = JDBCUtils.getConn(); //3.获取传输器对象 stat = conn.createStatement(); //4.执行sql语句 int count = stat.executeUpdate("insert into user values(null, 'zhaoliu', '123456', 'zhaoli@sina.com', '1999-09-09')"); //5.处理结果 if (count > 0) { System.out.println("执行成功!影响的行数为" + count); } else { System.out.println("执行失败!!"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { //6.关闭资源 JDBCUtils.close(null, stat, conn); } } }
JDBCUtils.java
1 package com.zhangbz.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.Properties; 9 10 public class JDBCUtils { 11 public static Properties prop = null; 12 private JDBCUtils() { 13 } 14 //使用配置文件 15 static { 16 try { 17 prop = new Properties(); 18 prop.load(JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties")); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 throw new RuntimeException(e); 22 } 23 } 24 /** 25 * 获取连接 26 * @return 27 * @throws ClassNotFoundException 28 * @throws SQLException 29 */ 30 public static Connection getConn() throws ClassNotFoundException, SQLException { 31 //1.注册数据库驱动 32 Class.forName(prop.getProperty("driver")); 33 //2.获取连接 34 return DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password")); 35 } 36 /** 37 * 关闭连接 38 */ 39 public static void close(ResultSet rs, Statement stat, Connection conn) { 40 if (rs != null) { 41 try { 42 rs.close(); 43 } catch (SQLException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } finally { 47 rs = null; 48 } 49 } 50 if (stat != null) { 51 try { 52 stat.close(); 53 } catch (SQLException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } finally { 57 stat = null; 58 } 59 } 60 if (conn != null) { 61 try { 62 conn.close(); 63 } catch (SQLException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } finally { 67 conn = null; 68 } 69 } 70 } 71 }
config.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql:///Day10 user=root password=root