- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么我得到一个 java.sql.PreparedStatement
从打开的 MySQL 连接关闭?
这是我的代码:
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class MySqlTest1
{
Connection connection = null;
PreparedStatement stmt = null;
public MySqlTest1()
{
System.out.println("Loading driver...");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}
String url = "jdbc:mysql://localhost:3306/world?autoReconnect=true&useSSL=false";
String username = "jee";
String password = "????????";
System.out.println("Connecting database...");
try (Connection connection = DriverManager.getConnection(url, username, password))
{
if (connection.isClosed())
{
System.out.println("Returned connection is closed");
return;
}
System.out.println("Database connected!");
System.out.println("create statement ...");
**stmt = connection.prepareStatement("select * from city");**
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
System.out.println("Selecting data ...");
ResultSet rs = null;
try {
System.out.println("execute query ...");
rs = stmt.executeQuery();
if (rs != null)
{
System.out.println("Data selected");
}
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("VendorError: " + ex.getErrorCode());
return;
}
该代码的结果是
Loading driver...
Driver loaded!
Connecting database...
Database connected!
create statement ...
Selecting data ...
execute query ...
****SQLException: No operations allowed after statement closed.****
SQLState: S1009
VendorError: 0
我也尝试过使用 Statement 并查看它的值,发现“isClosed”是正确的。我查看了 MySQL 日志,但一无所获。
最佳答案
您正在 try-with-resource block 中打开连接。 block 终止后,连接将关闭,并隐式地关闭从它创建的所有语句。只需扩展此 block 以包括该语句的用法,您就可以了。
关于java - 为什么我得到一个从打开的 MySQL 连接关闭的 java.sql.PreparedStatement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41090862/
我有一个简单的问题。我的代码中有一些内存泄漏,因为我认为这是因为 PreparedStatement 和 ResultSet 我想知道释放资源及其内存的最佳、安全和正确的方法是什么。 Prepared
package jdbcDemo; import java.sql.*; public class PreparedStatement { public static void mai
服务器端和客户端上的语句缓存是通过 PreparedStatement 的字符串呈现严格缓存还是其他什么?换句话说,如果两个不同的 PreparedStatement 是通过不同的方式创建的,但最终具
我对 C++ Mysql 连接器有“一点”问题。我想使用 PreparedStatement。我尝试了文档中的示例 http://dev.mysql.com/doc/refman/5.1/en/con
我已经将 JAVA 应用程序与 MySql 连接起来。当我编写 PreparedStatement ps = null ;然后显示导入包的两个选项。两个建议的包是:com.mysql.jdbc.Pre
PreparedStatement setString 取一个“null”(就像 String a = "null"),在 .addBatch 之后它被转换成一个普通的 null(就像 String
我正在使用 PreparedStatement 的 setString 方法在 sql 查询中设置开始日期和结束日期的值。 String sql = "..... " AND tableA.time
这个问题已经有答案了: Using Prepared Statements to set Table Name (8 个回答) 已关闭 5 年前。 我正在尝试使用准备好的语句创建一个新表,但收到错误
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
我正在开发一个使用PreparedStatement的java应用程序。 SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1='firstFieldVal
我正在使用PreparedStatements写入数据库,有时我会在日志中看到以下内容: 2015-02-02 15:44:14,601 WARN SQL Warning Code: 1292,
query = "SELECT * FROM POST_COMMENT WHERE Post_date_time= ? AND Post_User= !;"; query = query.r
有没有一种方法可以在 php 准备好的语句中设置字段名称,而不是仅仅设置那里的值,例如, $stmt = $mysqli->prepare("UPDATE movies SET filmName =
我对“同时使用 Java 和 SQL”这整个事情还很陌生,我似乎找不到解决这个问题的方法。我通过 Workbench 在服务器上创建了一个数据库,现在我正在使用它的 Netbeans 创建一个简单的应
我对这样的准备好的语句有疑问: select ... from ... where xy = ? and foo = ? and bla = ? 我可以为每个索引设置一个字符串吗?否则我不得不做 pr
我有一个多线程代码的工作版本,但是我对我的 PreparedStatement-wrapper 类是非线程安全的感到不满意。所以我决定在 ThreadLocal 中生成 PreparedStateme
当使用 JDBC 的 PreparedStatements 查询 Oracle 时,请考虑: String qry1 = "SELECT col1 FROM table1 WHERE rownum=?
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
使用 SQL 的最佳解决方法是什么 IN具有 java.sql.PreparedStatement 实例的子句,由于 SQL 注入(inject)攻击安全问题,不支持多个值:1 ?占位符代表一个值,而
我是一名优秀的程序员,十分优秀!