Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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_Database_Database Connection - Fatal编程技术网

Java 如何使用按钮关闭数据库连接

Java 如何使用按钮关闭数据库连接,java,database,database-connection,Java,Database,Database Connection,我想知道在我使用完数据库连接后,如何通过按下按钮(键绑定)来关闭它 以下是我得到的: public static void main(String[] argv) { System.out.println("-------- MySQL JDBC Connection Testing ------------"); try { Class.forName("com.mysql.jdbc.Driver"); } catch

我想知道在我使用完数据库连接后,如何通过按下按钮(键绑定)来关闭它

以下是我得到的:

public static void main(String[] argv) {

        System.out.println("-------- MySQL JDBC Connection Testing ------------");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }

        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test14", "root", "password");


        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }

        if (connection != null) {
            System.out.println("Connection to DB Success!");
        } else {
            System.out.println("Failed to make connection!");
        }
        System.out.println("\n" + "----------- Database Results ------------" + "\n");

        try {

            Statement stmt = connection.createStatement();
            String sql = "SELECT * FROM first_table";
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id_col = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");



                String p = id_col + " " + name + " " + address;
                System.out.println(p);
            }
        } catch (SQLException e) {
             e.printStackTrace();
        }


    }
}
我是否必须在查询语句之后添加close连接


非常感谢您的帮助。

实际上,这取决于您的业务,如果您刚刚使用完数据库,请关闭连接,如果没有,请保持连接打开,但关闭
语句和
结果集

对于第二个选项,您可能需要创建一个负责连接的类,并在每次使用DB时使用它。 查看帮助。

我在这里做什么


实际上,您想做什么?基本上可以选择在完成查询后关闭数据库连接。我只是在玩一些jdbc连接和java代码,看看它是如何工作的。
  call resultset.close  just after finishing your execution.
  rs.close();
   finally
      {
          try
          {
              if(stmt!=null)
                  stmt.close();
          }
          catch (Throwable th)
          {}
          catch (Throwable th)
          {}
          try
          {
              if (connection != null)
                  connection.close();
          }
          catch (Throwable th)
          {}
      }
  }