日期:2014-05-20  浏览次数:20726 次

JDBC 连接Oracle代码优化问题
以下代码是以前一同事写的,现在走了,发现conn rs stmt都没有关闭,又不知道从哪里下手,请各位指教!!
代码贴出来,高手们帮我看看!!

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Hashtable;

public class DBConnection {//用于数据库连接
public static Connection oracleConn()//建立连接ORACLE数据库
{
Hashtable hs = DataBaseInfo.getDBConfig();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection((String)hs.get("ORACLE_URL"),(String)hs.get("ORACLE_UID"),(String)hs.get("ORACLE_PWD"));
return conn;
}catch(ClassNotFoundException e)
{
System.out.println(e.getMessage()+"类找不到");
return null;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
return null;
}
catch(Exception e)
{
System.out.println("有错误:"+e.getMessage());
return null;
}
}
}


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import duba.dbo.dbaccess.DBConnection;

public class DBOperate {//数据库操作类
public static boolean executeOperate(PreparedStatement pstmt) throws SQLException
{
if(pstmt.executeUpdate()!=0)
{
return true;
}
else
{
return false;
}
}

public static ResultSet executeGetResultSet(PreparedStatement pstmt) throws SQLException
{
return pstmt.executeQuery();
}

public static PreparedStatement getPreparedStatement(String sql) throws SQLException
{
return DBConnection.oracleConn().prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}

public static int getRSsum(ResultSet rs) throws SQLException//获取该记录集里的记录数
{
rs.last();
int rowtotal = rs.getRow();
rs.beforeFirst();
return rowtotal;
}
}

public class ESorts_Operate {
public static boolean insertSort(E_Sorts sort)//添加类别
{
try
{
PreparedStatement pstmt = DBOperate.getPreparedStatement("insert into e_sorts(SORT_PID,SORT_NAME) values(?,?)");
pstmt.setInt(1, sort.getSort_pid());
pstmt.setString(2, sort.getSort_name());
return DBOperate.executeOperate(pstmt);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}

public static boolean deleteSort(E_Sorts sort)//删除类别
{
try
{
PreparedStatement pstmt = DBOperate.getPreparedStatement("delete e_sorts where sort_id=?");
pstmt.setInt(1, sort.getSort_id());
return DBOperate.executeOperate(pstmt);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}

public static boolean updateSort(E_Sorts sort)//修改类别
{
try
{
PreparedStatement pstmt = DBOperate.getPreparedStatement("update e_sorts set SORT_PID=?,SORT_NAME=? where sort_id=?");
pstmt.setInt(1, sort.getSort_pid());
pstmt.setString(2, sort.getSort_name());
pstmt.setInt(3, sort.getSort_id());
return DBOperate.executeOperate(pstmt);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}

public static E_Sorts getESortByid(int sort_id)//根据sort_id取得该id的信息
{
try
{
PreparedStatement pstmt = DBOperate.getPreparedStatement("select * from e_sorts where SORT_ID=?");
pstmt.setInt(1, sort_id);
return getESort(DBOperate.executeGetResultSet(pstmt));
}
catch(Exception e)
{
System.out.println(e.getMessage());
return null;
}
}

private static E_Sorts getESort(ResultSet rs) throws SQLException