把一些步骤抽取出来然后,从properties获取连接属性。
在项目下新建一个配置文件夹config和配置文件database.properties里面这样写,和map一样以键值对的形式存在,前面键后面值。因为我的密码为空所以不写。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydata?useUnicode=true&characterEncoding=UTF-8
user=root
password=
然后Properties加载配置文件,同时把读取配置文件写在静态代码块中方便只加载一次。
1 static String driver; 2 static String url; 3 static String user; 4 static String password; 5 6 static {// 加载配置文件 7 Properties properties = new Properties(); 8 try { 9 BufferedReader br = new BufferedReader(new FileReader("config/database.properties")); 10 properties.load(br); 11 driver = properties.getProperty("driver"); 12 url = properties.getProperty("url"); 13 user = properties.getProperty("user"); 14 password = properties.getProperty("password"); 15 } catch (FileNotFoundException e) { 16 e.printStackTrace(); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 }
再把获取数据库连接抽取出来
1 /** 获取数据库连接 */ 2 public Connection getConnection() { 3 Connection conn = null; 4 try { 5 // 1、加载驱动 6 Class.forName(driver); 7 // 2、建立连接 8 conn = DriverManager.getConnection(url, user, password); 9 } catch (ClassNotFoundException e) { 10 e.printStackTrace(); 11 } catch (SQLException e) { 12 e.printStackTrace(); 13 } 14 return conn; 15 }
把增删改抽取出来(查询单独出来在上一个博客)
1 /** 2 * 增删改 3 */ 4 public int execute(String sql, Object... args) { 5 Connection conn = null; 6 int result = -1; 7 PreparedStatement ps = null; 8 try { 9 conn = this.getConnection(); 10 ps = conn.prepareStatement(sql); 11 if (args != null) { 12 for (int i = 0; i < args.length; i++) { 13 ps.setObject(i + 1, args[i]); 14 } 15 } 16 result = ps.executeUpdate(); 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 } finally { 20 this.closeAll(null, ps, conn); 21 } 22 23 return result; 24 }
最后把关闭资源抽出来
1 /** 2 * 关闭资源 3 */ 4 5 public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) { 6 try { 7 if (rs != null) 8 rs.close(); 9 if (ps != null) 10 ps.close(); 11 if (conn != null) 12 conn.close(); 13 } catch (SQLException e) { 14 e.printStackTrace(); 15 } 16 }
最后代码就行这样
1 import java.io.BufferedReader; 2 import java.io.FileNotFoundException; 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.util.Properties; 11 12 /** 13 * 数据库连接工具类 14 * 15 * @author W 16 * 17 */ 18 19 public class JDBCUtils { 20 static String driver; 21 static String url; 22 static String user; 23 static String password; 24 25 static {// 加载配置文件 26 Properties properties = new Properties(); 27 try { 28 BufferedReader br = new BufferedReader(new FileReader("config/database.properties")); 29 properties.load(br); 30 driver = properties.getProperty("driver"); 31 url = properties.getProperty("url"); 32 user = properties.getProperty("user"); 33 password = properties.getProperty("password"); 34 } catch (FileNotFoundException e) { 35 e.printStackTrace(); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 /** 获取数据库连接 */ 42 public Connection getConnection() { 43 Connection conn = null; 44 try { 45 // 1、加载驱动 46 Class.forName(driver); 47 // 2、建立连接 48 conn = DriverManager.getConnection(url, user, password); 49 } catch (ClassNotFoundException e) { 50 e.printStackTrace(); 51 } catch (SQLException e) { 52 e.printStackTrace(); 53 } 54 return conn; 55 } 56 57 /** 58 * 关闭资源 59 */ 60 61 public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) { 62 try { 63 if (rs != null) 64 rs.close(); 65 if (ps != null) 66 ps.close(); 67 if (conn != null) 68 conn.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 74 /** 75 * 增删改 76 */ 77 public int execute(String sql, Object... args) { 78 Connection conn = null; 79 int result = -1; 80 PreparedStatement ps = null; 81 try { 82 conn = this.getConnection(); 83 ps = conn.prepareStatement(sql); 84 if (args != null) { 85 for (int i = 0; i < args.length; i++) { 86 ps.setObject(i + 1, args[i]); 87 } 88 } 89 result = ps.executeUpdate(); 90 } catch (SQLException e) { 91 e.printStackTrace(); 92 } finally { 93 this.closeAll(null, ps, conn); 94 } 95 96 return result; 97 } 98 99 }
原文:https://www.cnblogs.com/lingdu9527/p/11006753.html