我是在bean中建立connection,ResultSet,Statement
这些使用正常,但怎么关闭他们?
如果长期不关闭,会不会有比较严重的后果public class Conn {
public Connection Connect()
{
try{
//String strSql=new String();
Connection con;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl="jdbc:mysql://localhost:3306/bdgq";
String dbUser="root";
String dbPwd="";
con=java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
return con;
}
catch(Exception fe){
}
return null;
}
public ResultSet rs(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return null;
}
else
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSql);
return rs;
}
}
catch(Exception fe)
{}
return null;
}
//
public boolean executes(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return false;
}
else
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(strSql);
return true;
}
}
catch(Exception fe)
{}
return false;
}
}
这些使用正常,但怎么关闭他们?
如果长期不关闭,会不会有比较严重的后果public class Conn {
public Connection Connect()
{
try{
//String strSql=new String();
Connection con;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl="jdbc:mysql://localhost:3306/bdgq";
String dbUser="root";
String dbPwd="";
con=java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
return con;
}
catch(Exception fe){
}
return null;
}
public ResultSet rs(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return null;
}
else
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSql);
return rs;
}
}
catch(Exception fe)
{}
return null;
}
//
public boolean executes(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return false;
}
else
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(strSql);
return true;
}
}
catch(Exception fe)
{}
return false;
}
}
解决方案 »
- linux环境下启动jboss5.1报错(JDK是1.6),求解决办法!!3Q
- <s:if>标签的问题
- 实现一个主键id的自动增长
- 数据类型转换的问题?
- 求助:jfreechart生成图片显示两个?
- <srcipt>与<srcipt language=javasrcipt>的区别
- 求一个操作数据库的进度条
- java.sql.SQLException: No suitable driver高手指教!!!
- 有关各种jsp\j2ee服务器及软件的疑惑?
- 在 jsp 中,取 cookie 的值只能一次 request.getCookies 全取出来,是不是?
- jsp页面如何显示图片?
- 页面设计的问题??高手帮忙
正在学习ing ,关注中ing,
如:
public void release(Connection conn){
try {
if(!conn.isClosed())
conn.close();
}catch (SQLException ex) {
ex.printStackTrace();
}
}
如:
public void release(Connection conn){
try {
if(!conn.isClosed())
conn.close();
}catch (SQLException ex) {
ex.printStackTrace();
}
}
每次用完連接就調用關閉數據庫連接的函數就行了
---------------------------------------------------
问题是调用关闭链接的函数时怎么传参数?
我的操作数据库的方法是这样的:Conn cons=new Conn();String strSql="";
try
{
ResultSet rs=cons.rs(strSql);
if(rs.next())
{
//读取成功
}
else
{
//读取不成功
}
}
catch(Exception e)
{
//错误
}
------------------------------------------------------public class Conn {public Connection Connect()
{
try{
//String strSql=new String();
Connection con;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl="jdbc:mysql://localhost:3306/bdgq";
String dbUser="root";
String dbPwd="";
con=java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
return con;
}
catch(Exception fe){
}
return null;
}-------------------------------------------------------------------
public ResultSet rs(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return null;
}
else
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSql);
return rs;
}
}
catch(Exception fe)
{}
return null;
}
//
------------------------------------------------------------------------------
public boolean executes(String strSql)
{
try
{
Connection conn=Connect();
if(conn==null)
{
return false;
}
else
{
Statement stmt=conn.createStatement();
stmt.executeUpdate(strSql);
return true;
}
}
catch(Exception fe)
{}
return false;
}
}我的操作数据库的方法是这样的:Conn cons=new Conn();String strSql="";
try
{
ResultSet rs=cons.rs(strSql);
if(rs.next())
{
//读取成功
}
else
{
//读取不成功
}
}
catch(Exception e)
{
//错误
}///////////////////////////////////////////////////////////请问,关闭链接ResultSet,Statement,Connection该怎么做?
finally{
rs.close();
stmt.close();
cons.close();
}
try
{
ResultSet rs=cons.rs(strSql);
if(rs.next())
{
//读取成功
}
else
{
//读取不成功
}
}
catch(Exception e)
{
//错误
}finally{
stmt.close();
//調用關閉連接的函數 release(Connection conn) ,把連接的對象作為參數傳遞進去
release(cons);
}
servlet中的cons并不是数据库链接,而是写历数据库链接的类的引用