用javabean写了个数据库连接池,在Tomcat里如何配置一些文件?
我用javabean写了一个数据库连接池,但是不懂怎么配置Tomcat里的一些文件,还有,怎么在JSP文件里调用javabean数据库连接池,能不能写个例子,请高手多多指...
我用javabean写了一个数据库连接池,但是不懂怎么配置Tomcat里的一些文件,还有,怎么在JSP文件里调用javabean数据库连接池,能不能写个例子,请高手多多指教,谢谢。我用的是Tomcat 6.0+MySQL 5.0.
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
} catch (InstantiationException e) { e.printStackTrace()}
catch (IllegalAccessException e) { e.printStackTrace();}
catch (ClassNotFoundException e) { e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}
return conn;
}
public synchronized void releaseConnection(Connection conn) {
if (pools.size() >= maxClients)
try {conn.close();} catch (SQLException e) { e.printStackTrace();}
else {
pools.add(conn);
clients--;
notify();
}
}
public synchronized void clear(){
for(int i=0;i<pools.size();i++){
Connection conn =(Connection)pools.get(i);
try {conn.close();} catch (SQLException e) { e.printStackTrace(); }
}
pools.removeAllElements();
}
} 展开
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
} catch (InstantiationException e) { e.printStackTrace()}
catch (IllegalAccessException e) { e.printStackTrace();}
catch (ClassNotFoundException e) { e.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}
return conn;
}
public synchronized void releaseConnection(Connection conn) {
if (pools.size() >= maxClients)
try {conn.close();} catch (SQLException e) { e.printStackTrace();}
else {
pools.add(conn);
clients--;
notify();
}
}
public synchronized void clear(){
for(int i=0;i<pools.size();i++){
Connection conn =(Connection)pools.get(i);
try {conn.close();} catch (SQLException e) { e.printStackTrace(); }
}
pools.removeAllElements();
}
} 展开
展开全部
在tomcat\conf\context.xml写上
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
然后项目web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
把数据库jar包放在tomcat\lib下
写数据库实现类
public Connection getConn()
{
try
{
Context ic = new InitialContext();
DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/restrant");
Connection conn = source.getConnection();
return conn;
}
catch(NamingException nex)
{
nex.printStackTrace();
return null;
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
然后项目web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
把数据库jar包放在tomcat\lib下
写数据库实现类
public Connection getConn()
{
try
{
Context ic = new InitialContext();
DataSource source = (DataSource)ic.lookup("java:comp/env/jdbc/restrant");
Connection conn = source.getConnection();
return conn;
}
catch(NamingException nex)
{
nex.printStackTrace();
return null;
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询