码迷,mamicode.com
首页 > 其他好文 > 详细

懒汉单例安全basedao

时间:2018-12-12 18:54:16      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:mys   import   param   sql   print   count   url   mysql   new   

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

标签:mys   import   param   sql   print   count   url   mysql   new   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!