Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使在低于代码的情况下关闭连接,也会关闭连接_Java_Mysql_Database Connection - Fatal编程技术网

Java 即使在低于代码的情况下关闭连接,也会关闭连接

Java 即使在低于代码的情况下关闭连接,也会关闭连接,java,mysql,database-connection,Java,Mysql,Database Connection,下面我有一个基本的代码片段,但它不工作。它可能有什么问题 public List<String> getStores() throws SQLException{ List<String> store_id=new ArrayList<>(); String query="select distinct(store_id) from stores"; Connection con=ConnectDatabase.

下面我有一个基本的代码片段,但它不工作。它可能有什么问题

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;
    }
ConnectDatabase的代码是

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;
     }

我无法理解相同的原因。可能是什么问题。因为我在完成连接后正在关闭连接。

在我将其封闭在
try catch finally
块中后,它工作正常。更改了下面给出的代码

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;
    }
public List getStores()引发SQLException{
List store_id=new ArrayList();
Connection con=ConnectDatabase.getDb2ConObj();
试一试{
String query=“从存储中选择不同的(存储\u id)”;
语句stmt=con.createStatement();
java.sql.ResultSet rsResultSet=stmt.executeQuery(查询);
while(rsResultSet.next()){
store_id.add(rsResultSet.getString(1));
}
}捕获(例外e){
}最后{
con.close();
}
退货仓库标识;
}
谢谢。

使用

根据,您可以将try with resources块与常规try块组合

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

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

示例代码:

try(Connection con = getConnection()) {
   ...
}
阅读更多


  • 关闭
    语句
    结果集

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

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
  • 我建议您在Java代码之外使用并保留用户名和密码,以使其更易于管理。将数据库配置保存在单独的xml/属性文件中,而不是在Java文件中进行硬编码

    请参阅有关的Java教程

    我已经发布了一个很好的类来管理整个应用程序中单个类中的所有连接


    • 您可以使用这种类型的代码…来解决您的问题

           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){}
            }}
      
      如果it引发异常,则检查系统32位或64位。您应该尝试使用64位,然后请将dsn设置为32位,并使用ms access 2002-2003版本

      然后您将获得tour解决方案…..谢谢您获得了对象,但没有打开连接?什么是
      ConnectDatabase
      ?您可能正在
      ConnectDatabase.getDb2ConObj()
      中缓存连接,然后在下次调用此方法时返回相同的连接?您最好使用连接池,而不是尝试维护自己的连接池。请发布
      ConnectDatabase
      的代码,我们可以对此进行确认。您好,先编辑问题,这不是处理错误的好方法。第二,你现在真的得到数据了吗?嗨,巴特。是的,我在对象中定义了语句
      Class.forName(“com.mysql.jdbc.Driver”);con=DriverManager.getConnection(connectionString、用户名、密码)我更喜欢你的答案,而不是我的答案。在Java 6中,关闭资源是一项更为繁琐的工作,但这在Java 7中处理。在Java 7中,你不需要关闭资源,它会自动关闭。
      
           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){}
            }}