[英]mysql db connection with java
我正在为MySQL使用4型驱动程序。 代码如下。 在每个Java文件中,我都会创建数据库连接并最后关闭。 例如
在abc.java中
Dbconnection db=null;
Connection con=null;
PreparedStatement pstmt = null;
public ActionForward execute(----)
{
try{
db=new Dbconnection();//instantiating user defined Dbconnection class object
con=db.getConnection();//creating connection object
...........
Login_Check formBean=(Login_Check)form;//bean class object
pstmt=con.prepareStatement("select type from user_registration where user_name=? and password=? and user_status=?");
//form parameter values
pstmt.setString(1,formBean.getUname().trim());
pstmt.setString(2,formBean.getPassword().trim());
pstmt.setString(3,"Active");//user status should be active
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
................
db.releasePreparedStatement(pstmt);
db.releaseConnection(con);
return mapping.findForward(SUCCESS);//redirecting to success page
}
else
{
ActionErrors errors = new ActionErrors();
errors.add("both", new ActionMessage("errors.both.wrong"));//if both user name and password is incorrect, gives an error message
saveErrors(request,errors);
//closing connection and prepareStatement objects
db.releasePreparedStatement(pstmt);
db.releaseConnection(con);
return mapping.findForward(FAILURE);//redirecting to failure page
}
}
catch(Exception e)
{
e.printStackTrace();
}
return mapping.findForward(FAILURE);//redirecting to failure page
}
像在每个Java文件中一样,我也遵循相同的方式。
在Dbconnection.java文件中
public class Dbconnection
{
Connection con=null;
String DB_URL = "jdbc:mysql://localhost:3306/dbname";
String USER = "abc";//db user name
String PASS = "abc";//db password
PreparedStatement pstmt = null;
public synchronized Connection getConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");//loading mysql driver
con = DriverManager.getConnection(DB_URL,USER,PASS);//connecting to mysql
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
public void releaseConnection(Connection conn)//releasing Connection
{
if(conn!=null)
{
try
{
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void releasePreparedStatement(PreparedStatement stmt)//closing PreparedStatement object
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
但是问题是有时我会收到成功消息。 但是有时候我会收到一条失败消息。 在服务器中我遇到错误
The operation is not allowed after ResultSet is closed
仅当多个用户正在访问同一文件(例如abc.java)时,才会发生上述问题。
你应该做:
1)在finally
块中关闭PreparedStatement和Connection,因此,如果代码出现exception
您的代码将正确关闭资源,否则可能会发生内存泄漏。
2)如果在您的代码中使用类似ResultSet
ResultSet rs = pstm.executeQuery();
......
然后,您应该先关闭它,然后再重新使用它...
3)您在abc.java
的方法是静态的吗?
我将执行以下操作,在finally
块中移动close
方法,以防止在发生异常情况下发生内存泄漏
public ActionForward execute(----) {
Dbconnection db=null;
Connection con=null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
db=new Dbconnection();//instantiating user defined Dbconnection class object
con=db.getConnection();//creating connection object
// some code
Login_Check formBean = (Login_Check) form;//bean class object
pstmt = con.prepareStatement("select type from user_registration where user_name=? and password=? and user_status=?");
//form parameter values
pstmt.setString(1, formBean.getUname().trim());
pstmt.setString(2, formBean.getPassword().trim());
pstmt.setString(3, "Active"); //user status should be active
rs = pstmt.executeQuery();
if(rs.next())
{
/* some code */
return mapping.findForward(SUCCESS);//redirecting to success page
}
else
{
ActionErrors errors = new ActionErrors();
errors.add("both", new ActionMessage("errors.both.wrong"));//if both user name and password is incorrect, gives an error message
saveErrors(request,errors);
return mapping.findForward(FAILURE);//redirecting to failure page
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
db.releaseResultSet(rs);
db.releasePreparedStatement(pstmt);
db.releaseConnection(con);
}
return mapping.findForward(FAILURE);//redirecting to failure page
}
您显然需要在Dbconnection中添加新的releaseResultSet
方法以释放该设置...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.