JSP+JAVABEAN连接数据库.每隔一段时间就打不开网页了.需要重启TOCMAT才行.请高人帮我看看,程序有没有问题?
数据源JAVABEAN:
第一个DBConnSource.java
/*
* 数据源连接BEAN
*/
package mybean;import java.sql.*;
import javax.sql.*;
import javax.naming.*;public class DBConnSource {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
public DBConnSource(String dsName){
try{
Context initCtx = new InitialContext();
Context ctx =(Context)initCtx.lookup("java:comp/env");
DataSource ds =(DataSource)ctx.lookup(dsName);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.print(e.toString());
}
}
public synchronized Statement getStmt()throws Exception
{
stmt=conn.createStatement();
return stmt;
}
public synchronized PreparedStatement getPstmt(String sql)throws Exception
{
pstmt=conn.prepareStatement(sql);
return pstmt;
}
public void DBclose(){
try{
conn.close(); :重点是这里.这个conn.close 到底有没有被执行到.
}catch(Exception e){
System.out.print(e.toString());
}
}
}第二个TitleList.java
/*
* 显示帖子列表只显示前10条.
* 使用数据源连接.
*
*/
package mybean;import java.sql.*;import mybean.DBConnSource;public class TitleList { private String tableName="";
private Statement stmt;
public TitleList(){}
public void setTableName(String n){
this.tableName=n;
}
public StringBuffer getBuffer(){
StringBuffer buffer=new StringBuffer();
try{
DBConnSource dbc=new DBConnSource("jdbc/myweb");
stmt=dbc.getStmt();
}catch(Exception e){
System.out.print("不能连接到数据源"+e.toString());
}
try{
String strSql="SELECT * FROM "+tableName+" ORDER BY id DESC LIMIT 10";
ResultSet rs = stmt.executeQuery(strSql);
rs.first();
do{
String title=rs.getString("title");
buffer.append("<a href='cont.jsp?nid="+rs.getInt("id")+"' target=_blank>"+title+"</a></BR>");
}while(rs.next());
rs.close();
stmt.close();
}
catch(SQLException e){
System.out.print("数据连接错误."+e.toString());
}
return buffer;
}
}这两段代码.就是定义一个连接数据库的JAVABEAN,然后在每个需要使用动态数据的JAVABEAN里调用这个JAVABEAN.
象上述写法.那个conn.close(),有没有被执行到.
另外我的TOMCAT每隔一段时间就需要重启一下.还可能是什么原因?
谢谢
只有20分了.希望大家多多帮忙.
数据源JAVABEAN:
第一个DBConnSource.java
/*
* 数据源连接BEAN
*/
package mybean;import java.sql.*;
import javax.sql.*;
import javax.naming.*;public class DBConnSource {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
public DBConnSource(String dsName){
try{
Context initCtx = new InitialContext();
Context ctx =(Context)initCtx.lookup("java:comp/env");
DataSource ds =(DataSource)ctx.lookup(dsName);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.print(e.toString());
}
}
public synchronized Statement getStmt()throws Exception
{
stmt=conn.createStatement();
return stmt;
}
public synchronized PreparedStatement getPstmt(String sql)throws Exception
{
pstmt=conn.prepareStatement(sql);
return pstmt;
}
public void DBclose(){
try{
conn.close(); :重点是这里.这个conn.close 到底有没有被执行到.
}catch(Exception e){
System.out.print(e.toString());
}
}
}第二个TitleList.java
/*
* 显示帖子列表只显示前10条.
* 使用数据源连接.
*
*/
package mybean;import java.sql.*;import mybean.DBConnSource;public class TitleList { private String tableName="";
private Statement stmt;
public TitleList(){}
public void setTableName(String n){
this.tableName=n;
}
public StringBuffer getBuffer(){
StringBuffer buffer=new StringBuffer();
try{
DBConnSource dbc=new DBConnSource("jdbc/myweb");
stmt=dbc.getStmt();
}catch(Exception e){
System.out.print("不能连接到数据源"+e.toString());
}
try{
String strSql="SELECT * FROM "+tableName+" ORDER BY id DESC LIMIT 10";
ResultSet rs = stmt.executeQuery(strSql);
rs.first();
do{
String title=rs.getString("title");
buffer.append("<a href='cont.jsp?nid="+rs.getInt("id")+"' target=_blank>"+title+"</a></BR>");
}while(rs.next());
rs.close();
stmt.close();
}
catch(SQLException e){
System.out.print("数据连接错误."+e.toString());
}
return buffer;
}
}这两段代码.就是定义一个连接数据库的JAVABEAN,然后在每个需要使用动态数据的JAVABEAN里调用这个JAVABEAN.
象上述写法.那个conn.close(),有没有被执行到.
另外我的TOMCAT每隔一段时间就需要重启一下.还可能是什么原因?
谢谢
只有20分了.希望大家多多帮忙.
解决方案 »
- Filter 使用的问题
- 我查询出的结果怎么一直为空?
- java输出excel 报错IOException not an ISO 8859_1 character
- 关于Resin目录映射与URL重写Rewrite技巧的总结
- 关于text文本框的OnChange事件
- (Tomcat/5.5.26+myeclipse5.5.1GA+jdk1.5.0_06)javabean的问题
- <html:select property="kc" multiple>?? help
- 关于request.getCharacterEncoding() 急!!!!!
- 可不可能在发布jsp时只发布编译好的class,而不发布jsp文件,怎么做??
- throws 这个关键字是什么意思???
- ajax 老是说缺少'}' 这个怎么解决啊???
- Jsp保存按钮
/*
* 数据源连接BEAN
*/
package mybean;import java.sql.*;
import javax.sql.*;
import javax.naming.*;public class DBConnSource {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
public DBConnSource(String dsName){
try{
Context initCtx = new InitialContext();
Context ctx =(Context)initCtx.lookup("java:comp/env");
DataSource ds =(DataSource)ctx.lookup(dsName);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.print(e.toString());
}
}
public synchronized Statement getStmt()throws Exception
{
stmt=conn.createStatement();
return stmt;
}
public synchronized PreparedStatement getPstmt(String sql)throws Exception
{
pstmt=conn.prepareStatement(sql);
return pstmt;
}
public void DBclose(){
try{
conn.close(); :重点是这里.这个conn.close 到底有没有被执行到.
}catch(Exception e){
System.out.print(e.toString());
}
}
}第二个TitleList.java
/*
* 显示帖子列表只显示前10条.
* 使用数据源连接.
*
*/
package mybean;import java.sql.*;import mybean.DBConnSource;public class TitleList {private String tableName="";
private Statement stmt;
public TitleList(){}
public void setTableName(String n){
this.tableName=n;
}
public StringBuffer getBuffer(){
StringBuffer buffer=new StringBuffer();
try{
DBConnSource dbc=new DBConnSource("jdbc/myweb");
stmt=dbc.getStmt();
}catch(Exception e){
System.out.print("不能连接到数据源"+e.toString());
}
try{
String strSql="SELECT * FROM "+tableName+" ORDER BY id DESC LIMIT 10";
ResultSet rs = stmt.executeQuery(strSql);
rs.first();
do{
String title=rs.getString("title");
buffer.append("<a href='cont.jsp?nid="+rs.getInt("id")+"' target=_blank>"+title+"</a></BR>");
}while(rs.next());
rs.close();
stmt.close();
}
catch(SQLException e){
System.out.print("数据连接错误."+e.toString());
}
return buffer;
}
}
tomcat重新启动是什么原因要重新启动啊?public LoginActionForm selectLoginInfo(String uName,String pwd)
{
// 用户信息存储集合
LoginActionForm loginForm = new LoginActionForm();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "";
try
{
// 取得连接
conn = GetConnection.getConn();
// sql语句
sql = "select * from TJDXGJ.dbo.UT_COMPANYINFO where tcUserName='"+uName+"' and tcUserPass='"+pwd+"'";
// sql执行
pst = conn.prepareStatement(sql);
// 结果集取得
rs = pst.executeQuery();
if(rs.next())
{
// 企业编码
loginForm.setCompanyId(rs.getInt("tnCompanyId"));
}
}catch (SQLException e) {
e.printStackTrace();
} finally {
GetConnection.closeRs(rs);
GetConnection.closeStmt(pst);
GetConnection.closeConn(conn); }
// 返回用户信息
return loginForm;
}
2012-3-10 8:12:49 org.apache.coyote.http11.Http11Protocol pause
信息: Pausing Coyote HTTP/1.1 on http-10152
2012-3-10 8:12:50 org.apache.catalina.connector.Connector pause
严重: Protocol handler pause failed
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at java.net.Socket.<init>(Socket.java:372)
at java.net.Socket.<init>(Socket.java:215)
at org.apache.jk.common.ChannelSocket.unLockSocket(ChannelSocket.java:487)
at org.apache.jk.common.ChannelSocket.pause(ChannelSocket.java:284)
at org.apache.jk.server.JkMain.pause(JkMain.java:683)
at org.apache.jk.server.JkCoyoteHandler.pause(JkCoyoteHandler.java:153)
at org.apache.catalina.connector.Connector.pause(Connector.java:1073)
at org.apache.catalina.core.StandardService.stop(StandardService.java:563)
at org.apache.catalina.core.StandardServer.stop(StandardServer.java:744)
at org.apache.catalina.startup.Catalina.stop(Catalina.java:633)
at org.apache.catalina.startup.Catalina.start(Catalina.java:608)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
2012-3-10 8:12:51 org.apache.catalina.core.StandardService stop
信息: Stopping service Catalina
2012-3-10 8:12:52 org.apache.coyote.http11.Http11Protocol destroy
信息: Stopping Coyote HTTP/1.1 on http-10152
2012-3-10 8:13:04 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\wwwroot\xuwei770214\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Intel\DMIX;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;D:\PHPnow\MySQL-5.0.15b\bin;;D:\JDK1.6\bin;D:\JDK1.6\jre\bin
2012-3-10 8:13:05 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-10152
2012-3-10 8:13:05 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 6776 ms
2012-3-10 8:13:05 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2012-3-10 8:13:05 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.20
2012-3-10 8:13:06 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(D:\wwwroot\xuwei770214\webapps\ROOT\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2012-3-10 8:13:07 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-10152
2012-3-10 8:13:08 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:20152
2012-3-10 8:13:08 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/375 config=null
2012-3-10 8:13:08 org.apache.catalina.startup.Catalina start
信息: Server startup in 2926 ms
try{
//conn.close(); :重点是这里.这个conn.close 到底有没有被执行到.
if(conn != null){
conn.close();
}
}catch(Exception e){
System.out.print(e.toString());
}finally{
if(conn != null){
conn.close();
}
conn = null;
}
}
Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
这一系列组合本身就是一个事务处理,放在一个try ,catch里边就可以,统一完成一个事务,如果抛异常都差不多,最后写关闭conn,java是一个比较神奇的东西,谁也不知道下一秒钟它会干点啥!
还有你的tom是启动报错,不是运行报错,建议你清一下tom内部缓存,tomcat是一个更神寄的东西,代码组合一下。tom缓存清理一下
做了许多测试后.在远程空间的stdout日志里找到"org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object不能连接到数据源"这个提示.
看来还是代码出了问题.请大家再帮我看看上面的代码.谢谢.
我在DBConnSource里定义的那个close()要怎么正确调用呢?
另外,还有一个问题.象我这样定义了一个连接数据库的JAVABEAN,然后在数据操作的JAVABEAN里调用的方式,要怎么关闭数据库连接比较合适.是否需要写一个专门依次关闭ResultSet rs, Statement stmt, connection conn 的JAVABEAN 然后在每个数据操作的页面里调用.
rs.close();
stmt.close();
}这块不对。
应该是,在最后,catch后,给个finally{
if(rs != null){
rs.close();
}if(stmt != null){
stmt.close();
}if(rs != null){
rs.close();
}
}
重新修改了代码;package mybean;
import java.sql.*;
import mybean.DBConnSource;
public class StyleList { private String tableName;
private Statement stmt;
private ResultSet rs;
public StyleList(){}
public void setTableName(String n){
this.tableName=n;
}
public StringBuffer getBuffer(){
StringBuffer buffer=new StringBuffer();
try{
DBConnSource dbc=new DBConnSource("jdbc/myweb");
stmt=dbc.getStmt();
}catch(Exception e){
System.out.print("不能连接到数据源");
}
try{
String strSql="SELECT * FROM "+tableName;
rs = stmt.executeQuery(strSql);
while(rs.next()){
buffer.append("<a href='style.jsp?style="+rs.getString("style")+"' target=_blank>"+rs.getString("style")+"</a>");
buffer.append(" ");
}
}catch(SQLException e){
System.out.print(e.toString());
}finally{
try{ //关闭 ResultSet rs.
if(rs!= null){
rs.close();}
}catch(SQLException e){
System.out.print(e.toString());
}finally{
rs=null;
}
try{ // 关闭 Statement stmt.
if(stmt!= null){
stmt.close();}
}catch(SQLException e){
System.out.print(e.toString());
}finally{
stmt=null;
}
}
return buffer;
}
}
另外我在DBConnSource.java中重新修改了DBclose()./*
* 数据源连接BEAN
*/
package mybean;import java.sql.*;
import javax.sql.*;
import javax.naming.*;public class DBConnSource {
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
public DBConnSource(String dsName){
try{
Context initCtx = new InitialContext();
Context ctx =(Context)initCtx.lookup("java:comp/env");
DataSource ds =(DataSource)ctx.lookup(dsName);
conn = ds.getConnection();
}
catch(Exception e)
{
System.out.print(e.toString());
}
}
public synchronized Statement getStmt()throws Exception
{
stmt=conn.createStatement();
return stmt;
}
public synchronized PreparedStatement getPstmt(String sql)throws Exception
{
pstmt=conn.prepareStatement(sql);
return pstmt;
}
public void DBclose(){
try{ //关闭 Connection conn;
if(conn!=null){
conn.close();
}
}catch(Exception e){
System.out.print(e.toString());
}finally{
conn=null;
}
try{ //关闭Statement stmt;
if(stmt!=null){
stmt.close();
}
}catch(Exception e){
System.out.print(e.toString());
}finally{
stmt=null;
}
try{ //关闭 PreparedStatement pstmt;
if(pstmt!=null){
pstmt.close();
}
}catch(Exception e){
System.out.print(e.toString());
}finally{
pstmt=null;
}
}
}
按照这种写法.我要怎么调用DBConnSource.java里的DBclose();
另外我还有一个问题.关于这个 stmt,我在第一个JAVABEAN里已经关闭了stmt.在第二个DBConnSource.java里是否还需要再关闭一次.那个pstmt也是同样的问题.在用到pstmt的地方关闭了pstmt以后,是否还需要通过这个DBclose()再关闭一次.
谢谢.
if(rs != null){
rs.close();
}if(stmt != null){
stmt.close();
}if(dbc.getConn != null){
conn.close();
}
}}
try{
conn.close(); :重点是这里.这个conn.close 到底有没有被执行到.
}catch(Exception e){
System.out.print(e.toString());
}
}
你这个方法没有调用,conn.close什么可能被执行?
* 网站JAVABEAN
* 所有帖子分类列表.
* 使用数据源连接.
* 前后台共用JAVABEAN.
*/
package mybean;import java.sql.*;import mybean.DBConnSource;public class StyleList {
private String tableName;
private Statement stmt;
private ResultSet rs;
public StyleList(){}
public void setTableName(String n){
this.tableName=n;
}
public StringBuffer getBuffer(){
StringBuffer buffer=new StringBuffer();
try{
DBConnSource dbc=new DBConnSource("jdbc/myweb");
stmt=dbc.getStmt();
}catch(Exception e){
System.out.print("不能连接到数据源");
}
try{
String strSql="SELECT * FROM "+tableName;
rs = stmt.executeQuery(strSql);
while(rs.next()){
buffer.append("<a href='style.jsp?style="+rs.getString("style")+"' target=_blank>"+rs.getString("style")+"</a>");
buffer.append(" ");
}
}catch(SQLException e){
System.out.print(e.toString());
}finally{
try{ //关闭 ResultSet rs.
if(rs!= null){
rs.close();}
}catch(SQLException ex){
System.out.print(ex.toString());
}finally{
rs=null;
}
try{ // 关闭 Statement stmt.
if(stmt!= null){
stmt.close();}
}catch(SQLException ex){
System.out.print(ex.toString());
}finally{
stmt=null;
}
try{
if(conn != null){ //这里报错无法解析conn;
conn.close();} //这里也报错无法解析conn; }catch(SQLException ex){
System.out.print(ex.toString());
}finally{
conn=null; //这里也报错无法解析conn;
}
}
return buffer;
}
}请问是怎么回事?
我要怎么样才能关闭引用的数据连接封装JAVABEAN里的数据连接.
谢谢