mysql数据库与java的连接
我正在使用mysql的Type4驱动程序。代码如下所示。在每个java文件中,我创建db连接并在最后关闭。比如说 在abc.java中mysql数据库与java的连接,java,mysql,Java,Mysql,我正在使用mysql的Type4驱动程序。代码如下所示。在每个java文件中,我创建db连接并在最后关闭。比如说 在abc.java中 Dbconnection db=null; Connection con=null; PreparedStatement pstmt = null; public ActionForward execute(----) { try{ db=new Dbconnection();//instantiating user defined Dbc
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,这样,如果代码出现异常
您的代码将正确关闭资源,否则可能会发生内存泄漏
2) 如果在代码中使用结果集
类似
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
方法来释放集合 尝试在连接之前关闭preparedStatement
,您还应该包含更多代码,因为异常中引用的结果集不会出现在您提供的代码中…更多代码意味着什么?这是创建和关闭连接的正确方法吗。hi@Marcx,我更新了一些代码。你能告诉我正确的书写方式吗?如果条件为真,它将重定向到成功页面。此代码是否将关闭db连接。是,它将关闭!!!始终执行finally块,如果try/catch
中有return
,则必须关闭语句obj。我应该写一行来关闭连接而不关闭语句吗。如db.releaseConnection(con);您需要关闭所有已打开的内容,否则可能会发生内存泄漏。。。因此,您需要关闭ResultSet
PreparedStatement
,然后关闭Connection
。。。