当前位置: 技术问答>java相关
求助啊,关于jsp连接池
来源: 互联网 发布时间:2015-09-17
本文导语: 我模仿别人写了一个数据库连接池类DBConnectionManager。然后想通过servlet建立连接池类对象,再通过一个javabean从连接池对象取得一个连接并获得查询结果,然后从.jsp文件中显示。不知道各位高手能否明白我的意思。具...
我模仿别人写了一个数据库连接池类DBConnectionManager。然后想通过servlet建立连接池类对象,再通过一个javabean从连接池对象取得一个连接并获得查询结果,然后从.jsp文件中显示。不知道各位高手能否明白我的意思。具体应该是怎么个流程,请给与详细解答或提供一完整的例子。谢谢!
|
//使用JNDI技术
public static Connection getDBConnection()
{
ConnectionPoolDataSource ds = null;
PooledConnection pconn = null;
Connection conn = null;
Context initCtx=null;
Context envCtx=null;
try
{
initCtx = new InitialContext();
}
catch (NamingException ne1)
{
System.err.println( "Error on initalcontext:" + ne1.getMessage() );
}
try
{
envCtx = (Context) initCtx.lookup("java:/comp/env");
}
catch (NamingException ne2)
{
System.err.println( "Error on lookup env:" + ne2.getMessage() );
}
try
{
ds = (ConnectionPoolDataSource)envCtx.lookup("jdbc/dbconnection");
}
catch (NamingException ne3)
{
System.err.println( "Error on lookup jndi:" + ne3.getMessage() );
}
int tryCount = 0;
try
{
pconn = ds.getPooledConnection();
conn = pconn.getConnection();
}
catch (SQLException se)
{
//if (tryCount == 2)
System.err.println("tttt" + se.getMessage());
}
return conn;
}
public opendb()
{
initCtx.lookup("java:/comp/env");
}
public ResultSet executeQuery(String sql)
{
rs = null;
try{
conn1 = getDBConnection();
stmt = conn1.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public static Connection getDBConnection()
{
ConnectionPoolDataSource ds = null;
PooledConnection pconn = null;
Connection conn = null;
Context initCtx=null;
Context envCtx=null;
try
{
initCtx = new InitialContext();
}
catch (NamingException ne1)
{
System.err.println( "Error on initalcontext:" + ne1.getMessage() );
}
try
{
envCtx = (Context) initCtx.lookup("java:/comp/env");
}
catch (NamingException ne2)
{
System.err.println( "Error on lookup env:" + ne2.getMessage() );
}
try
{
ds = (ConnectionPoolDataSource)envCtx.lookup("jdbc/dbconnection");
}
catch (NamingException ne3)
{
System.err.println( "Error on lookup jndi:" + ne3.getMessage() );
}
int tryCount = 0;
try
{
pconn = ds.getPooledConnection();
conn = pconn.getConnection();
}
catch (SQLException se)
{
//if (tryCount == 2)
System.err.println("tttt" + se.getMessage());
}
return conn;
}
public opendb()
{
initCtx.lookup("java:/comp/env");
}
public ResultSet executeQuery(String sql)
{
rs = null;
try{
conn1 = getDBConnection();
stmt = conn1.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
|
http://www.java-cn.net/technology/technology_detail.jsp?id=465
http://www.java-cn.net/technology/technology_detail.jsp?id=1185
http://www.java-cn.net/technology/technology_detail.jsp?id=1246
http://www.java-cn.net/technology/technologybrow.jsp
去看看吧
http://www.java-cn.net/technology/technology_detail.jsp?id=1185
http://www.java-cn.net/technology/technology_detail.jsp?id=1246
http://www.java-cn.net/technology/technologybrow.jsp
去看看吧