麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > 幫助中心 > 正文

詳解JDBC數據庫鏈接及相關方法的封裝

2024-07-09 22:47:58
字體:
來源:轉載
供稿:網友

詳解JDBC數據庫鏈接及相關方法的封裝

 使用的是MySQL數據庫,首先導入驅動類,然后根據數據庫URL和用戶名密碼獲得數據的鏈接。由于使用的是MySQL數據庫,它的URL一般為,jdbc:mysql://主機地址:端口號/庫名。

  下面是封裝的具體類,用到了泛型和反射,不過還存在些問題,就是對使用的泛型對象有些限制,只能用于泛型類對象屬性名與數據庫表中列名相同的對象,而且初始化對象的方法必須為set+屬性名的方法。本來想通過返回值類型,參數列表來確定該屬性初始化方法的,然而可能是目前學到的還是太少,只學了三周,所以并沒有實現,感覺這個方法還是很low,以后還要繼續完善。本來看到網上有用beanUtils包,利用map將查詢的一列存起來,直接轉化成該對象的,但是就是想試試新學到的反射。而且最后的垃圾回收器并不能如同C++的析構函數一樣,所以關閉數據庫鏈接的地方也需要改善。

實現代碼:

public class Consql { private static Consql consql=null;//單例設計模式 private Connection conn=null;//數據庫鏈接 private final String url;//數據庫url private final String username;//數據庫用戶名 private final String password;//數據庫密碼 //驅動類的加載 static{//以靜態代碼塊的形式加載驅動類,靜態代碼塊只在類加載的時候執行一次 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //構造函數 private Consql(String url,String username,String password) throws SQLException{ this.url = url; this.username = username; this.password = password; open();//創建連接 } private Connection open() throws SQLException { try {//驅動器獲取數據庫鏈接 conn=DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block //e.printStackTrace(); throw e; } return conn; } /** * 帶限制條件查找 * @param sql 帶占位符?的sql語句 * @param t 返回相關類型對象的類(T.class) * @param params 替換占位符的數據,為動態數組 * @return ArrayList<T> * @throws SQLException */ public <T> ArrayList<T> select(String sql,Class<T> t,Object...params) throws SQLException {//獲取T類所有public方法 Method[] declaredMethods = t.getDeclaredMethods(); //創建一個盛放該類型對象集合 ArrayList<T> arrayList=new ArrayList<>(); try (PreparedStatement pStatement=conn.prepareStatement(sql);) { for(int i=0;i<params.length;i++) { pStatement.setObject(i+1, params[i]); } try(ResultSet rSet=pStatement.executeQuery();) { ResultSetMetaData rData=rSet.getMetaData(); //獲取查詢到結果表的列數 int columnCount = rData.getColumnCount(); while (rSet.next()) { T a=t.newInstance();//創建泛型類實例 for(int i=0;i<columnCount;i++) {//獲得方數組里的set方法,這里造成了局限性,只能數據庫表列名與對象名一致,且只能是set方法 String aString="set"+rData.getColumnName(i+1); for (Method method : declaredMethods) { if(method.getParameterCount()==1&&method.getReturnType().toString().equals("void")&&method.getName().equalsIgnoreCase(aString)) {//這里存在問題,前兩個判斷條件基本沒用,主要是最初不想用上面拼串的方式來判斷是不是調用該參數的方法 method.setAccessible(true); //利用反射調用該方法 method.invoke(a, rSet.getObject(i+1)); break; } } } arrayList.add(a); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SQLException e) { // TODO Auto-generated catch block throw e; } return arrayList; } /** * 數據插入 * @param sql 帶占位符?的sql語句 * @param params 替換占位符的數據,動態數組 * @throws SQLException */ public void insert(String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for(int i=0;i<params.length;i++) { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } } /** * 數據更新 * @param sql 帶占位符?的sql語句 * @param params 替換占位符的數據,動態數組 * @throws SQLException */ public void update(String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for(int i=0;i<params.length;i++) { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } } /** * 帶限制條件刪除 * @param sql 帶占位符?的sql語句 * @param params 替換占位符的數據,動態數組 * @throws SQLException */ public void delete(String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for(int i=0;i<params.length;i++) { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } } /** * 刪除全部,不帶有限制 * @param sql * @throws SQLException */ public void deleteall(String sql) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } } /** * 無限制條件查找 * @param sql * @param t 泛型類T.class * @return ArrayList<T> * @throws SQLException */ public <T> ArrayList<T> select(String sql,Class<T> t) throws SQLException { Method[] declaredMethods = t.getDeclaredMethods(); ArrayList<T> arrayList=new ArrayList<>(); try (PreparedStatement pStatement=conn.prepareStatement(sql);) { try(ResultSet rSet=pStatement.executeQuery();) { ResultSetMetaData rData=rSet.getMetaData(); int columnCount = rData.getColumnCount(); while (rSet.next()) { T a=t.newInstance(); for(int i=0;i<columnCount;i++) { String aString="set"+rData.getColumnName(i+1); for (Method method : declaredMethods) { if(method.getName().equalsIgnoreCase(aString)) { method.setAccessible(true); method.invoke(a, rSet.getObject(i+1)); break; } } } arrayList.add(a); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SQLException e) { // TODO Auto-generated catch block throw e; } return arrayList; } /** * 返回表中數據行數 * @param tableName 數據庫表名 * @return 行數 * @throws SQLException */ public int count(String tableName) throws SQLException { String sql="select count(*) from "+tableName; try(PreparedStatement pStatement=conn.prepareStatement(sql); ResultSet rsSet=pStatement.executeQuery(); ) { if(rsSet.next()) { return rsSet.getInt(1); } } catch (SQLException e) { // TODO Auto-generated catch block throw e; } return 0; } /** * 判斷數據是否存在 * @param sql 帶占位符?的sql語句 * @param params 替換占位符的數據,動態數組 * @return boolean * @throws SQLException */ public boolean isExist(String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for(int i=0;i<params.length;i++) { pStatement.setObject(i+1, params[i]); } try(ResultSet rsSet=pStatement.executeQuery();) { if(rsSet.next()) { return true; } } finally { } } catch (SQLException e) { // TODO Auto-generated catch block throw e; } return false; } /** * 創建實例 * @param url 數據庫url * @param username 用戶名 * @param password 密碼 * @return consql對象 * @throws SQLException */ public static Consql getnewInstance(String url,String username,String password) throws SQLException { if(consql==null) consql=new Consql(url, username, password); return consql; } //垃圾回收,貌似并不能達到析構函數的效果 protected void finalize() throws Throwable { if(conn!=null) { conn.close(); } super.finalize(); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 在线亚洲免费 | 龙的两根好大拔不出去h | 九九热在线观看视频 | 欧美不卡 | 国产精品久久久久久久av三级 | 久久精品综合视频 | 久久毛片 | 羞羞网站入口 | 欧美成人国产va精品日本一级 | 久久人体 | 羞羞视频免费视频欧美 | 视频一区二区久久 | 成人毛片视频免费看 | 欧美大逼网 | 国产青草视频在线观看视频 | 亚洲卡通动漫在线观看 | 久久久久久久久久久国产精品 | 亚洲精品久久久久久下一站 | 97超级碰碰人国产在线观看 | 中国女警察一级毛片视频 | 欧美久久久一区二区三区 | 国产羞羞视频在线观看 | 黄色免费入口 | 国产精品99精品 | av成人免费在线观看 | 中文字幕一区二区三区久久 | 日日碰日日操 | 亚洲国产资源 | 全黄裸片武则天艳史 | 黄色毛片视频在线观看 | 国产乱淫a∨片免费视频 | xx53xx| 91九色国产视频 | 羞羞视频.www在线观看 | 久久精品网址 | 久久九九热re6这里有精品 | 久久精品中文字幕一区 | 久久成人综合视频 | 91久久精品一区二区 | 污黄视频在线观看 | 亚洲一级电影在线观看 |