使用JDBC創建數據庫對象(5)
發表于:2007-07-14來源:作者:點擊數:
標簽:
使用JDBC創建數據庫對象(5) · 構建更高級別的JDBC對象 從上面的例子可以明顯看出,如果可以將我們使用過的一些方法封裝在幾個更高級別對象中,那將非常有幫助,我們不僅可以封裝 try 程序塊,而且可以更簡單地訪問 ResultSet 方法。 在這一部分中,我們將
使用JDBC創建數據庫對象(5)
· 構建更高級別的JDBC對象
從上面的例子可以明顯看出,如果可以將我們使用過的一些方法封裝在幾個更高級別對象中,那將非常有幫助,我們不僅可以封裝 try 程序塊,而且可以更簡單地訪問 ResultSet 方法。
在這一部分中,我們將構建一個新的 resultSet 對象,該對象封裝了JDBC ResultSet 對象,并以String數組的形式返回一行數據。我們發現您始終需要從 ResultSetMetaData 對象中獲取列的序號和名稱,因此,創建一個封裝元數據的新對象就非常合理。
另外,我們經常需要按名稱或整數索引提取某行的元素,如果不必總是將這些訪問語句包括 try 塊中,那將大有幫助。最后一點,如果我們需要整行的內容,則更方便的做法是將整行以String數組形式返回。在下面所示的 resultSet 對象中,我們致力于實現這些目標:
CCCCCC"> class resultSet { // 這個類是 JDBC ResultSet 對象的更高級抽象
ResultSet rs; ResultSetMetaData rsmd; int numCols; public resultSet(ResultSet rset) { rs = rset; try { // 同時獲取元數據和列數
rsmd = rs.getMetaData(); numCols = rsmd.getColumnCount(); } catch (Exception e) {System.out.println("resultset error" +e.getMessage());} } //-- public String[] getMetaData() { // 返回包含所有列名或其他元數據的
// 一個數組
String md[] = new String[numCols]; try { for (int i=1; i<= numCols; i++) md[i-1] = rsmd.getColumnName(i); } catch (Exception e) {System.out.println("meta data error"+ e.getMessage());} return md; } //-- public boolean hasMoreElements() { try{ return rs.next(); } catch(Exception e){return false;} } //-- public String[] nextElement() { // 將行的內容復制到字符串數組中
String[] row = new String[numCols]; try { for (int i = 1; i <= numCols; i++) row[i-1] = rs.getString(i); } catch (Exception e) {System.out.println("next element error"+ e.getMessage());} return row; } //-- public String getColumnValue(String columnName) { String res = ""; try { res = rs.getString(columnName); } catch (Exception e) {System.out.println("Column value error:"+ columnName+e.getMessage());} return res; } //-- public String getColumnValue(int i) { String res = ""; try { res = rs.getString(i); } catch (Exception e) {System.out.println("Column value error:"+ columnName+e.getMessage());} return res; } //-- public void finalize() { try{rs.close();} catch (Exception e) {System.out.println(e.getMessage());} } } |
通過簡單使用 new 操作符就地創建一個 ResultSet 對象,我們很容易將任何 ResultSet 對象封裝在此類中:
ResultSet results = .. // 按通常的方法獲得 ResultsSet // 利用它創建一個更有用的對象 resultSet rs = new resultSet(results); |
并很容易在任何 JDBC 程序中使用這個對象。
· 構建一個Database對象
我們沿 00 鏈向上移的另一部分努力是創建一個 Database 對象,它將封裝下列對象的行為: Connection 、 Statement 和 DatabaseMetaData 對象, 以及我們剛剛構建的 SQL 查詢和 resultSet 。我們的 Database 對象允許我們創建連接、獲取表名、在數據庫中移動以及更簡單地獲得行和列的值。請注意, Execute 方法返回一個 resultSet 對象,您可以直接對它進行操作。
class Database { // 這是一個將 JDBC 數據庫的所有功能封裝在單個對象中的類 Connection con; resultSet results; ResultSetMetaData rsmd; DatabaseMetaData dma; String catalog; String types[]; public Database(String driver) { types = new String[1]; types[0] = "TABLES"; // 初始化類型 try{Class.forName(driver);} // 加載 JDBC-ODBC 橋驅動程序 catch (Exception e) {System.out.println(e.getMessage());} } //-- public void Open(String url, String cat) { catalog = cat; try {con = DriverManager.getConnection(url); dma =con.getMetaData(); // 獲取元數據 } catch (Exception e) {System.out.println(e.getMessage());} } //-- public String[] getTableNames() { String[] tbnames = null; Vector tname = new Vector(); // 將表名添加到一個 Vector 中, // 因為我們不知道有多少個表 try { results = new resultSet(dma.getTables(catalog, null, "%", types)); while (results.hasMoreElements()) tname.addElement(results.getColumnValue("TABLE_NAME")); } catch (Exception e) {System.out.println(e);} // 將表名復制到一個 String 數組中 tbnames = new String[tname.size()]; for (int i=0; i< tname.size(); i++) tbnames[i] = (String)tname.elementAt(i); return tbnames; } //-- public String[] getTableMetaData() { // 返回表類型的信息 results = null; try{ results = new resultSet(dma.getTables(catalog, null, "%", types)); } catch (Exception e) {System.out.println(e.getMessage());} return results.getMetaData(); } //-- public String[] getColumnMetaData(String tablename) { // 返回一個列的數據 results = null; try { results = new resultSet(dma.getColumns(catalog, null, tablename, null)); } catch (Exception e) {System.out.println(e.getMessage());} return results.getMetaData(); } //-- public String[] getColumnNames(String table) { // 返回一個列名數組 String[] tbnames = null; Vector tname = new Vector(); try { results = new resultSet(dma.getColumns(catalog, null, table, null)); while (results.hasMoreElements() ) tname.addElement(results.getColumnValue("COLUMN_NAME")); } catch (Exception e) {System.out.println(e);} tbnames = new String[tname.size()]; for (int i=0; i< tname.size(); i++) tbnames[i] = (String)tname.elementAt(i); return tbnames; } //-- public String getColumnValue(String table, String columnName) { // 返回給定列的值 String res = null; try { if (table.length()>0) results = Execute("Select " + columnName + " from " + table + " order by "+columnName); if (results.hasMoreElements()) res = results.getColumnValue(columnName); } catch (Exception e) {System.out.println("Column value error" + columnName+ e.getMessage());} return res; } //-- public String getNextValue(String columnName) { // 使用存儲的 resultSet // 返回該列的下一個值 String res = ""; try { if (results.hasMoreElements()) res = results.getColumnValue(columnName); } catch (Exception e) {System.out.println("next value error"+ columnName+ e.getMessage());} return res; } //-- public resultSet Execute(String sql) { // 對此數據庫執行一個 SQL 查詢 results = null; try { Statement stmt = con.createStatement(); results = new resultSet(stmt.executeQuery(sql)); } catch (Exception e) {System.out.println("execute error"+ e.getMessage());} return results; } } |
(未完待續)
原文轉自:http://www.anti-gravitydesign.com