首页 > 其他 > 详细

懒汉单例安全basedao

时间:2018-12-12 18:29:17      阅读:162      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/lenlen/p/10109748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!