懒汉单例安全basedao

package Dao;

import java.sql.*;

public class BaseDao {
private String drname = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/jdhb";
private String name = "root";
private String pwd = "root";
private static Connection conn = null;

private BaseDao() {
try {
Class.forName(drname);
conn = DriverManager.getConnection(url, name, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static Connection getconn() {
if (conn == null) {
new BaseDao();
}
return conn;
}

public static void closeAll(ResultSet rs, Statement stat) {
try {
if (rs != null)
rs.close();
if (stat != null)
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* 公共增删改方法
*
* @param sql sql语句
* @param objects 语句中各个问号所表达的值
* @return
*/
public static int excutesql(String sql, Object... objects) {
int count = 0;
Connection con = getconn();
PreparedStatement pre = null;
try {
pre = con.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pre.setObject(i + 1, objects[i]);
}
count = pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}

}

猜你喜欢

转载自www.cnblogs.com/lenlen/p/10109748.html