BaseDao
希望这个工具类对大家有帮助!
继承BaseDao类,即可调用BaseDao中的所有非私有成员。
节省了代码量,使代码更加简洁,通俗易懂。
只需调用方法,即可实现功能。
import java.sql.*;
/**
* 数据库连接与关闭工具类
*/
public class BaseDao {
private final String driver = "com.mysql.cj.jdbc.Driver";// 数据库驱动字符串
private final String url = "jdbc:mysql://localhost:3306/Leave";// 连接URL字符串
private final String user = "root"; // 数据库用户名
private final String password = "admin."; // 用户密码
Connection conn = null; // 数据连接对象
/**
* 获取数据库连接对象
*/
public Connection getConnection() {
if(conn==null) {
// 获取连接并捕获异常
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace(); // 异常处理
}
}
return conn; // 返回连接对象
}
/**
* 关闭数据库连接
* @param conn 数据库连接
* @param stmt Statement对象
* @param rs 结果集
*/
public void closeAll(Connection conn, Statement stmt,
ResultSet rs) {
// 若结果集对象不为空,则关闭
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若Statement对象不为空,则关闭
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 若数据库连接对象不为空,则关闭
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 增、删、改的操作
* @param preparedSql 预编译的 SQL 语句
* @param param 参数的字符串数组
* @return 影响的行数
*/
public int exceuteUpdate (String preparedSql, Object... param) {
PreparedStatement pstmt = null;
int num = 0;
conn = getConnection();
try {
pstmt = conn.prepareStatement(preparedSql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
//为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn,pstmt,null);
}
return num;
}
/**
* 查询操作
* @param preparedSql
* @param param
* @return
*/
public ResultSet executeQuery (String preparedSql, Object... param) {
PreparedStatement pstmt = null;
ResultSet rs = null;
conn = getConnection();
try {
pstmt = conn.prepareStatement(preparedSql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
//为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
} finally{
//closeAll(conn,pstmt,null);
}
return rs;
}
public int executeFirst (String preparedSql, Object... param) {
PreparedStatement pstmt = null;
ResultSet rs = null;
int num = 0;
conn = getConnection();
try {
pstmt = conn.prepareStatement(preparedSql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
//为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
rs = pstmt.executeQuery();
if (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
//closeAll(conn,pstmt,rs);
}
return num;
}
}
例如:
public class demo1 extends BaseDao {
public static void main(String[] args) {
//System.out.println(bd.executeUpdate("update tb_user set uname = ? where uname=?","嘎嘎","拉拉"));
ResultSet rs = executeQuery("select * from tb_type");
try {
while (rs.next()) {
int tid = rs.getInt("tid");
String tname = rs.getString("tname");
String tmenu = rs.getString("tmenu");
System.out.println(tid+"~~"+tname+"~~"+tmenu);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}