你这个问题,我当时也是遇到了,我把我的发给你,看看
package mypack;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
public class bsDB {
private DataSource ds;
private Connection con;
private Statement stmt=null;
private ResultSet rs=null;
public bsDB () throws Exception{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("No Context");
ds =(DataSource)ctx.lookup("java:comp/env/jdbc/bs");
}
public Connection getConnection()throws Exception{
return ds.getConnection();
}
public void closeConnection(Connection con){
try{
if(con!=null) con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void closePrepStmt(PreparedStatement prepStmt){
try{
if(prepStmt!=null) prepStmt.close();
}catch(Exception e){
e.printStackTrace();
}
} public void closeResultSet(ResultSet crs){
try{
if(crs!=null) crs.close();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean creatConnection()
{
try
{
con=getConnection();
System.out.print("qiaomingzheng");
System.out.print(con);
System.out.print(con);
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return true;
}
public boolean executeUpdate(String sql)
{
if(con==null)
{
creatConnection();
}
try
{
stmt=con.createStatement();
stmt.executeUpdate(sql);
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return true;
}
public ResultSet executeQuery(String sql)
{
try
{
if(con==null)
{
creatConnection();
}
stmt=con.createStatement();
try
{
rs=stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.print(e.getMessage());
return null;
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
return null;
}
return rs;
}
}
把下面的加到tomcat的server.xml中的resource中 <Resource name="jdbc/BookDB"
auth="Container"
type="javax.sql.DataSource" /> <ResourceParams name="jdbc/BookDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter> <!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>dbuser</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter> <!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter> <!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/BookDB?autoReconnect=true</value>
</parameter>
</ResourceParams>
package mypack;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
public class bsDB {
private DataSource ds;
private Connection con;
private Statement stmt=null;
private ResultSet rs=null;
public bsDB () throws Exception{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("No Context");
ds =(DataSource)ctx.lookup("java:comp/env/jdbc/bs");
}
public Connection getConnection()throws Exception{
return ds.getConnection();
}
public void closeConnection(Connection con){
try{
if(con!=null) con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void closePrepStmt(PreparedStatement prepStmt){
try{
if(prepStmt!=null) prepStmt.close();
}catch(Exception e){
e.printStackTrace();
}
} public void closeResultSet(ResultSet crs){
try{
if(crs!=null) crs.close();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean creatConnection()
{
try
{
con=getConnection();
System.out.print("qiaomingzheng");
System.out.print(con);
System.out.print(con);
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return true;
}
public boolean executeUpdate(String sql)
{
if(con==null)
{
creatConnection();
}
try
{
stmt=con.createStatement();
stmt.executeUpdate(sql);
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return true;
}
public ResultSet executeQuery(String sql)
{
try
{
if(con==null)
{
creatConnection();
}
stmt=con.createStatement();
try
{
rs=stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.print(e.getMessage());
return null;
}
}
catch(Exception e)
{
System.out.print(e.getMessage());
return null;
}
return rs;
}
}
把下面的加到tomcat的server.xml中的resource中 <Resource name="jdbc/BookDB"
auth="Container"
type="javax.sql.DataSource" /> <ResourceParams name="jdbc/BookDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter> <!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter> <!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter> <!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter> <!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>dbuser</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter> <!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter> <!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/BookDB?autoReconnect=true</value>
</parameter>
</ResourceParams>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货