我的DAO对象
java.library.path
我想关闭连接。我有几个servlet调用package com.myselect;
import java.sql.*;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataAccess {
Connection conn;
PreparedStatement pst;
ResultSet rs;
public DataAccess() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/db", "root", "root");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
}
public String getAge(String name) {
String userage = null;
try {
pst = conn.prepareStatement("select age from mydb where name= ?");
pst.setString(1, name);
rs = pst.executeQuery();
while (rs.next()) {
userage = rs.getString("age");
System.out.println(userage);
}
} catch (Exception ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
return userage;
}
public int insertRecord(String name, int addage) {
int b = 0;
try {
pst = conn.prepareStatement("insert into mydb values(?,?)");
pst.setString(1, name);
pst.setInt(2, addage);
b = pst.executeUpdate();
} catch (Exception ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
return b;
}
}
和insertRecord
方法。什么是关闭连接的最佳方法?创建另一个方法并从getAge
和insertRecord
方法或构造函数调用?
答案 0 :(得分:2)
应在需要时打开和关闭连接。在您的情况下,您在一个点上打开一个连接,然后在另一个点上使用它。这意味着您打开连接并且可能暂时不使用它。
您需要做的是使方法自包含,这意味着方法应该打开连接,完成后关闭它。结束部分应该在finally
块内完成,这将确保无论发生什么,集合将始终关闭。
像这样:
public String getAge(String name) {
String userage = null;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/db", "root", "root");
PreparedStatement pst = conn.prepareStatement("select age from mydb where name= ?");
pst.setString(1, name);
rs = pst.executeQuery();
while (rs.next()) {
userage = rs.getString("age");
System.out.println(userage);
}
} catch (Exception ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
if(conn != null) {
conn.close();
}
if(pst != null) {
pst.close();
}
if(rs != null) {
rs.close();
}
}
return userage;
}
编辑:根据@virtualpathum建议,您还可以查看第三方框架,它们为您提供管理连接池的方法,通常是通过实施Singleton编程模式。
答案 1 :(得分:2)
在考虑连接之前,每次使用时,总是关闭class BannedIP
attr_accessor :ip, :time, :reason, :status
def initialize(ip)
@ip = ip
@time = Time.new
@reason = reason
@status = status.nil? ? false : true
end
def ban(reason)
@reason = reason
@time = Time.new
@status = true
end
def unban
@reason = ""
@time = Time.new
@status = false
end
def banned?
return @status
end
end
if __FILE__ == $0
puts "ban the bad guy at 10.0.0.1 because he was cheating"
a = BannedIP.new("10.0.0.1")
a.ban "he is a cheater!!"
puts "The status of 10.0.0.1 is " + a.status.to_s
end
$ ruby stack1.rb
ban the bad guy at 10.0.0.1 because he was cheating
The status of 10.0.0.1 is true
和ResultSet
个对象。不要将它们作为状态保存在Statement
对象中。将它们作为局部变量并在使用后关闭它们:
DataAccess
连接相同:理想情况下,每个方法都应创建一个public String getAge(String name) {
PreparedStatement pst = null;
ResultSet rs = null;
String userage = null;
try {
pst = conn.prepareStatement("select age from mydb where name= ?");
pst.setString(1, name);
rs = pst.executeQuery();
while (rs.next()) {
userage = rs.getString("age");
System.out.println(userage);
}
} catch (Exception ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// closing JDBC resources
if(rs != null) {
rs.close();
}
if(pst != null) {
pst.close();
}
return userage;
}
对象,并在使用后将其关闭。根据您在方法中打开和关闭连接的频率,您可能希望使用连接池来共享打开的连接以提高效率。
要关闭连接及其资源,您可以创建一个方法来执行此操作:
Connection