连接关闭后结果集被清除。 SQLite
ResultSet cleared after connection close. SQLite
这是我的代码
public static void main(String[] args) throws SQLException {
Long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
ResultSet rs = selectUser(what, from, chatId);
if (rs.next()) {
System.out.println("NEVER REACH");
}
}
private static ResultSet selectUser(String what, String from, long chatId) throws SQLException {
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
ResultSet rs;
try (Connection con = DriverManager.getConnection(url);
PreparedStatement pst = con.prepareStatement(sql)) {
pst.setLong(1, chatId);
rs = pst.executeQuery();
return rs;
}
}
如您所料,IF 块始终为假。但是当这段代码变成这样时:
public static void main(String[] args) throws SQLException {
Long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
ResultSet rs;
try (Connection con = DriverManager.getConnection(url);
PreparedStatement pst = con.prepareStatement(sql)) {
pst.setLong(1, chatId);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("HELLO");
}
}
}
一切正常。并且 IF 块可以为真。
我想 ResultSet 在连接关闭时会重置。为什么会发生这种情况以及如何防止这种情况?
我想通了。
不得关闭准备好的语句以保存 ResultSet。所以这有效:
public static void main(String[] args) throws SQLException {
long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
ResultSet rs = f(what, from, chatId);
if (rs.next()) {
System.out.println("HELLO");
}
}
private static ResultSet f(String what, String from, long chatId) throws SQLException {
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
try (Connection con = DriverManager.getConnection(url)) {
PreparedStatement pst = con.prepareStatement(sql);
pst.setLong(1, chatId);
return pst.executeQuery();
}
}
这是我的代码
public static void main(String[] args) throws SQLException {
Long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
ResultSet rs = selectUser(what, from, chatId);
if (rs.next()) {
System.out.println("NEVER REACH");
}
}
private static ResultSet selectUser(String what, String from, long chatId) throws SQLException {
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
ResultSet rs;
try (Connection con = DriverManager.getConnection(url);
PreparedStatement pst = con.prepareStatement(sql)) {
pst.setLong(1, chatId);
rs = pst.executeQuery();
return rs;
}
}
如您所料,IF 块始终为假。但是当这段代码变成这样时:
public static void main(String[] args) throws SQLException {
Long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
ResultSet rs;
try (Connection con = DriverManager.getConnection(url);
PreparedStatement pst = con.prepareStatement(sql)) {
pst.setLong(1, chatId);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println("HELLO");
}
}
}
一切正常。并且 IF 块可以为真。 我想 ResultSet 在连接关闭时会重置。为什么会发生这种情况以及如何防止这种情况?
我想通了。 不得关闭准备好的语句以保存 ResultSet。所以这有效:
public static void main(String[] args) throws SQLException {
long chatId = 432878720L;
String what = "*";
String from = "BtcUser";
ResultSet rs = f(what, from, chatId);
if (rs.next()) {
System.out.println("HELLO");
}
}
private static ResultSet f(String what, String from, long chatId) throws SQLException {
String sql = "SELECT "+what+" FROM "+from+" WHERE chatId = ?;";
try (Connection con = DriverManager.getConnection(url)) {
PreparedStatement pst = con.prepareStatement(sql);
pst.setLong(1, chatId);
return pst.executeQuery();
}
}