What's wrong with my JSP+JavaBean

Discussion in 'Java' started by Installman Zhong, Jul 26, 2004.

  1. 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.
     
    Installman Zhong, Jul 26, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quick Function

    array in javabean and access from jsp

    Quick Function, Jul 22, 2004, in forum: Java
    Replies:
    3
    Views:
    8,736
    Hal Rosser
    Jul 22, 2004
  2. Replies:
    0
    Views:
    4,464
  3. Dave

    JSP/JavaBean - Newbie help

    Dave, Oct 1, 2006, in forum: Java
    Replies:
    1
    Views:
    397
  4. harryos
    Replies:
    3
    Views:
    1,388
  5. focode

    jsp and JavaBean

    focode, Jan 2, 2009, in forum: Java
    Replies:
    0
    Views:
    362
    focode
    Jan 2, 2009
Loading...

Share This Page