存不存在安全隐患
close方法会不会造成内存泄漏
public class Jdbc {
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 jdbcPath = "src/jdbc.properties";
//测试代码
public static void main(String[] args) {
Jdbc jdbc = new Jdbc();
try {
jdbc.open();
String sql = "select * from GC_EXAMTYPE t where t.examtype_id= ? or examtype_id=?";
List<Map<String, Object>> query = jdbc.query(sql,5,6);
System.out.println(query);
String inser_sql = "insert into test (id, name) values (?, ?)";
Integer add_flg = jdbc.add(inser_sql, 2,2);
System.out.println(add_flg);
String mod_sql = "update test set name = ? where id=?";
Integer mod_flg =jdbc.mod(mod_sql, 2,1);
System.out.println(mod_flg);
String del_sql = "delete test where id=?";
Integer del_flg =jdbc.del(del_sql, 1);
System.out.println(del_flg);
} catch (Exception e) {
e.printStackTrace();
}finally {
/*
* 我最大的疑惑就在这里,这里我并没有告诉计算机,应该关闭某个对象
* 我就是直接写了一个close方法
* 例如100个人同时使用这个代码
* 计算机是怎么知道该关闭哪个对象的
* 或者说,我这段代码本身就是有问题的
*/
jdbc.close();
}
}
/**
* 连接数据库
* @throws Exception
*/
public void open() throws Exception{
fis = new FileInputStream(jdbcPath);
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);
}
/**
* 关闭连接
*/
public void close() {
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) throws SQLException {
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
flg = ps.executeUpdate();
return flg;
}
/**
* 执行查询语句
* @param sql 查询语句
* @param objs 参数
* @return 查询结果
* @throws SQLException sql异常
*/
public List<Map<String,Object>> query(String sql,Object...objs) throws SQLException{
list = new ArrayList<>();
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);
}
return list;
}
/**
* 添加数据
* @param sql 添加语句
* @param objs 参数
* @return 0失败 1成功
* @throws SQLException sql异常
*/
public int add(String sql,Object...objs) throws SQLException {
flg = dml(sql, objs);
return flg;
}
/**
* 修改数据
* @param sql 修改语句
* @param objs 参数
* @return 0失败 1成功
* @throws SQLException sql异常
*/
public int mod(String sql,Object...objs) throws SQLException{
flg = dml(sql, objs);
return flg;
}
/**
* 删除数据
* @param sql 删除语句
* @param objs 参数
* @return 0失败 1成功
* @throws SQLException sql异常
*/
public int del(String sql,Object...objs) throws SQLException{
flg = dml(sql, objs);
return flg;
}
}