当前位置: 技术问答>java相关
有哪些高手做过将对数据库的所有操作封装到一个JAVABEAN中?提供有用建议的就给分,分可一直加……
来源: 互联网 发布时间:2015-04-09
本文导语: 能否将所有对数据库的操作,SELECT、INSERT、UPDATE、DELETE等操作封装到JAVABEAN中?哪位高手做过,能给我提供一个大概的思路吗? 在实现基础操作的基础上,重点是组合查询以及条件的嵌套! 尤其是整个BE...
能否将所有对数据库的操作,SELECT、INSERT、UPDATE、DELETE等操作封装到JAVABEAN中?哪位高手做过,能给我提供一个大概的思路吗?
在实现基础操作的基础上,重点是组合查询以及条件的嵌套!
尤其是整个BEAN应该如何组织、为什么?有什么好处?
谢谢了先!我一定会给分的!!!!!!
在实现基础操作的基础上,重点是组合查询以及条件的嵌套!
尤其是整个BEAN应该如何组织、为什么?有什么好处?
谢谢了先!我一定会给分的!!!!!!
|
我有一个书上的例子,对我这个初学者来说还可以。
package xbook.common ;
import java.lang.* ;
import java.sql.* ;
import javax.servlet.* ;
import xbook.common.* ;
public class SQLBridge {
private ConnPool connPool ;
private Connection conn ;
private ResultSet rs ;
private ResultSetMetaData rsmd ;
private Statement stmt ;
private String driverName ;
private String jdbcURL ;
private String username ;
private String password ;
// -------------------------------------- Constructor --------------------------------------
public SQLBridge() {
connPool=null ;
conn=null ;
rs=null ;
rsmd=null ;
stmt=null ;
}
// -----------------------------------------------------------------------------------------
private void clearResult() throws SQLException {
if( rs!=null ) rs.close() ;
rs=null ;
if( stmt!=null ) stmt.close() ;
stmt=null ;
rsmd=null ;
}
public void closeDB() throws SQLException {
clearResult() ;
if( connPool!=null ) {
connPool.returnConnection() ;
connPool=null ;
}
else {
if( conn==null )
throw new SQLException( "This connection has been closed already." ) ;
if( conn.isClosed() )
throw new SQLException( "This connection has been closed." ) ;
conn.close() ;
}
conn=null ;
}
public int execSQL( String sqlStmt )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "This connection has not been established yet." ) ;
if( sqlStmt==null )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( true ) ;
stmt=conn.createStatement() ;
if( sqlStmt.toUpperCase().startsWith( "SELECT" ) ) {
rs=stmt.executeQuery( sqlStmt ) ;
rsmd=rs.getMetaData() ;
return -1 ;
}
else {
int numRow=stmt.executeUpdate( sqlStmt ) ;
clearResult() ;
return numRow ;
}
}
public void execUpdate( String[] sqlStmts )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "The connection has not been established yet." ) ;
if( sqlStmts==null || sqlStmts.length==0 )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( false ) ;
try {
for( int i=0 ; i
package xbook.common ;
import java.lang.* ;
import java.sql.* ;
import javax.servlet.* ;
import xbook.common.* ;
public class SQLBridge {
private ConnPool connPool ;
private Connection conn ;
private ResultSet rs ;
private ResultSetMetaData rsmd ;
private Statement stmt ;
private String driverName ;
private String jdbcURL ;
private String username ;
private String password ;
// -------------------------------------- Constructor --------------------------------------
public SQLBridge() {
connPool=null ;
conn=null ;
rs=null ;
rsmd=null ;
stmt=null ;
}
// -----------------------------------------------------------------------------------------
private void clearResult() throws SQLException {
if( rs!=null ) rs.close() ;
rs=null ;
if( stmt!=null ) stmt.close() ;
stmt=null ;
rsmd=null ;
}
public void closeDB() throws SQLException {
clearResult() ;
if( connPool!=null ) {
connPool.returnConnection() ;
connPool=null ;
}
else {
if( conn==null )
throw new SQLException( "This connection has been closed already." ) ;
if( conn.isClosed() )
throw new SQLException( "This connection has been closed." ) ;
conn.close() ;
}
conn=null ;
}
public int execSQL( String sqlStmt )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "This connection has not been established yet." ) ;
if( sqlStmt==null )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( true ) ;
stmt=conn.createStatement() ;
if( sqlStmt.toUpperCase().startsWith( "SELECT" ) ) {
rs=stmt.executeQuery( sqlStmt ) ;
rsmd=rs.getMetaData() ;
return -1 ;
}
else {
int numRow=stmt.executeUpdate( sqlStmt ) ;
clearResult() ;
return numRow ;
}
}
public void execUpdate( String[] sqlStmts )
throws SQLException {
if( conn==null || conn.isClosed() )
throw new SQLException( "The connection has not been established yet." ) ;
if( sqlStmts==null || sqlStmts.length==0 )
throw new SQLException( "SQL-statement is null." ) ;
clearResult() ;
conn.setAutoCommit( false ) ;
try {
for( int i=0 ; i