用完之后,
最好把Connection、Statement、ResultSet都关闭掉。
例如: /**
* <pre>
* 函数功能:1、关闭对象stmt,stmt1,
* 2、把连接对象con还给连接池,
* 3、在所有连接调用后,必须调用此方法。
* 备注:在事务处理中不能关闭连接,在一个jsp页面里,
* 所有连接结束后不调用此方法,那么该连接将不会
* 返回连接池,则造成的后果是连接数会大于数据库的
* 最大连接数。
* </pre>
*/
public void close()
{
try
{
if(stmt != null)
{
stmt.close();
stmt = null;
}
if(stmt1 != null)
{
stmt1.close();
stmt1 = null;
}
if(rs != null){
rs.close();
}
if(conn != null)
connMgr.freeConnection(this.poolName, conn);
}
catch(Exception ex)
{
setErr("close() error:" + ex, -1);
}
}
最好把Connection、Statement、ResultSet都关闭掉。
例如: /**
* <pre>
* 函数功能:1、关闭对象stmt,stmt1,
* 2、把连接对象con还给连接池,
* 3、在所有连接调用后,必须调用此方法。
* 备注:在事务处理中不能关闭连接,在一个jsp页面里,
* 所有连接结束后不调用此方法,那么该连接将不会
* 返回连接池,则造成的后果是连接数会大于数据库的
* 最大连接数。
* </pre>
*/
public void close()
{
try
{
if(stmt != null)
{
stmt.close();
stmt = null;
}
if(stmt1 != null)
{
stmt1.close();
stmt1 = null;
}
if(rs != null){
rs.close();
}
if(conn != null)
connMgr.freeConnection(this.poolName, conn);
}
catch(Exception ex)
{
setErr("close() error:" + ex, -1);
}
}
在tomcat的conf/server.xml中加入一下内容
<Context path="/MyWeb" docBase="C:\MyWeb" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/bn" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/bn">
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://127.0.0.1/MyWebSource</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>
关闭时必须Connection = null、连接问题如果是使用JNDI访问数据库时Connection、Statement、ResultSet都关闭掉。
中connMagr是什么类的对象阿?
建议不使用Vector
改用List来实现
但是如果不需要像递归算法一样的查询,
则最好不要用到自定义词结果集,
设想如果有上万甚至更多的记录,
因为效率问题的存在,肯定不可取。 zhugegod(诸葛) ( ) :connMgr.freeConnection(this.poolName, conn);
中connMagr是什么类的对象阿?
这个你无需用到,那是我的程序顺便拷贝上去的,和连接池有关。
但你用到数据源则不需要用到这些了。 zhugegod(诸葛) ( ) :Vector的对象不能够使用ResutSet那样getString(“cloName”)得到其中存储的内容,Java中有没有一种和ResutSet类似的数据结构阿?
呵呵,那只能自己写一个getString了
ResultSet和Statement最好也手动关闭关闭时必须Connection = null???
这个应该不必吧,如果你close()
上面的语句有什么用,只是把一个变量赋值null而已
对象本身还在
在我的机子上好像是在关闭了ResultSet之前关闭Connection就会出现异常