请看源代码:
类com.wf.db.cMdbResult
{
public com.ibm.db.SelectResult rs=null;
}
public boolean close() {
rs.close();
rs=null;
}
类com.wf.db.conpool
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.ibm.db.*;
import java.sql.*;
import javax.sql.*;
import com.ibm.ejs.dbm.jdbcext.*;
import javax.naming.*;
public class conpool{
private Connection conn=null;
public static DataSource ds = null;
private DatabaseConnection connBean=null;
private int MaxRowCount=0;
}
public void disconnect()
{
if (conn != null)
conn.close();
}
public boolean GetDBConnection(DataSource ds,String user,String password ) {
conn = ds.getConnection(user, password);
connBean = new DatabaseConnection(conn);
}
public boolean GetDBConnection(Connection conn1 ) {
conn=conn1;
connBean = new DatabaseConnection(conn);
}
public MdbResult QuerySQL(String strQuery)
{
try{
MdbResult mresult=new MdbResult();
SelectResult result=null;
SelectStatement sqlStatement = new SelectStatement();
sqlStatement.setConnection(connBean);
StatementMetaData metaData = sqlStatement.getMetaData();
metaData.setSQL(strQuery);
int rownum=getMaxRowCount();
if(rownum>0){
sqlStatement.setMaximumRows(rownum);
}
sqlStatement.execute();
result = sqlStatement.getResult();
mresult.rs=result;
tfmetaData=metaData;
finally{
result.close();
result=null;
sqlStatement.close();
sqlStatement=null;
}
return mresult;
}
调用例子:
public void performTask(
javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res)
{
com.wf.db.conpool db = new com.wf.db.conpool();
Connection conn = null;
try
{
com.wf.db.MdbResult rs = new com.wf.db.MdbResult();
conn = ds.getConnection('aa', 'aa');
if (db.GetDBConnection(conn) == false)
{
errstr =
errstr + "Connection To Database Error:" + db.ErrorInfo + db.test + "<br>";
}
else
{
sql = "select * from dic_dept where groupname not like '%*%'";
com.wf.db.MdbResult dicDept = new com.wf.db.MdbResult();
dicDept = db.QuerySQL(sql);
finally
{
db.disconnect();
conn=null;
}
}