0

我在我的小型 JSP 项目中遇到了 MySQL 的问题。我之前问过一个关于这个的问题(http://stackoverflow.com/questions/9860129/jsp-mysql-jar-file-stops-working),这是一个基于该问题的信息表单的后续问题。

该类工作,页面从数据库中获取信息几天,然后它停止工作,直到我重新启动 Tomcat 服务器或再次部署 war 文件。

这是我的 MySQL 连接类,任何想法将不胜感激:

public class Connect {
    final String host = "localhost";
    final String username = "admin";
    final String password = "admin";   
    String connectionURL = "jdbc:mysql://" + host + ":3306/db?user=;password=";
    Connection connection = null;
    Statement statement = null;

    public Connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = (Connection) DriverManager.getConnection(connectionURL, username, password);
            statement = (Statement) connection.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ResultSet executeQuery(String query) {
            ResultSet result = null;
            try {
                    if (statement != null) {
                            result = statement.executeQuery(query);

                    } else {
                            System.out.println("Null");
                    }
        } catch (SQLException ex) {
                Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
            return result;
    }
    public boolean execute(String query) {
            System.out.println(query);
            boolean result = false;
            try {
                    if (statement != null) {
                            result = statement.execute(query);
                    } else {
                            System.out.println("Null");
                    }
        } catch (SQLException ex) {
                Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
            return result;
    }
4

2 回答 2

1

看起来你从来没有关闭连接,所以几天后连接池和/或你的 MySQL 服务器已经用完了免费连接。

您是为每个查询创建一个新的 Connect 类,还是重复使用相同的类(如果是,您将如何处理多线程?)

于 2012-04-08T15:07:12.607 回答
1
    public class Connect {
            final String host = "localhost";
            final String username = "admin";
            final String password = "admin";   
            String connectionURL = "jdbc:mysql://" + host + ":3306/db?user=;password=";
            Connection connection = null;
            Statement statement = null;

    public Connect() {
    try {
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           connection = (Connection) DriverManager.getConnection(connectionURL,      username, password);

          } catch (Exception ex) {
                    out.println("Exception is: " +ex)
                    } 
            }

            public ResultSet executeQuery(String query) {
                    ResultSet result = null;
                    try {

                            statement=(Statement)connection.createStatement();
                            result = statement.executeQuery(query);
                            if(result>0){
                                 result=true;
                               }else{
                                   result=false;
                                  } 
                         } catch (SQLException ex) {
                           Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
                         }finally{
                         if(statement!=null){try{statement.close;}catch(SQLException logOrIgnore){}}
                          if(connection!=null){try{connection.close;}catch(SQLException logOrIgnore){}}
        }
                    return result;
            }
            public boolean execute(String query) {
                    System.out.println(query);
                    boolean result = false;
                    try {

                           statement=(Statement)connection.createStatement();
                           result = statement.executeQuery(query);
                          if(result>0){
                              result=true;
                           }else{
                               result=false;
                           }
                       } catch (SQLException ex) {
                            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
                           }finally{
                             if(statement!=null){try{statement.close;}catch(SQLException logOrIgnore){}}
                            if(connection!=null){try{connection.close;}catch(SQLException logOrIgnore){}}
        }
                         return result;
      }

     }
于 2012-04-08T20:29:04.517 回答