欢迎参观:http://netfish.aboutme.com/myclass3.htm我来帮你顶,UP!UP!
解决方案 »
- Tomcat裸奔的问题
- 购物车程序中如何实现在添加商品时仍停留在商品展示页面?
- 页面数据老不正确
- apache + tomcat 的一个疑问,求达人解答!
- structs项目中JBOSS启动问题,来者给分,不够再加
- 高分求助数据处理问题-mysql表怎么在页面上显示并修改
- j2ee价格
- 在线等待!急,请问jbuilder里怎么自动生成get和set方法亚?
- jsp里以下代码报错,在线等待,有分
- 关于RestTemplate中进行jsonp的ajax请求的问题
- 在weblogic布署war文件后,展开的文件在哪里?
- 我的页面放在那里一段时间后再去操作,出现Communication link failure: java.net.SocketException
conn.close();
PreparedStatement psm = null;
ResultSet rs = null;
try {
...
} catch (SQLException sqlx) {
...
} finally {
try {
if (rs != null)
rs.close();
if (psm != null)
psm.close();
if (conn != null)
conn.close();
} catch (Exception ex) {
System.out.println("can't close database connection : " + ex.toString());
}
}
import java.sql.*;public class conn {
private static String userName="";
private static String password="";
private static String url="jdbc:mysql://localhost/trmg3?useUnicode=true&characterEncoding=gb2312";
private Connection conn=null;
private Statement stmt = null;
private ResultSet rs=null; public conn()
{
try
{
netgate.DbConnPool.SetJDBC(url,userName,password);
netgate.DbConnPool.setDriverName("com.mysql.jdbc.Driver");
netgate.DbConnPool.setMinCount(10);
conn = netgate.DbConnPool.getConnection();
}catch(SQLException ex)
{
ex.printStackTrace(System.out);
System.err.println("Can't connect to DataBase Use the Url:"+url+"\n"
+" Or UserName can't perform password!"); }
} public boolean executeInsert(String sql){ //写入数据库
try{
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
return true;
}
catch (SQLException ex){
System.err.println("Update DataBase Error:"+ex.getMessage());
return false;
}
} public ResultSet executeQuery (String sql) throws SQLException{ //查询数据库
if(rs != null)
rs.close();
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
return rs; }
catch(SQLException ex){
System.err.println("Query DataBase Error:"+ex.getMessage());
return null;
}
} public static String UnicodeToChinese(String s){
try{
if(s==null || s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
} public static String ChineseToUnicode(String s){
try{
if(s==null || s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
} public void destory() throws Exception{
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
}
}
public void destory() throws Exception{
try {
if(rs != null)
{
rs.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}try {
if (stmt != null) {
stmt.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}try {
if(conn != null)
{
conn.close();
}
}catch(Exception e){
System.err.println("Close connection error:"+e);
}
finally{
try {conn.close();}
catch (Exception e) { e.printStackTrace(); }
} }
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exceptio e) {
if (rs != null) {
System.err.println("Close resultset error:"+e);
}
if (stmt != null) {
System.err.println("Close statement error:"+e);
}
if (conn != null) {
System.err.println("Close connection error:"+e);
}
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
System.err.println("Finally close connection error:"+e);
}
}个人认为这样的结构比较好一点,一个大try就可以了
在catch(Exception e)部分
不过只是提供一种思路,具体你可以再改过来
原因在于如果没有调用executeQuery,rs就是空的
或者调用executeQuery失败,rs也是空的
rs.close()出错后
可以在后面的finally里继续执行conn.close();