tomecat4连接池问题 Csdn上多的很,你自己搜一下就有N多! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 正好,我刚写了一段给你: public Connection getPoolDataSource(String jndi) { Connection conn = null; try { InitialContext itial = new InitialContext(); DataSource ds = (DataSource)itial.lookup("java:comp/env/jdbc/"+jndi); conn = ds.getConnection(); } catch(Exception er) { System.out.println("Exception in getPoolDataSource: " + er.toString()); } return conn; } 不用连接池时是这样做的吧:Class.forName(...)Connection conn = DriverManager.getConnection(....);Statement stat = conn.createStatement();现在你就可以这样:public Connection getPoolDataSource(String jndi){Connection conn = null;try { InitialContext itial = new InitialContext(); DataSource ds = (DataSource)itial.lookup("java:comp/env/"+jndi); conn = ds.getConnection(); } catch(Exception er) { System.out.println("Exception in getPoolDataSource: " + er.toString()); } return conn;}Connection con = getPoolDataSource("jdbc/mysql");Statement stat = conn.createStatement();...后面就一样了,只是得到的数据源连接的方式不同"jdbc/mysql"是在tomcat为连接池配的JNDI名字 我原来的java代码是这样的:package online;import java.sql.*;public class onlineconn1{ String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String sConnStr = "jdbc:odbc:online"; //String strUser = "sa"; //String strPassword = "1978sx"; Connection conn = null; public ResultSet rs = null; private java.sql.Statement stmt = null; public onlineconn1() { try { Class.forName(sDBDriver); } catch(java.lang.ClassNotFoundException e) { System.err.println("onlineconn1(): " + e.getMessage()); } } public ResultSet executeQuery(String sql) { rs = null; try { conn = DriverManager.getConnection(sConnStr); Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch(SQLException ex) { System.err.println("aq.executeQuery: " + ex.getMessage()); } return rs; } public void executeUpdate(String sql) { try { conn = DriverManager.getConnection(sConnStr); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate(sql); } catch(SQLException ex) { System.err.println("aq.executeUpdate: " + ex.getMessage()); } } public void disconnectToDB() throws java.sql.SQLException{ if(rs!=null) { rs.close(); rs = null; } if(stmt!=null){ stmt.close(); stmt = null; } if(conn!=null) { conn.close(); conn = null; } }}然后jsp页面中这样调用:<jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";try{ workM.executeQuery(strSQL); if(!workM.rs.next()){ response.sendRedirect("stuloginerror.htm"); } else{ ......现在用连接池怎么改?谢谢! <jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";把上面的改成<jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";Connection con=workM.getConnection();PreparedStatement pre=con.preparedStatement(sql);ResultSet res=pre.executeQuery(); 改为连接池,我原来的java代码应如何改呢?谢谢!package online;import java.sql.*;public class onlineconn1{ String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String sConnStr = "jdbc:odbc:online"; //String strUser = "sa"; //String strPassword = "1978sx"; Connection conn = null; public ResultSet rs = null; private java.sql.Statement stmt = null; public onlineconn1() { try { Class.forName(sDBDriver); } catch(java.lang.ClassNotFoundException e) { System.err.println("onlineconn1(): " + e.getMessage()); } } public ResultSet executeQuery(String sql) { rs = null; try { conn = DriverManager.getConnection(sConnStr); Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch(SQLException ex) { System.err.println("aq.executeQuery: " + ex.getMessage()); } return rs; } public void executeUpdate(String sql) { try { conn = DriverManager.getConnection(sConnStr); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate(sql); } catch(SQLException ex) { System.err.println("aq.executeUpdate: " + ex.getMessage()); } } public void disconnectToDB() throws java.sql.SQLException{ if(rs!=null) { rs.close(); rs = null; } if(stmt!=null){ stmt.close(); stmt = null; } if(conn!=null) { conn.close(); conn = null; } }} tomcat下的两个一样的项目,项目名不一样,运行的时候互串 hibernate 报错 求一个去掉word转html垃圾代码的正则表达式或者已经封装好的方法 ssh三级动态级联的问题 一个小问题!但我找了很久也找不出毛病!求大家帮帮忙 错误:stack overflow at line:37 如何在由servlet重定向到jsp时也能显示页面使用的css样式? 有哪个高手作个定制网页的东西?就是用户可以自己选择字体颜色,大小布局什么的... 多表连接查询的速度问题 在JSP中如何将值传给弹出式页面? 求论坛源代码,请那位达人帮忙 MVC初级问题
给你: public Connection getPoolDataSource(String jndi)
{
Connection conn = null;
try
{
InitialContext itial = new InitialContext();
DataSource ds = (DataSource)itial.lookup("java:comp/env/jdbc/"+jndi);
conn = ds.getConnection();
}
catch(Exception er)
{
System.out.println("Exception in getPoolDataSource: " + er.toString());
}
return conn;
}
Class.forName(...)
Connection conn = DriverManager.getConnection(....);
Statement stat = conn.createStatement();现在你就可以这样:
public Connection getPoolDataSource(String jndi)
{
Connection conn = null;
try
{
InitialContext itial = new InitialContext();
DataSource ds = (DataSource)itial.lookup("java:comp/env/"+jndi);
conn = ds.getConnection();
}
catch(Exception er)
{
System.out.println("Exception in getPoolDataSource: " + er.toString());
}
return conn;
}Connection con = getPoolDataSource("jdbc/mysql");
Statement stat = conn.createStatement();
...后面就一样了,
只是得到的数据源连接的方式不同"jdbc/mysql"是在tomcat为连接池配的JNDI名字
import java.sql.*;
public class onlineconn1
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:online";
//String strUser = "sa";
//String strPassword = "1978sx";
Connection conn = null;
public ResultSet rs = null;
private java.sql.Statement stmt = null;
public onlineconn1()
{
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("onlineconn1(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeUpdate: " + ex.getMessage());
}
}
public void disconnectToDB() throws
java.sql.SQLException{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
} if(conn!=null)
{
conn.close();
conn = null;
}
}
}
然后jsp页面中这样调用:
<jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;
strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
try{
workM.executeQuery(strSQL);
if(!workM.rs.next()){
response.sendRedirect("stuloginerror.htm");
}
else{
......
现在用连接池怎么改?
谢谢!
strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
把上面的改成
<jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;
strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
Connection con=workM.getConnection();
PreparedStatement pre=con.preparedStatement(sql);
ResultSet res=pre.executeQuery();
import java.sql.*;
public class onlineconn1
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:online";
//String strUser = "sa";
//String strPassword = "1978sx";
Connection conn = null;
public ResultSet rs = null;
private java.sql.Statement stmt = null;
public onlineconn1()
{
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("onlineconn1(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql)
{
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeUpdate: " + ex.getMessage());
}
}
public void disconnectToDB() throws
java.sql.SQLException{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
} if(conn!=null)
{
conn.close();
conn = null;
}
}
}