package com.foxconn.dbmanage;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/**
* @author cucu
* @version $Id$
*/
public class Test {
private static Log log = LogFactory.getLog(Test.class);
private DbManage db = DbManage.getInstance(); public void getData() { Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
StringBuffer buf = new StringBuffer();
buf.append("select name from classes where name = ? ");
String sql = buf.toString();
try {
con = db.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, "xiaxin");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println("name is" + rs.getString(1));
} } catch (SQLException se) {
log.error(se, se);
} finally {
db.freeConnection(con, null, ps, rs); if (con != null) {
System.out.println("con is not be closed");
System.out.println("con is " + con); } else {
System.out.println("con is be closed");
}
}
}
}问题如下:我在finally里面把connection用db.freeConnection给关掉了,然后 if (con != null)打印出con is not be closed;但是紧接着下面打印出con is null???con!=null怎么又是null呢?前后矛盾啊!!!哭!!!找了好久没有找出问题关键!!劳请大侠们帮忙!!!
import org.apache.commons.logging.LogFactory;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/**
* @author cucu
* @version $Id$
*/
public class Test {
private static Log log = LogFactory.getLog(Test.class);
private DbManage db = DbManage.getInstance(); public void getData() { Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
StringBuffer buf = new StringBuffer();
buf.append("select name from classes where name = ? ");
String sql = buf.toString();
try {
con = db.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, "xiaxin");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println("name is" + rs.getString(1));
} } catch (SQLException se) {
log.error(se, se);
} finally {
db.freeConnection(con, null, ps, rs); if (con != null) {
System.out.println("con is not be closed");
System.out.println("con is " + con); } else {
System.out.println("con is be closed");
}
}
}
}问题如下:我在finally里面把connection用db.freeConnection给关掉了,然后 if (con != null)打印出con is not be closed;但是紧接着下面打印出con is null???con!=null怎么又是null呢?前后矛盾啊!!!哭!!!找了好久没有找出问题关键!!劳请大侠们帮忙!!!
解决方案 »
- 怎样保证修改tomcat访问首页后,其他应用不受影响
- 关于lucene 排序问题
- 想问一下 java用JXL API中设置excel中cell的批注属性
- 自写 taglib 怎么读取jsp中的变量
- (特急)如何在Java中使用session类控制用户通过地址栏直接访问受限制页面?
- JSP问题-------------------------------------结帖超快!!!!!!!!!!
- 大型电子商务网站怎么实现图片的管理和显示
- helloword.java怎么运行???要不要配置?
- 我用ASP写的一个网站,几经周折,终于可以访问了,请大家来看看!
- 用<form action="/myweb/servlet/HelloWorldExample" method="post">结果显示错误
- Struts中动态Form如何继承另一个动态Form
- 为什么加了个jspsmartupload 就会这样
try {
conn.close();
conn = null; //2
} catch (SQLException e) {
log.error("释放资源出错!", e);
}
}
1.判断conn是否关闭不应该判断是否为null,而是判断conn.isClosed()
2.这里的conn = null,对你Test类中的conn是没有影响的,搞清楚参数传递原则再写
另外,自己写的那些获取conn和关闭conn的方法有些画蛇添足了....statement的关闭会自动关闭关联的rs,conn关闭会关闭关联的statement
System.out.println("con is " + con.toString());
不知道JAVA它这里的toString()方法的逻辑是什么。
要们就是这样System.out.println("con is " + (con==null?"null":"not null"));
因为你在freeConnection里面置null只是把引用置为null,在Test.java里面的conn的引用仍然存在。但为什么打印出来的conn会是null?倒还真是奇怪。
请看这个例子:
public class Hello {
public static void main(String[] args){
Abc a = Abc.getInstance();
Def b = a.getDef();
a.setNull(b);
if (b != null) {
System.out.println("b is not be closed");
System.out.println("b is " + b); } else {
System.out.println("b is be closed");
} }
} class Abc{
private static Abc pInstance = new Abc();
public static Abc getInstance(){
return pInstance;
}
public Def getDef(){
return new Def();
}
public void setNull(Def a){
a = null;
}
}
class Def{
}