我自己封装了一个jdbc的包
如何能判断出,我这段代码是否存在内漏泄漏的情况
/**
* oracle jdbc
* @author rgy
*
*/
public class OracleJdbc {
private Connection conn;
private ResultSet rs;
private ResultSetMetaData md;
private PreparedStatement ps;
private Properties prop;
private String url;
private String user;
private String psw;
private List<Map<String,Object>> list;
private Map<String,Object> map;
private Integer flg;
private FileInputStream fis;
private String path = "src/orcl.properties";
//测试代码
public static void main(String[] args) {
OracleJdbc oracle = new OracleJdbc();
try {
String sql = "select * from GC_EXAMTYPE t where t.examtype_id= ? or examtype_id=?";
List<Map<String, Object>> query = oracle.query(sql,5,6);
System.out.println(query);
String inser_sql = "insert into test (id, name) values (?, ?)";
Integer add_flg = oracle.add(inser_sql, 2,2);
System.out.println(add_flg);
String mod_sql = "update test set name = ? where id=?";
Integer mod_flg =oracle.mod(mod_sql, 2,1);
System.out.println(mod_flg);
String del_sql = "delete test where id=?";
Integer del_flg =oracle.del(del_sql, 1);
System.out.println(del_flg);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 连接数据库
* @throws Exception
*/
public void open(){
try {
fis = new FileInputStream(path);
prop = new Properties();
prop.load(fis);
url = prop.getProperty("db.url");
user = prop.getProperty("db.user");
psw = prop.getProperty("db.password");
Class.forName(prop.getProperty("driverClass"));
conn = DriverManager.getConnection(url, user, psw);
} catch (IOException | ClassNotFoundException | SQLException e) {
close(conn,ps,rs);
}
}
/**
* 关闭连接
*/
public void close(Connection conn,PreparedStatement ps,ResultSet rs) {
try {
if(ps!=null) ps.close();
if(rs!=null) rs.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 执行dml语句
* @param sql dml语句
* @param objs 参数
* @return 0失败 1成功
* @throws SQLException sql异常
*/
public int dml(String sql, Object... objs) {
open();
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
flg = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(conn,ps,rs);
}
return flg;
}
/**
* 执行查询语句
* @param sql 查询语句
* @param objs 参数
* @return 查询结果
*/
public List<Map<String,Object>> query(String sql,Object...objs){
open();
list = new ArrayList<>();
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
rs = ps.executeQuery();
md = rs.getMetaData();
while (rs.next()) {
map = new HashMap<>();
for (int i = 1; i <= md.getColumnCount(); i++) {
String key = md.getColumnName(i);
Object value = rs.getObject(i);
map.put(key, value);
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(conn,ps,rs);
}
return list;
}
/**
* 添加数据
* @param sql 添加语句
* @param objs 参数
* @return 0失败 1成功
*/
public int add(String sql,Object...objs) {
flg = dml(sql, objs);
return flg;
}
/**
* 修改数据
* @param sql 修改语句
* @param objs 参数
* @return 0失败 1成功
* @throws SQLException sql异常
*/
public int mod(String sql,Object...objs){
flg = dml(sql, objs);
return flg;
}
/**
* 删除数据
* @param sql 删除语句
* @param objs 参数
* @return 0失败 1成功
*/
public int del(String sql,Object...objs){
flg = dml(sql, objs);
return flg;
}
}