实例涉及到的操作系统是MS Windows XP + SP2和SUN Solaris 8,数据库平台有:MS Access 2000(以下简称Access),MS SQL Server 2000(以下简称SQL Server),My SQL,Oracle和Java DB(J2SE 1.6.0中绑定)。
SUN公司提供的SQL Server的JDBC驱动的版本应该比较陈旧,所以可能导致在操作数据库功能支持和效率方面比当前新的JDBC驱动要差一些。Microsoft提供的SQL Server JDBC驱动类名为:“com.microsoft.jdbc.sqlserver.SQLServerDriver”,连接字符串形如:“jdbc:microsoft:sqlserver://hostname:port;DataBaseName=dbname”。
(3)MySQL数据库
众所周知,MySQL数据库既可以在Windows可以在Solaris平台运行,而且执行效率也深得业界的好评。本试例中连接MySQL使用的是MySQL专用驱动(在第一部分已经详述,在MySQL官方网页有很多的支持文档),以下是关键代码:
final String connectStr = “jdbc:mysql://localhost/phome”;
final String userName = “root”; //
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){…}} 作者: qingshangwuheng 时间: 2011-7-9 14:18
希望楼主将各数据库的配置找出来,尽量做到统一后再进行测试,谢谢……