妯′豢Common DBUtils鐨勫姛鑳藉疄鐜拌嚜瀹氫箟鐨凞BUtils
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了妯′豢Common DBUtils鐨勫姛鑳藉疄鐜拌嚜瀹氫箟鐨凞BUtils相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/set' title='set'>set color count 涓暟 date rac one final common
鈶犲鍏ラ渶瑕佺殑jar鍖呭拰閰嶇疆鏂囦欢
鈶¤緟鍔╃被
1 package com.yxfyg.util; 2 3 import java.sql.Connection; 4 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import com.mchange.v2.c3p0.ComboPooledDataSource; 9 10 public class JDBCUtil { 11 12 private static ComboPooledDataSource dataSource = null; 13 14 static { 15 dataSource = new ComboPooledDataSource(); 16 } 17 18 public static Connection getConnection() { 19 Connection conn = null; 20 try { 21 conn = dataSource.getConnection(); 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 public static ComboPooledDataSource getDataSource() { 29 return dataSource; 30 } 31 32 public static void release(ResultSet rs,Statement st,Connection conn) { 33 closeRs(rs); 34 closeSt(st); 35 closeConn(conn); 36 } 37 38 public static void release(Statement st,Connection conn) { 39 closeSt(st); 40 closeConn(conn); 41 } 42 43 private static void closeRs(ResultSet rs) { 44 try { 45 if(rs != null) { 46 rs.close(); 47 } 48 }catch(SQLException e) { 49 e.printStackTrace(); 50 }finally { 51 rs = null; 52 } 53 } 54 55 private static void closeSt(Statement st) { 56 try { 57 if(st != null) { 58 st.close(); 59 } 60 }catch(SQLException e) { 61 e.printStackTrace(); 62 }finally { 63 st = null; 64 } 65 } 66 67 private static void closeConn(Connection conn) { 68 try { 69 if(conn != null) { 70 conn.close(); 71 } 72 }catch(SQLException e) { 73 e.printStackTrace(); 74 }finally { 75 conn = null; 76 } 77 } 78 }
1 package com.yxfyg.bean; 2 3 public class Account { 4 5 private int id; 6 7 private String name; 8 9 private int money; 10 11 public int getId() { 12 return id; 13 } 14 15 public void setId(int id) { 16 this.id = id; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public int getMoney() { 28 return money; 29 } 30 31 public void setMoney(int money) { 32 this.money = money; 33 } 34 35 @Override 36 public String toString() { 37 return "Account [id=" + id + ", name=" + name + ", money=" + money + "]"; 38 } 39 40 }
鈶㈣嚜瀹氫箟鐨凞BUtils鍜岄渶瑕佷娇鐢ㄨ€呰繘琛屽疄鐜扮殑鎺ュ彛
1 package com.yxfyg.dbutils; 2 3 import java.sql.Connection; 4 import java.sql.ParameterMetaData; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 import com.yxfyg.util.JDBCUtil; 10 11 public class MyDBUtils { 12 13 // 澧炪€佸垹銆佹敼 14 public int update(String sql, Object... args) { 15 // 鑾峰彇杩炴帴 16 Connection conn = JDBCUtil.getConnection(); 17 PreparedStatement ps = null; 18 int update = 0; 19 try { 20 ps = conn.prepareStatement(sql); 21 // 纭畾sql璇彞涓棶鍙风殑涓暟 22 ParameterMetaData data = ps.getParameterMetaData(); 23 int count = data.getParameterCount(); 24 for (int i = 0; i < count; i++) { 25 ps.setObject(i + 1, args[i]); 26 } 27 update = ps.executeUpdate(); 28 } catch (SQLException e) { 29 e.printStackTrace(); 30 } finally { 31 JDBCUtil.release(ps, conn); 32 } 33 return update; 34 } 35 36 public <T> T query(String sql, ResultSetHandler<T> handler, Object... args) { 37 38 // 鑾峰彇杩炴帴 39 Connection conn = JDBCUtil.getConnection(); 40 PreparedStatement ps = null; 41 ResultSet rs = null; 42 T t = null; 43 try { 44 ps = conn.prepareStatement(sql); 45 // 纭畾sql璇彞涓棶鍙风殑涓暟 46 ParameterMetaData data = ps.getParameterMetaData(); 47 int count = data.getParameterCount(); 48 for (int i = 0; i < count; i++) { 49 ps.setObject(i + 1, args[i]); 50 } 51 rs = ps.executeQuery(); 52 53 //灏嗙粨鏋滈泦浜ょ粰鐢变娇鐢ㄨ€呭疄鐜扮殑绫籖esultSetHandler鐨勫璞″鐞?/span> 54 t = handler.handle(rs); 55 56 } catch (SQLException e) { 57 e.printStackTrace(); 58 } finally { 59 JDBCUtil.release(rs , ps, conn); 60 } 61 return t; 62 } 63 64 }
1 package com.yxfyg.dbutils; 2 3 import java.sql.ResultSet; 4 5 public interface ResultSetHandler<T> { 6 7 T handle(ResultSet rs); 8 9 }
鈶d娇鐢?/p>
1 package com.yxfyg.test; 2 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.List; 6 7 import org.junit.Test; 8 9 import com.yxfyg.bean.Account; 10 import com.yxfyg.dbutils.MyDBUtils; 11 import com.yxfyg.dbutils.ResultSetHandler; 12 13 public class MainTest { 14 15 @Test 16 public void testUpdate() { 17 18 MyDBUtils myUtils = new MyDBUtils(); 19 20 // 澧炪€佸垹銆佹敼閮戒娇鐢╱pdate鏂规硶锛屾煡浣跨敤query鏂规硶 21 // 鎻掑叆涓€鏉℃暟鎹?/span> 22 myUtils.update("insert into account values(null,?,?)","Lucy",2000); 23 24 // 淇敼涓€鏉℃暟鎹?/span> 25 myUtils.update("update account set money = money - ? where name = ?", 300, "Lucy"); 26 27 // 鍒犻櫎涓€鏉℃暟鎹?/span> 28 myUtils.update("delete from account where name = ?","Lucy"); 29 30 } 31 32 @Test 33 public void testQuery() { 34 35 MyDBUtils myUtils = new MyDBUtils(); 36 37 // 鏌ヨ涓€鏉℃暟鎹?浣跨敤鍖垮悕鍐呴儴绫伙紝鑷繁瀹炵幇鏂规硶) 38 Account account = myUtils.query("select * from account where name = ?", new ResultSetHandler<Account>() { 39 40 @Override 41 public Account handle(ResultSet rs) { 42 Account account = new Account(); 43 try { 44 while (rs.next()) { 45 account.setId(rs.getInt("id")); 46 account.setName(rs.getString("name")); 47 account.setMoney(rs.getInt("money")); 48 } 49 } catch (SQLException e) { 50 e.printStackTrace(); 51 } 52 return account; 53 } 54 }, "Jack"); 55 56 System.out.println(account.toString()); 57 } 58 59 }
以上是关于妯′豢Common DBUtils鐨勫姛鑳藉疄鐜拌嚜瀹氫箟鐨凞BUtils的主要内容,如果未能解决你的问题,请参考以下文章
馃専 Chrome DevTools 涓冨ぇ鏂板姛鑳?/a>
涓ゅ垎閽熶簡瑙e叏涓栫晫鏈€蹇殑 Sketch 鎻掍欢锛歊unner