可以把一些常用的简单SQL进行方法的封装(或者封装成类)
这样你写起来也方便些
例如
select * from tableName where fieldName condition value
-> getResultSet("table name","field name","condition","value")
public boolean nextRow()
throws SQLException {
if( rs==null )
throw new SQLException( "ResultSet is null." ) ;
return rs.next() ;
}
public void openDB( String drvName ,String url ,
String uname ,String passwd )
throws SQLException {
if( conn!=null && !conn.isClosed() )
throw new SQLException( "The connection has been established already." ) ;
clearResult() ;
try {
Class.forName( drvName ) ;
}
catch( ClassNotFoundException ex ) {
throw new SQLException( ex.toString() ) ;
}
conn=DriverManager.getConnection( url ,uname ,passwd ) ;
}
public void openDB( ConnPool pool )
throws SQLException {
if( conn!=null && !conn.isClosed() )
throw new SQLException( "The connection has been established already." ) ;
if( pool==null )
throw new SQLException( "The connection pool cannot be found." ) ;
clearResult() ;
connPool=pool ;
conn=connPool.getConnection() ;
}