Datasource source=...
Connection conn=source.getConnection();
try{
Statement stat=conn.createStatement();
String command="insert into User values("root","123456");
stat.executeUpdate(command);
}
finally{
conn.close();
}
这个简单的规则完全可以解决连接泄漏的问题,但它没有处理异常,如果象下面那样:
Datasource source=...
Connection conn=null;
try{
conn=source.getConnection();
Statement stat=conn.createStatement();
String command="insert into User values("root","123456");
stat.executeUpdate(command);
}
catch(SQLExceptin e){
//日志记录
}
finally{
conn.close();
}
代码中又有两个小错误。首先,如果调用getConnection会抛出一个异常,而conn仍然是null,这时将不能调用close()。此外,调用close()也会抛出SQLException。正确的代码应该为:
Datasource source=...
try{
Connection conn=source.getConnection();
try{
Statement stat=conn.createStatement();
String command="insert into User values("root","123456");
stat.executeUpdate(command);
}
finally{
conn.close();
}
}
catch(SQLException e){
//日志记录
}
内部的try语句块确保连接被关闭,外部的try语句块确保异常被记录。
当然,还可以使用throws SQLException来标注方法并将外部try语句块留给调用者处理。如:
boolean loggedIN;
public void doLogin() throws SQLException, NamingException {
Context ctx = new InitialContext();
if (ctx == null) throw new NamingException("No initial context");
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
if (ds == null) throw new NamingException("No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException("No connection");
try {
PreparedStatement passwordQuery = conn.prepareStatement(
"SELECT password from Users WHERE username = ?");
passwordQuery.setString(1, name);
ResultSet result = passwordQuery.executeQuery();
if (!result.next()) return;
String storedPassword = result.getString("password");
loggedIn = password.equals(storedPassword.trim());
}
finally {
conn.close();
}
}
这是调用这个方法的代码:
public String login() {
try {
doLogin();
}
catch (SQLException ex) {
logger.log(Level.SEVERE, "loginAction", ex);
return "internalError";
}
catch (NamingException ex) {
logger.log(Level.SEVERE, "loginAction", ex);
return "internalError";
}
if (loggedIn)
return "loginSuccess";
else
return "loginFailure";
}
↑返回目录
前一篇: 使用JdbcRowSet和CachedRowSet的例子
后一篇: 通过JDBC、Hibernate API访问数据库图示