技术开发 频道

热门数据库JDBC驱动试用心得

六、结束语

通过前两部分的说明,相信大家对JDBC的使用应该有相当部分的了解和收获。即使作为初学者,通过简化后的JDBC函数和固定的试用方式,就可以实现通过JSP页面,Java Applet或者是Java Application等程序来访问各种类型的数据库平台了。即使是在本文中没有提到的其他数据库平台,例如:IBM的DB2,Solaris 10中绑定的PostgreSQL数据库。

尤其的,对于中高级技术人员而言,也可以通过这几种热门的数据库平台在不同操作系统下的使用案例得到一定的参考。

其中,特别是介绍了对嵌入式数据库的使用,相信也一定开阔了大家的视野和应用范围,毕竟嵌入式系统的应用在目前也是如火如荼。在后续的实际开发中笔者会将更多的心得体会和大家一起分享。

七、附录:

1.主要FoolDB函数参考
//Get a conn to special database. public static Connection openDB(final String url, final String user, final String passwd) { try { return (DriverManager.getConnection(url, user, passwd) ); } catch (SQLException CONNECT_FAILURE) { … } } //Get a statement object that can be used for query (read only) public static Statement getQueryStat(final Connection conn) { try { return (conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY) ); } catch(SQLException CREATE_QUERY_STATEMENT) { … } } //Get a statement object that can be used for update (can write) public static Statement getExecStat(final Connection conn) { try { return (conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE) ); } catch(SQLException CREATE_EXEC_STATEMENT) { … } } //Execute SQL statement, and get the result set. public static ResultSet openQuery(final Statement stat, final String sql) { try { return (stat.executeQuery(sql) ); } catch(SQLException OPEN_QUERY) { … } } //Get the rows cout of result set. //The result set type should be ResultSet type is TYPE_SCROLL_SENSITIVE. public static int getRowsCount(ResultSet rs) { try { int rowsCount = 0; //Backup current row no. int rowNo = rs.getRow(); //Locate last row rs.last(); //Get the rows count rowsCount = rs.getRow(); //Return back original row if(rowNo < 1) //before first row { rs.beforeFirst(); } else // { rs.absolute(rowNo); } return (rowsCount); } catch(SQLException GET_ROWS_COUNT_FAILURE) { … } } //Get the columns count of resut set. public static int getColsCount(final ResultSet rs) { try { ResultSetMetaData rsmd = rs.getMetaData(); return (rsmd.getColumnCount() ); } catch(SQLException GET_COLS_COUNT_FAILURE) { … } } //Get special column name. //Note: The index of column base 1, but not 0. public static String getColName(final ResultSet rs, final int colIndex) { try { ResultSetMetaData rsmd = rs.getMetaData(); return (rsmd.getColumnName(colIndex) ); } catch(SQLException GET_COL_NAME_FAILURE) { … } } //Move the cursor of result set to next row public static boolean moveNext(ResultSet rs) { try { return (rs.next() ); } catch(SQLException MOVE_NEXT_FAILURE) { … } } //Get the retValue of cell by special row number and column number //The result set type should be ResultSet type is TYPE_SCROLL_SENSITIVE. //Note: The index of row and column all base 1, but no 0. public static Object getValueAt(ResultSet rs, final int rowIndex, final int colIndex) { if( (rowIndex < 1) || (colIndex < 1) ) { return (null); } try { //Backup current row no. int rowNo = rs.getRow(); Object retValue = null; //Locate to special row rs.absolute(rowIndex); //Get retValue retValue = rs.getObject(colIndex); //Return back origianl row rs.absolute(rowNo); return (retValue); } catch(SQLException GET_VALUE_FAILURE) { … } } //Get the retValue of cell by special row number and field name //The result set type should be ResultSet type is TYPE_SCROLL_SENSITIVE. //Note: The index of row and column all base 1, but no 0. public static Object getFieldByName(ResultSet rs, final int rowIndex, final String fieldName) { if( (rowIndex < 1) || (fieldName.equals("") == true) ) { return (null); } try { //Backup current row no. int rowNo = rs.getRow(); Object retValue = null; //Locate to special row rs.absolute(rowNo); //Get retValue retValue = rs.getObject(fieldName); //Return back origianl row no. rs.absolute(rowNo); return (retValue); } catch(SQLException GET_FIELD_BY_NAME_FAILURE) { … } } //Get the retValue of cell within current row by special field name //The result set type should be ResultSet type is TYPE_SCROLL_SENSITIVE. //Note: The index of row and column all base 1, but no 0. public static Object getFieldByName(final ResultSet rs, final String fieldName) { if( (isBOF(rs) == true) || (isEOF(rs) == true) ) { return (null); } try { return (rs.getObject(fieldName) ); } catch(SQLException GET_FIELD_BY_NAME_FAILURE) { … } } //Get the retValue of cell within current row by special column index //The result set type should be ResultSet type is TYPE_SCROLL_SENSITIVE. //Note: The index of row and column all base 1, but no 0. public static Object getFieldByIndex(final ResultSet rs, final int columnIndex) { if( (columnIndex < 1) || (isBOF(rs) == true) || (isEOF(rs) == true) ) { return (null); } try { return (rs.getObject(columnIndex) ); } catch(SQLException GET_FIELD_BY_INDEX_FAILURE) { … } }
0
相关文章