首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使在代码下面关闭了连接,也会关闭连接

即使在代码下面关闭了连接,也会关闭连接
EN

Stack Overflow用户
提问于 2014-07-20 22:28:13
回答 3查看 728关注 0票数 0

我有一个基本的代码片段下面,但它不是working.What可能是它的问题。

代码语言:javascript
复制
public List<String> getStores() throws SQLException{
        List<String> store_id=new ArrayList<>();
        String query="select distinct(store_id) from stores";
        Connection con=ConnectDatabase.getDb2ConObj();
        Statement stmt=con.createStatement();
        java.sql.ResultSet rsResultSet=stmt.executeQuery(query);
        while(rsResultSet.next()){
            store_id.add(rsResultSet.getString(1));
        }
        con.close();
        return store_id;
    }

它抛出了下面的异常

代码语言:javascript
复制
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
    at com.mysql.jdbc.Connection.checkClosed(Connection.java:1931)
    at com.mysql.jdbc.Connection.createStatement(Connection.java:3087)
    at com.mysql.jdbc.Connection.createStatement(Connection.java:3069)
    at com.dao.StoreDao.getStores(StoreDao.java:52)
    at org.apache.jsp.adminViewAllStore_jsp._jspService(adminViewAllStore_jsp.java:119)

ConnectDatabse的代码是

代码语言:javascript
复制
public class ConnectDatabase {
     static Connection con=null;
     static String connectionString="jdbc:mysql://localhost:3306/ayurveda";
     static String username="root";
     static String password="";
     public static Connection getDb2ConObj(){
         if(con==null){
         try{
             Class.forName("com.mysql.jdbc.Driver");
             con=DriverManager.getConnection(connectionString,username,password);
         }
         catch(ClassNotFoundException | SQLException e)
         {
             System.out.println("Connect initialized with error"+e.getMessage());
             e.printStackTrace();
         }
         }
         return con;
     }

我不能理解same.What的原因可能是在我完成连接后关闭连接的problem.Since。

EN

回答 3

Stack Overflow用户

发布于 2014-07-20 22:31:41

在我将它封装在try catch finally block.Changed中之后,它工作了,代码如下所示

代码语言:javascript
复制
public List<String> getStores() throws SQLException{
        List<String> store_id=new ArrayList<>();
        Connection con=ConnectDatabase.getDb2ConObj();
        try{
        String query="select distinct(store_id) from stores";
        Statement stmt=con.createStatement();
        java.sql.ResultSet rsResultSet=stmt.executeQuery(query);
        while(rsResultSet.next()){
            store_id.add(rsResultSet.getString(1));
        }
        }catch(Exception e){

        }finally{
            con.close();
        }
        return store_id;
    }

谢谢。

票数 0
EN

Stack Overflow用户

发布于 2014-07-20 22:35:03

使用Java 7 -The try-with-resources Statement

根据oracle documentation,您可以将try- with -resources块与常规try块组合在一起

典型的Java应用程序操作几种类型的资源,如文件、流、套接字和数据库连接。这样的资源必须非常小心地处理,因为它们获取用于其操作的系统资源。因此,即使在出现错误的情况下,您也需要来确保它们被释放。

实际上,错误的资源管理是生产应用程序中常见的故障来源,通常的陷阱是数据库连接和文件描述符在代码中的其他位置发生异常后仍保持打开状态。这导致应用程序服务器在资源耗尽时频繁重新启动,因为操作系统和服务器应用程序通常具有资源的上限。

示例代码:

代码语言:javascript
复制
try(Connection con = getConnection()) {
   ...
}

阅读更多Java 7 Automatic Resource Management JDBC

  • 同时关闭StatementResultSet
  • 不会在每次需要连接时都加载驱动程序类。只需在静态初始化块中加载一次即可。

static { try { Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) { e.printStackTrace();}}

  • 我建议您使用JNDIDataSource将用户名和密码保留在Java代码之外,以使其更易于管理。将数据库配置保存在单独的xml/properties文件中,而不是文件中的硬编码。

请参阅Connecting with DataSource Objects上的Java教程

我已经发布了一个很好的ConnectionUtil类来管理整个application.的单个类中的所有连接

票数 0
EN

Stack Overflow用户

发布于 2014-07-20 23:12:43

您可以使用这种类型的code...for解决您的问题

代码语言:javascript
复制
     public void actionPerformed(ActionEvent ae)
     {
     String u=t1.getText();
     String p=t2.getText();
    if(ae.getSource()==b1)
     {
      try{
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection con=DriverManager.getConnection("jdbc:odbc:newdsn");

      String stp="SELECT * FROM reg";
      Statement sa=con.createStatement();
      rs=sa.executeQuery(stp);
     while(rs.next())
      {
       String du=rs.getString(2);
       String dp=rs.getString(3);
       if(du.equals(u)&&dp.equals(p))
      {
       a=0;
     break;
     }else{ a=1;}
       }
     if(a==0){
     JOptionPane.showMessageDialog(this,"LOGIN PAGE","Login is successful",1);
       }
      if(a==1){

      JOptionPane.showMessageDialog(this,"LOGIN PAGE","Login is not successful",1);

      }}    
      catch(Exception e){}
      }}

如果甚至抛出异常,那么您应该检查系统的32位或64位bit..you应尝试如果64位,那么请使您的dsn在32位,并使用ms access 2002-2003版本

然后你就可以得到tour solution .....thank了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24851444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档