JDBC 试试资源
JDBC with try with resources
我正在尝试创建一个集中式 class 连接 returns SQL 查询的 ResultSet
,这样我就不必总是创建一个每次我尝试获取查询时都会建立新连接。
我正在使用 try-with-resources
,但是,每当我使用 try-with-resources
时都会遇到编译时错误,我不知道为什么?
public class JDBC {
// logger declaration is omitted
private static final String dbURL = "jdbc:oracle:";
private static final String userName = "blah";
private static final String password = "12345";
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try (conn = DriverManager.getConnection(dbUrl, user, password);
statement = conn.createStatement();
rs = statement.executeQuery(sqlQuery)) {
} catch (SQLException e) {
logger.info(e.getMessage());
}
return rs;
}
}
你应该这样使用它:
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(dbUrl, user, password);
statement = conn.createStatement();
rs = statement.executeQuery(sqlQuery);
} catch (SQLException e) {
logger.info(e.getMessage());
}
return rs;
}
它不起作用,因为您将代码放在 brackets.You 中应该将它放在这些括号内 -> {}。这也是显示错误的原因,因为没有 class 那里有一个方法如下:
try(bla bla bla) {}
Java 7
当您使用 try-with-resources 时,指向 Closeable
资源 的变量必须在 try-with-resources
块.
内声明
此外,returning rs
是一个坏主意,方法完成后将关闭。所以你可能会在你的方法之外得到一个 SQLException
(类似于 "ResultSet is closed")。您应该在 try-with-resources
块中解析 rs
和 return SQL 不可知对象从您的方法:
public ResultSet retrieveSQLQuery(String sqlQuery) {
try (Connection conn = DriverManager.getConnection(dbUrl, user, password);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sqlQuery)) {
MyResult result = ...; // parse rs here
return myResult;
} catch (SQLException e) {
logger.info(e.getMessage());
// return something (empty MyResult or null) from here or rethrow the exception
// I'd recommend to get rid of this catch block and declare the SQLException on method signature
}
}
你在不正确的 try-with-resources
语法上遇到编译时错误,就是这样。
更新
Java 9
Java 9 为try-with-resources
提供了更灵活的语法。您可以在 try (...)
块之外声明 Closeable
资源:
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = DriverManager.getConnection(dbUrl, user, password);
try (conn; ResultSet rs = conn.createStatement().executeQuery(sqlQuery)) {
MyResult result = ...; // parse rs here
return myResult;
} catch (SQLException e) {
// handle error
}
}
我正在尝试创建一个集中式 class 连接 returns SQL 查询的 ResultSet
,这样我就不必总是创建一个每次我尝试获取查询时都会建立新连接。
我正在使用 try-with-resources
,但是,每当我使用 try-with-resources
时都会遇到编译时错误,我不知道为什么?
public class JDBC {
// logger declaration is omitted
private static final String dbURL = "jdbc:oracle:";
private static final String userName = "blah";
private static final String password = "12345";
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try (conn = DriverManager.getConnection(dbUrl, user, password);
statement = conn.createStatement();
rs = statement.executeQuery(sqlQuery)) {
} catch (SQLException e) {
logger.info(e.getMessage());
}
return rs;
}
}
你应该这样使用它:
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(dbUrl, user, password);
statement = conn.createStatement();
rs = statement.executeQuery(sqlQuery);
} catch (SQLException e) {
logger.info(e.getMessage());
}
return rs;
}
它不起作用,因为您将代码放在 brackets.You 中应该将它放在这些括号内 -> {}。这也是显示错误的原因,因为没有 class 那里有一个方法如下:
try(bla bla bla) {}
Java 7
当您使用 try-with-resources 时,指向 Closeable
资源 的变量必须在 try-with-resources
块.
此外,returning rs
是一个坏主意,方法完成后将关闭。所以你可能会在你的方法之外得到一个 SQLException
(类似于 "ResultSet is closed")。您应该在 try-with-resources
块中解析 rs
和 return SQL 不可知对象从您的方法:
public ResultSet retrieveSQLQuery(String sqlQuery) {
try (Connection conn = DriverManager.getConnection(dbUrl, user, password);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sqlQuery)) {
MyResult result = ...; // parse rs here
return myResult;
} catch (SQLException e) {
logger.info(e.getMessage());
// return something (empty MyResult or null) from here or rethrow the exception
// I'd recommend to get rid of this catch block and declare the SQLException on method signature
}
}
你在不正确的 try-with-resources
语法上遇到编译时错误,就是这样。
更新
Java 9
Java 9 为try-with-resources
提供了更灵活的语法。您可以在 try (...)
块之外声明 Closeable
资源:
public ResultSet retrieveSQLQuery(String sqlQuery) {
Connection conn = DriverManager.getConnection(dbUrl, user, password);
try (conn; ResultSet rs = conn.createStatement().executeQuery(sqlQuery)) {
MyResult result = ...; // parse rs here
return myResult;
} catch (SQLException e) {
// handle error
}
}