1. JDBC工具类
- 23.12.18 22:46更新
- 23.12.19 23:32更新
1.1. DBUtil
public class DBUtil {
private static ResourceBundle bundle = ResourceBundle.getBundle("resources/jdbc");
private static String driver = bundle.getString("driver");
private static String url = bundle.getString("url");
private static String user = bundle.getString("user");
private static String password = bundle.getString("password");
private DBUtil(){}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static ThreadLocal<Connection> local = new ThreadLocal<>();
public static Connection getConnection() throws SQLException {
Connection conn = local.get();
if (conn == null) {
conn = DriverManager.getConnection(url, user, password);
local.set(conn);
}
return conn;
}
public static void close(Connection conn, Statement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close()
}catch(SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close()
} catch(SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close()
local.remove();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
1.2. 模糊查询
public class JDBCTest09 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "select ename from emp where ename like ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "_A%");
ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getString("ename"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
}
}
1.3. 增
public int insert(Account act) {
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "insert into t_act(actno, balance) values (?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, act.getActno);
ps.setDouble(2, act.getBalance());
count = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(conn, ps, null);
}
return count;
}
1.4. 删
public int deleteById(Long id) {
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "delete from t_act where id = ?";
ps = conn.prepareStatement(sql);
ps.setLong(1, id);
count = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(conn, ps, null);
}
}
1.5. 改
public int update(Account act) {
Connection conn = null;
PreparedStatement ps = null;
int count = 0;
try {
conn = DBUtil.getConnection();
String sql = "update t_act set balance = ?, actno = ? where id = ?";
ps = conn.prepareStatement(sql);
ps.setDouble(1, act.getBalance());
ps.setString(2, act.getActno());
ps.setLong(3, act.getId());
count = ps.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(conn, ps, null);
}
return count;
}
1.6. 查
public Account selectByActno(String actno) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Account act = null;
try {
conn = DBUtil.getConnection();
String sql = "select id, balance from t_act where actno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, actno);
rs = ps.executeQuery();
if (rs.next()) {
Long id = rs.getLong("id");
Double balance = rs.getDouble("balance");
act = new Account();
act.setId(id);
act.setActno(actno);
act.setBalance(balance);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(conn, ps, null);
}
return act;
}
public List<Account> selectAll() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Account> list = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "select id, actno, balance from t_act";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
where (rs.next()) {
Long id = rs.getLong("id");
String actno = rs.getString("actno");
Double balance = rs.getDouble("balance");
Account act = new Account();
act.setId(id);
act.setActno(actno);
act.setBalance(balance);
list.add(act);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(conn, ps, null);
}
return list;
}
1.7. 视频
- start://www.bilibili.com/video/BV1Z3411C7NZ?p=67