亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解JDBC數(shù)據(jù)庫鏈接及相關(guān)方法的封裝

 更新時間:2017年08月20日 09:32:12   投稿:lqh  
這篇文章主要介紹了詳解JDBC數(shù)據(jù)庫鏈接及相關(guān)方法的封裝的相關(guān)資料,下面是封裝的具體類,用到了泛型和反射,希望能幫助到大家,需要的朋友可以參考下

詳解JDBC數(shù)據(jù)庫鏈接及相關(guān)方法的封裝

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

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

實現(xiàn)代碼:

public class Consql {
 private static Consql consql=null;//單例設(shè)計模式
 private Connection conn=null;//數(shù)據(jù)庫鏈接
 private final String url;//數(shù)據(jù)庫url
 private final String username;//數(shù)據(jù)庫用戶名
 private final String password;//數(shù)據(jù)庫密碼
 //驅(qū)動類的加載
 static{//以靜態(tài)代碼塊的形式加載驅(qū)動類,靜態(tài)代碼塊只在類加載的時候執(zhí)行一次
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }
 //構(gòu)造函數(shù)
 private Consql(String url,String username,String password) throws SQLException{
  this.url = url;
  this.username = username;
  this.password = password;
  open();//創(chuàng)建連接
 }
 private Connection open() throws SQLException
 {
  try {//驅(qū)動器獲取數(shù)據(jù)庫鏈接
   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 返回相關(guān)類型對象的類(T.class)
  * @param params 替換占位符的數(shù)據(jù),為動態(tài)數(shù)組
  * @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();
  //創(chuàng)建一個盛放該類型對象集合
  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();
    //獲取查詢到結(jié)果表的列數(shù)
    int columnCount = rData.getColumnCount();    
    while (rSet.next()) {
     T a=t.newInstance();//創(chuàng)建泛型類實例
     for(int i=0;i<columnCount;i++)
     {//獲得方數(shù)組里的set方法,這里造成了局限性,只能數(shù)據(jù)庫表列名與對象名一致,且只能是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))
       {//這里存在問題,前兩個判斷條件基本沒用,主要是最初不想用上面拼串的方式來判斷是不是調(diào)用該參數(shù)的方法
        method.setAccessible(true);
        //利用反射調(diào)用該方法
        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;  
 }
 /**
  * 數(shù)據(jù)插入
  * @param sql 帶占位符?的sql語句
  * @param params 替換占位符的數(shù)據(jù),動態(tài)數(shù)組
  * @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;
  }
 }
 /**
  * 數(shù)據(jù)更新
  * @param sql 帶占位符?的sql語句
  * @param params 替換占位符的數(shù)據(jù),動態(tài)數(shù)組
  * @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 替換占位符的數(shù)據(jù),動態(tài)數(shù)組
  * @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; 
 }
 /**
  * 返回表中數(shù)據(jù)行數(shù)
  * @param tableName 數(shù)據(jù)庫表名
  * @return 行數(shù)
  * @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;
 }
 /**
  * 判斷數(shù)據(jù)是否存在
  * @param sql 帶占位符?的sql語句
  * @param params 替換占位符的數(shù)據(jù),動態(tài)數(shù)組
  * @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;  
 }
 /**
  * 創(chuàng)建實例
  * @param url 數(shù)據(jù)庫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;  
 }
 //垃圾回收,貌似并不能達到析構(gòu)函數(shù)的效果
 protected void finalize() throws Throwable
 {
  if(conn!=null)
  {
   conn.close();  
  }
  super.finalize();
 }
}

以上就是詳解JDBC數(shù)據(jù)庫鏈接及相關(guān)方法的封裝的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Oracle開啟和關(guān)閉的四種模式

    Oracle開啟和關(guān)閉的四種模式

    這篇文章主要介紹了Oracle開啟和關(guān)閉的四種模式 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • 了解MySQL查詢語句執(zhí)行過程(5大組件)

    了解MySQL查詢語句執(zhí)行過程(5大組件)

    這篇文章主要介紹了了解MySQL查詢語句執(zhí)行過程(5大組件),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • 詳解mysql表數(shù)據(jù)壓縮

    詳解mysql表數(shù)據(jù)壓縮

    mysql進行壓縮是借助于zlib庫,采用L777壓縮算法,這種算法在減少數(shù)據(jù)大小、CPU利用方面是成熟的、健壯的、高效的,這篇文章主要介紹了mysql表數(shù)據(jù)壓縮,需要的朋友可以參考下
    2022-01-01
  • SUSE Linux下通過RPM方式卸載MySQL 5過程筆記

    SUSE Linux下通過RPM方式卸載MySQL 5過程筆記

    這篇文章主要介紹了SUSE Linux下通過RPM方式卸載MySQL 5過程筆記,本文針對使用rpm方式安裝的mysql,需要的朋友可以參考下
    2014-09-09
  • 詳解mysql8.018在linux上安裝與配置過程

    詳解mysql8.018在linux上安裝與配置過程

    這篇文章主要介紹了mysql8.018在linux上安裝與配置過程,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • mysql升級到5.7時,wordpress導數(shù)據(jù)報錯1067的問題

    mysql升級到5.7時,wordpress導數(shù)據(jù)報錯1067的問題

    小編最近把mysql升級到5.7了,wordpress導數(shù)據(jù)報錯,導入數(shù)據(jù)庫時報1067 – Invalid default value for ‘字段名’的問題,怎么解決這個問題,下面小編把我的解決方案分享到腳本之家平臺供大家參考,希望對大家有所幫助
    2021-05-05
  • mysql利用覆蓋索引避免回表優(yōu)化查詢

    mysql利用覆蓋索引避免回表優(yōu)化查詢

    這篇文章主要給大家介紹了關(guān)于mysql如何利用覆蓋索引避免回表優(yōu)化查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Mysql按條件計數(shù)多種實現(xiàn)方法詳解

    Mysql按條件計數(shù)多種實現(xiàn)方法詳解

    這篇文章主要介紹了Mysql按條件計數(shù)多種實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • mysql的innodb和myisam的區(qū)別及說明

    mysql的innodb和myisam的區(qū)別及說明

    這篇文章主要介紹了mysql的innodb和myisam的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • MySQL外鍵約束(FOREIGN KEY)的具體使用

    MySQL外鍵約束(FOREIGN KEY)的具體使用

    MySQL外鍵約束是表的一個特殊字段,經(jīng)常與主鍵約束一起使用,本文主要介紹了MySQL外鍵約束(FOREIGN KEY)的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05

最新評論