编写代码关闭数据库连接资源,Connection、Statement、ResultSet
1条回答 默认 最新
关注示例代码如下。
String url = "jdbc:mysql://127.0.0.1:3306/test"; String username = "root"; String password = "12345678"; // 1. 获取连接 Connection connection = DriverManager.getConnection(url, username, password); // 2. 获取查询语句 PreparedStatement preparedStatement = connection.prepareStatement("select * from test"); // 3. 执行查询,获取结果集 ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ // 4. 从结果集取数据 int id = resultSet.getInt("id"); System.out.println(id); } // 5. 关闭连接 resultSet.close(); preparedStatement.close(); connection.close();本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用