一、通过上篇文章,我们已经可以使用JDBC对数据库中的表进行增删改查啦(JDBC的基本使用:https://www.cnblogs.com/Infancy/p/12499806.html),我们对上篇文章的代码从头到尾看一下会发现,增删改查每个方法中有些一样的代码,如:加载驱动、获取连接等,为了使代码更简洁高效,我们将公用代码提取出来,封装到一个工具类中。
二、封装此工具类分为三步:
将驱动类全路径、数据库连接地址、数据库用户名、数据库密码写到一个配置文件中,properties或xml都行,这里我们以properties为例。写到配置文件里面的好处是后期我们修改数据库连接相关信息的时候直接修改这个配置文本文件即可。
新建一个JDBCUtils类,在静态代码块中将数据库连接信息加载
编写静态的加载驱动、获取连接、释放资源方法
三、代码示例(自己写的,亲测可以跑起来)
1.db.properties配置文件

1driverClassName=com.mysql.jdbc.Driver 2dbUrl=jdbc:mysql://localhost:3306/web_test3 3username=root 4password=root
View Code

2.JDBCUtils代码

1package com.zhurouwangzi.utils; 2 3import java.io.FileInputStream; 4import java.sql.Connection; 5import java.sql.DriverManager; 6import java.sql.ResultSet; 7import java.sql.Statement; 8import java.util.Properties; 9 10public class JDBCUtils { 11 public static String driverClassName; 12 public static String dbUrl; 13 public static String username; 14 public static String password; 15 16 //静态代码块用于加载配置文件中的数据库信息 17 static { 18 Properties properties = new Properties(); 19 try { 20 properties.load(new FileInputStream("src/db.properties")); 21 }catch (Exception ex){ 22 ex.printStackTrace(); 23 } 24 driverClassName = properties.getProperty("driverClassName"); 25 dbUrl = properties.getProperty("dbUrl"); 26 username = properties.getProperty("username"); 27 password = properties.getProperty("password"); 28 } 29 //加载驱动方法 30 public static void loadDriver(){ 31 try { 32 Class.forName(driverClassName); 33 }catch (Exception ex){ 34 ex.printStackTrace(); 35 } 36 } 37 38 //获取连接 39 public static Connection getConnection(){ 40 Connection conn = null; 41 try { 42 conn = DriverManager.getConnection(dbUrl,username ,password ); 43 }catch (Exception ex){ 44 ex.printStackTrace(); 45 } 46 return conn; 47 } 48 49 public static void release(Connection conn, Statement statement, ResultSet rs){ 50 if(conn!=null){ 51 try{ 52 conn.close(); 53 }catch (Exception e){ 54 e.printStackTrace(); 55 } 56 conn = null; 57 } 58 if(statement!=null){ 59 try { 60 statement.close(); 61 }catch (Exception e){ 62 e.printStackTrace(); 63 } 64 statement = null; 65 } 66 if(rs!=null){ 67 try { 68 rs.close(); 69 }catch (Exception e){ 70 e.printStackTrace(); 71 } 72 rs = null; 73 } 74 } 75}
View Code
3.使用JDBC工具类,重新编写之前的方法(这里以添加方法为例)

1@Test 2 public void addData(){ 3 Connection conn = null; 4 Statement statement = null; 5 try { 6 //加载驱动 7 JDBCUtils.loadDriver(); 8 //获取连接 9 conn = JDBCUtils.getConnection(); 10 //获取执行sql的对象 11 statement = conn.createStatement(); 12 //编写sql 13 String strSql = "insert into user_baseinfo values(null,'2','学生','正常','aa')"; 14 //执行sql并获得返回结果 15 int num = statement.executeUpdate(strSql); 16 if(num>0){ 17 System.out.println("添加成功"); 18 }else{ 19 System.out.println("添加失败"); 20 } 21 }catch (Exception ex){ 22 ex.printStackTrace(); 23 }finally { 24 //释放资源 25 JDBCUtils.release(conn, statement,null); 26 } 27 }
View Code
四、参数化执行sql

1@Test 2 public void deleteData(){ 3 Connection conn = null; 4 PreparedStatement prestatement = null; 5 try { 6 //加载驱动 7 JDBCUtils.loadDriver(); 8 //获取连接 9 conn = JDBCUtils.getConnection(); 10 //编写sql 11 String strSql = "delete from user_baseinfo where id = ?"; 12 //获取连接对象 13 prestatement = conn.prepareStatement(strSql); 14 //设置参数 15 prestatement.setString(1, "11"); 16 //执行sql 17 int num = prestatement.executeUpdate(); 18 if(num>0){ 19 System.out.println("删除成功"); 20 }else{ 21 System.out.println("删除失败"); 22 } 23 }catch (Exception ex){ 24 ex.printStackTrace(); 25 }finally { 26 JDBCUtils.release(conn, prestatement, null); 27 } 28 }
View Code
五、JDBC的批处理
在默认情况下,Mysql的批处理是没有开启的,需要在数据库的url后边加一个参数才能开启(?rewriteBatchedStatements=true)
如:url=jdbc:mysql://localhost:3306/web_test3?rewriteBatchedStatements=true

1.实例代码

1@Test 2 public void addData(){ 3 Connection conn = null; 4 Statement statement = null; 5 try { 6 //加载驱动 7 JDBCUtils.loadDriver(); 8 //获取连接 9 conn = JDBCUtils.getConnection(); 10 //获取执行sql的对象 11 statement = conn.createStatement(); 12 //编写sql 13 String strSql = "insert into user_baseinfo values(null,'2','学生','正常','aa')"; 14 String strSql2 = "insert into user_baseinfo values(null,'3','工人','正常','bb')"; 15 String strSql3 = "insert into user_baseinfo values(null,'4','农民','正常','cc')"; 16 //将要执行的sql添加到批处理里面 17 statement.addBatch(strSql); 18 statement.addBatch(strSql2); 19 statement.addBatch(strSql3); 20 //执行批处理 21 statement.executeBatch(); 22 23 }catch (Exception ex){ 24 ex.printStackTrace(); 25 }finally { 26 //释放资源 27 JDBCUtils.release(conn, statement,null); 28 } 29 }
View Code
2.批处理参数版

1@Test 2 public void addDataPre(){ 3 Connection conn = null; 4 PreparedStatement preStatement = null; 5 try { 6 //加载驱动 7 JDBCUtils.loadDriver(); 8 //获取连接 9 conn = JDBCUtils.getConnection(); 10 //编写sql 11 String strSql = "insert into user_baseinfo values(null,?,?,?,?)"; 12 //预编译sql 13 preStatement = conn.prepareStatement(strSql); 14 for(int i =1;i<=10;i++){ 15 preStatement.setString(1, "tel"+i); 16 preStatement.setString(2, "type"+i); 17 preStatement.setString(3, "statu"+i); 18 preStatement.setString(4, "name"+i); 19 //添加到批处理 20 preStatement.addBatch(); 21 //执行批处理(每5个执行一次) 22 if(i%5==0){ 23 preStatement.executeBatch(); 24 preStatement.clearBatch(); 25 } 26 } 27 }catch (Exception ex){ 28 ex.printStackTrace(); 29 }finally { 30 JDBCUtils.release(conn,preStatement, null); 31 } 32 }
View Code
转载请注明出处:cnblogs.com/Infancy/p/12502329.html