What's wrong with my JSP+JavaBean

I

Installman Zhong

The Bean is:
/*
* Created on 2004-7-25
*/
/**
* @author lex
*/
package MyConnFactory;

import java.sql.*;
import java.util.*;

public class ConnFactory {
//connection informations
//jdbc Driver name
private String name;
private String url;
private String usr;
private String pwd;
static private Connection conn;
private Statement stmt;
private ResultSet rs;

//Constructor
public ConnFactory() {
setInfos();
newConnection();
}

private void setInfos() {
//for MySQL
name = "org.gjt.mm.mysql.Driver";
url = "jdbc:mysql://localhost/paktc";
usr = "paktc";
pwd = "paktc";
}

public void getInfos(Vector v) {
v.addElement(name);
v.addElement(url);
v.addElement(usr);
v.addElement(pwd);
}

private boolean newConnection() {
if (conn != null) {
System.out.println("Existing Connection");
return false;
}

try {
Class.forName(name).newInstance();
conn = DriverManager.getConnection(url, usr, pwd);
System.out.println("Connection Success");
} catch (java.lang.Exception ex) {
System.out.println(ex.getMessage());
return false;
}
return true;
}

public boolean closeConnection() {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}

if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}

if (conn != null) {
try {
conn.close();
conn = null;
System.out.println("Clost Connection Success");
return true;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
return false;
}
} else {
System.out.println("No Open Connection");
return false;
}
}

public void showInfos() {
Vector v = new Vector();
getInfos(v);
for (int i = 0; i < v.size(); i++) {
System.out.println(v.get(i));
}
}

public ResultSet readElem(String que) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(que);
return rs;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
return null;
}
}

public synchronized boolean writeElem(String que) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(que);
return true;
} catch (SQLException ex) {
System.out.println(ex.getMessage());
return false;
}
}
}
============================================================================
=
and the JSP is:
<%@ page language="java" import="java.sql.*, java.util.*" %>
<%@ page contentType="text/html;charset=ISO8859_1" %>
<jsp:useBean id="myConn" scope="session" class="MyConnFactory.ConnFactory"/>

<%
ResultSet rs = myConn.readElem("select * from test");
try{
while(rs.next()){
out.print(rs.getString("row"));
out.print("<br>");
}
}catch(SQLException ex){
ex.printStackTrace();
}
myConn.closeConnection();
%>
============================================================================
And the problem is,whenever I request the jsp page,the server(resin-ee) will
idle for a while,about one minute.and then I can't request this page
anymore.
I think that may be the problem of my coding.But I can't find it out.
best regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top