[英]Which Place to write connection.close() and preparedstatement.close()
我是JDBC的新手...
学生班上有构造函数,add(),update()和delete()等方法...
在构造函数中打开一个连接。 在下面的代码中哪个地方可以写conn.close()和pstmt.close()
class Student
{
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
public Student()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
}
catch(Exception e)
{
System.out.println("Error :"+e.getMessage());
}
}
public void add(int rollno,String name)
{
try
{
pstmt = conn.prepareStatement("insert into student values (?, ?)");
pstmt.setInt(1,rollno);
pstmt.setString(2, name);
int i = pstmt.executeUpdate();
if (i != 0) {
System.out.println("Record Inserted");
} else {
System.out.println("Record Not Inserted");
}
}
catch(Exception e)
{
System.out.println("Error :"+e.getMessage());
}
}
public void update(int rollno,String name)
{
try
{
pstmt = conn.prepareStatement("update student set name=? where rollno=?");
pstmt.setString(1, name);
pstmt.setInt(2,rollno);
int i = pstmt.executeUpdate();
if (i != 0) {
System.out.println("Record Updated");
} else {
System.out.println("Record Not Updated");
}
}
catch(Exception e)
{
System.out.println("Error :"+e.getMessage());
}
}
public void delete(int rollno)
{
try
{
pstmt = conn.prepareStatement("delete from student where rollno=?");
pstmt.setInt(1,rollno);
int i = pstmt.executeUpdate();
if (i != 0) {
System.out.println("Record Deleted");
} else {
System.out.println("Record Not Deleted");
}
}
catch(Exception e)
{
System.out.println("Error :"+e.getMessage());
}
}
}
根据您发布的示例代码,似乎您尝试执行的所有操作(如Add
Update
和Delete
均使用全局创建并在构造函数“ Student()”中初始化的同一连接对象。 This is not a standard practice
按照标准,您不应该这样做,您应该分别在每个操作(例如“添加”,“更新”和“删除”)上创建新连接作为local
变量
另外请记住,如果您正在使用“ Bean管理的事务”,则需要在关闭事务之前提交该事务。否则添加conn.commit();
在最后一块
这是一个有关如何使用方法的简单示例
public void add(int rollno,String name)
{
Connection conn = null;
PreparedStatement pstmt = null;
try
{
pstmt = conn.prepareStatement("insert into student values (?, ?)");
pstmt.setInt(1,rollno);
pstmt.setString(2, name);
int i = pstmt.executeUpdate();
if (i != 0) {
System.out.println("Record Inserted");
} else {
System.out.println("Record Not Inserted");
}
} catch(Exception e) {
System.out.println("Error :"+e.getMessage());
} finally {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.comit(); // Add this line ONLY if you use bean managed transaction
conn.close();
}
}
}
我猜您应该添加另一种方法来关闭连接,并在完成操作后在对象上调用它。
public void closeConnection() {
conn.close();
}
创建另一个方法来打开连接,而不是从构造函数中打开它也是一个好主意。
首先,我不会在构造函数中打开连接,因为它会导致许多连接长时间不使用而打开,并且由于连接是一种有限的资源,因此您应该将连接保持打开的时间最少为可能。
我会在使用连接的每个方法( add
, update
, delete
等)中打开和关闭连接(以及准备好的语句)。 这样,仅在需要时才打开连接。
您可以使用try-catch-finally块并在finally中关闭连接。
Connection con;
try
{
con = new Connection(...);
}
catch (Exception e)
{
//Error Handling
}
finally
{
connection.close();
}
或者,您使用try-with-resource并让VM负责关闭连接(在这种情况下,您不需要将Connection声明为字段,但每个操作都有一个):
try(Connection con = new Connection(...))
{
}
catch()
{
}
在catch
块之后添加一个finally
块,以关闭PreparedStatement
和Connection
对象。 就像是
catch (Exception d) {
//do whatever you want when exception occurs
}
finally {
//close resultset
//close prepared statement
//close connection object
}
finally
确保在控件返回之前关闭资源。 即使您在try
块内使用return
资源也会被关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.