/**
* 作为所有Dao实现类的一个基类(父类)
* @author BluceYoung
* 2020-4-13
*/
public class BaseDao {
// 加载驱动的字符串
static String driverClass = "com.mysql.jdbc.Driver";
// 数据库字符串
static String url = "jdbc:mysql://localhost:3306/hrinfo?userUnicode=true&character=utf-8";
// 数据库用户名
static String user = "root";
// 密码
static String pwd = "abc123";
static {
System.out.println("读取配置文件");
Properties properties=new Properties();
try {
//创建一个读取配置文件的输入流
InputStream is=BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
properties.load(is);//开始读取文件里的信息到properties对象中
driverClass=properties.getProperty("driver");
url=properties.getProperty("url");
user=properties.getProperty("name");
pwd=properties.getProperty("pwd");
} catch (IOException e) {
System.out.println("读取配置文件出错");
e.printStackTrace();
}
}
/**
* 获取一个数据库连接
*
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() {
// 1.加载驱动
try {
Class.forName(driverClass);
// 2.创建连接(自动打开)
Connection connection = DriverManager.getConnection(url, user, pwd);
return connection;
} catch (ClassNotFoundException e) {
System.out.println("加载驱动出错,请检查是否导入jar包,以及驱动类的拼写");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("创建连接出错,请检查数据库名、用户名和密码是否正确");
e.printStackTrace();
}
return null;
}
/**
* 执行一个带参数的增删改sql,返回影响行数
*
* @param sql 带?参数的sql语句
* @param params sql语句里需要替换的参数具体的值
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int execute(String sql, Object... params) {
// 获取一个连接
Connection connection = this.getConnection();
PreparedStatement stat=null;
// 根据sql和连接,获取一个声明
try {
stat = connection.prepareStatement(sql);
this.setParams(stat, params);
int hang = stat.executeUpdate();
return hang;
} catch (SQLException e) {
System.out.println("执行sql出错");
System.out.println("执行的sql:" + sql);
System.out.println("传入的参数是:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]);
}
e.printStackTrace();
}finally {
this.closeAll(connection, stat, null);
}
return 0;
}
/**
* 执行插入语句,返回自增编号
* @param sql
* @param params
* @return
*/
public int executeInsert(String sql, Object... params) {
// 获取一个连接
Connection connection = this.getConnection();
PreparedStatement stat = null;
ResultSet generatedKeys = null;
// 根据sql和连接,获取一个声明
try {
stat = connection.prepareStatement(sql);
this.setParams(stat, params);
int hang = stat.executeUpdate();
generatedKeys = stat.getGeneratedKeys();//查询刚插入的自增列
if (generatedKeys.next()) {
return generatedKeys.getInt(1);//返回
}
} catch (SQLException e) {
System.out.println("执行sql出错");
System.out.println("执行的sql:" + sql);
System.out.println("传入的参数是:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]);
}
e.printStackTrace();
} finally {
this.closeAll(connection, stat, generatedKeys);
}
return 0;
}
/**
* 获取某个查询的总数据条数(分页时用)
* @param baseSql 查询语句,没有limit的查询
* @param params 参数
* @return
*/
public int getDataCount(String baseSql,Object... params) {
String sql="select count(0) from ("+baseSql+") t;";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=getConnection();
ps=conn.prepareStatement(sql);
this.setParams(ps, params);
rs = ps.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 为Statement加参数
* @param ps
* @param params
* @throws SQLException
*/
public void setParams(PreparedStatement ps,Object... params) throws SQLException {
if(params==null){
return;
}
for(int i=0;i<params.length;i++) {
ps.setObject(i+1, params[i]);
}
}
/**
* 释放所有资源
*
* @param connection
* @param stat
* @param rs
*/
public void closeAll(Connection connection, PreparedStatement stat, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stat != null) {
stat.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}