詳解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; }
public ArrayList select (String sql,Class t,Object...params) throws SQLException {//獲取T類所有 public 方法 Method[] declaredMethods = t.getDeclaredMethods(); //建立一個盛放該型別物件集合 ArrayList arrayList=new ArrayList(); try (PreparedStatement pStatement=conn.prepareStatement(sql);) { for ( int i=0;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 {//獲得方陣列裡的 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; }
public void insert (String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for ( int i=0;i { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } }
public void update (String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for ( int i=0;i { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } }
public void delete (String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for ( int i=0;i { pStatement.setObject(i+1, params[i]); } pStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block throw e; } }
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; } }
public ArrayList select (String sql,Class t) throws SQLException { Method[] declaredMethods = t.getDeclaredMethods(); ArrayList 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 { 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; }
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; }
public boolean isExist(String sql,Object...params) throws SQLException { try(PreparedStatement pStatement=conn.prepareStatement(sql);) { for ( int i=0;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 ; }
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(); } }
|
以上就是詳解JDBC資料庫連結及相關方法的封裝的例項詳解,如有疑問請留言或者到本站社群交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支援!