小弟我是jsp初学者,试着做了个网站,采用的环境配置为JDK1.42,tomcat5.0和access数据库,近日做好了放到网上进行测试,机器为P43.0,内存2G的配置,一开始连上速度还可以,但多刷新几次或jsp页面多点几次就不行了,速度就会越来越慢,甚至会出现空指针的错误,实在不行了重启下tomcat就又没事了.
不知道这个问题怎么解决,恳请各位赐教!
我数据库连接没有采用连接池,不知道有没有关系?
麻烦各位高手把网站发布的一些经验或注意事项告知,不甚感激!
不知道这个问题怎么解决,恳请各位赐教!
我数据库连接没有采用连接池,不知道有没有关系?
麻烦各位高手把网站发布的一些经验或注意事项告知,不甚感激!
{
stmt=conn.createStatement();
try
{
rs=stmt.executeQuery(sql);
while(rs.next())
{
}
}
finally
{
rs.close();
rs=null;
}
}
finally
{
stmt.close();
stmt=null;
}
}
finally
{
conn.close();
conn=null;
}
public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
stmt=conn.createStatement();中加上try{}catch{}finally{}块,例如:try
{
conn=DriverManager.getConnection(strConn,"root","8888");
stmt=conn.createStatement();}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
this.close();
}
package com.wasan.db;import java.sql.*;public class Conn {
private static Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
private static int CON_COUNT=0; public static synchronized Connection getCon()throws Exception{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ec","","");
CON_COUNT++;
return con;
}catch(SQLException e){
System.err.println(e.getMessage());
throw e;
}
} public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
return stmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public ResultSet getRs(String sql){
try{
stmt=getStmtread();
rs=stmt.executeQuery(sql);
return rs;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
return stmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public int getConcount(){
return CON_COUNT;
} public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
return pstmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
然后我在页面中就加入以下jsp代码,其他都是html语言的,
<%
Conn con=new Conn();
ResultSet rs=con.getRs("SELECT * FROM notice ORDER BY issuetime DESC");
while(rs.next()){
String noticename=rs.getString(2);
String path=rs.getString(3);
}
con.close();
%>
非常非常感激!!!
import java.sql.*; public class Conn {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
private static int CON_COUNT=0; public synchronized Connection getCon()throws Exception{
try{
Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/amtium","root","8888");
CON_COUNT++;
return con;
}catch(SQLException e){
close();
System.err.println(e.getMessage());
throw e;
}
} public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
return stmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public ResultSet getRs(String sql){
try{
stmt=getStmtread();
rs=stmt.executeQuery(sql);
return rs;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
return stmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public int getConcount(){
return CON_COUNT;
} public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
return pstmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
} public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
如果你还是有问题的话,肯定像楼上所说的,你连接的方式不好,jdbc-odbc的效率是很低的,应该用jdbc来连,由于不知你是何种数据库,那条formName语句你自己上网找找。还有,就算问题解决了,你这个bean也建议重写,原因是, 你每次操作数据库,都要人工的加上close,万一一个网站有几百个页面,出现了连接耗尽的情况,你就要每个每个页面地查看哪个没加close了,在bean中把它关闭,是最好的办法。再有,你得学习一下使用连接池。好了,不能再当保姆了,没人可以代替你自己思考的。
小弟我用的access数据库,刚学的缘故,我还第一次听说jdbc-odbc的效率是很低的,就知道老师上课跟我们就这么讲,我在网上查了下,似乎jdbc对access的支持不是很好,不知道我改怎样改用jdbc来连接呢?
terry_yip的意见很好,我也觉得每次在页面中关闭要很小心而且烦,我会重写我的bean的,谢谢!