懒汉单例安全basedao
Posted lenlen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了懒汉单例安全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;
}
}
以上是关于懒汉单例安全basedao的主要内容,如果未能解决你的问题,请参考以下文章