案例四:封装共有操作
封装一个数据库的会话的类
| import java.sql.*; |
| |
| public class ConnectionUtil { |
| |
| |
| |
| |
| public static Connection getConnection() { |
| try { |
| Class.forName("com.mysql.cj.jdbc.Driver"); |
| String url = "jdbc:mysql://localhost:3306/jdbctest?serverTimezone=GMT"; |
| String user = "root"; |
| String password = "root"; |
| Connection connection = DriverManager.getConnection(url, user, password); |
| return connection; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| |
| public static void close(Connection conn) { |
| if(conn != null) { |
| try { |
| conn.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| public static void close(PreparedStatement ps) { |
| if(ps != null) { |
| try { |
| ps.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| public static void close(ResultSet rs) { |
| if(rs != null) { |
| try { |
| rs.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| } |
| |
封装对用户类的crud方法
| import java.sql.Connection; |
| import java.sql.Date; |
| import java.sql.PreparedStatement; |
| import java.sql.ResultSet; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class UserManager { |
| |
| |
| |
| public void add(User user) { |
| Connection conn = null; |
| PreparedStatement ps = null; |
| try { |
| conn = ConnectionUtil.getConnection(); |
| String sql = "insert into t_user(username, age, sex, birthday) value(?, ?, ?, ?)"; |
| ps = conn.prepareStatement(sql); |
| |
| ps.setString(1, user.getUsername()); |
| ps.setInt(2, user.getAge()); |
| ps.setString(3, user.getSex()); |
| ps.setDate(4, new Date(user.getBirthday().getTime()), null); |
| int rows = ps.executeUpdate(); |
| System.out.println("->插入成功" + rows + "条信息"); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| ConnectionUtil.close(ps); |
| ConnectionUtil.close(conn); |
| } |
| } |
| |
| |
| |
| |
| public void del(int id) { |
| Connection conn = null; |
| PreparedStatement ps = null; |
| try { |
| conn = ConnectionUtil.getConnection(); |
| String sql = "delete from t_user where id=?"; |
| ps = conn.prepareCall(sql); |
| ps.setInt(1, id); |
| int i = ps.executeUpdate(); |
| System.out.println("删除成功" + i + "条信息"); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| }finally { |
| ConnectionUtil.close(ps); |
| ConnectionUtil.close(conn); |
| } |
| } |
| |
| |
| |
| |
| public User getUser(int id) { |
| Connection conn = null; |
| PreparedStatement ps = null; |
| ResultSet rs = null; |
| try { |
| conn = ConnectionUtil.getConnection(); |
| String sql = "select * from t_user where id=?"; |
| ps = conn.prepareStatement(sql); |
| ps.setInt(1, id); |
| rs = ps.executeQuery(); |
| while(rs.next()) { |
| User u = new User(); |
| |
| u.setId(rs.getInt("id")); |
| |
| u.setUsername(rs.getString("username")); |
| u.setAge(rs.getInt("age")); |
| u.setBirthday(rs.getDate("birthday")); |
| u.setSex(rs.getString("sex")); |
| return u; |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| ConnectionUtil.close(rs); |
| ConnectionUtil.close(ps); |
| ConnectionUtil.close(conn); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| public List<User> getAll() { |
| Connection conn = null; |
| PreparedStatement ps = null; |
| ResultSet rs = null; |
| try { |
| conn = ConnectionUtil.getConnection(); |
| String sql = "select * from t_user"; |
| ps = conn.prepareStatement(sql); |
| |
| rs = ps.executeQuery(); |
| List<User> lists = new ArrayList<User>(); |
| while(rs.next()) { |
| User u = new User(); |
| u.setId(rs.getInt("id")); |
| u.setUsername(rs.getString("username")); |
| u.setAge(rs.getInt("age")); |
| u.setBirthday(rs.getDate("birthday")); |
| u.setSex(rs.getString("sex")); |
| lists.add(u); |
| } |
| return lists; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| ConnectionUtil.close(rs); |
| ConnectionUtil.close(ps); |
| ConnectionUtil.close(conn); |
| } |
| return null; |
| } |
| |
| |
| |
| |
| public int update(User user){ |
| Connection conn = null; |
| PreparedStatement ps = null; |
| try { |
| conn = ConnectionUtil.getConnection(); |
| String sql = "update t_user set age=?,username=?,sex=?,birthday=? where id=?"; |
| ps = conn.prepareCall(sql); |
| ps.setInt(1, user.getAge()); |
| ps.setString(2, user.getUsername()); |
| ps.setString(3, user.getSex()); |
| ps.setDate(4, new Date(user.getBirthday().getTime()), null); |
| ps.setInt(5, user.getId()); |
| int i = ps.executeUpdate(); |
| System.out.println("跟新成功" + i + "条信息"); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| }finally { |
| ConnectionUtil.close(ps); |
| ConnectionUtil.close(conn); |
| } |
| return 0; |
| } |
| |
| } |
| |
测试
| import java.text.ParseException; |
| import java.text.SimpleDateFormat; |
| import java.util.List; |
| |
| public class UserTest { |
| |
| public static void main(String[] args) throws ParseException { |
| UserManager um = new UserManager(); |
| User u = new User(); |
| u.setUsername("大力"); |
| u.setAge(20); |
| u.setSex("男"); |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| u.setBirthday(sdf.parse("1990-10-1")); |
| |
| um.add(u); |
| |
| |
| User user = um.getUser(1); |
| System.out.println("id=1 ->" + user.getUsername()); |
| |
| |
| List<User> list = um.getAll(); |
| for(User u1 : list) { |
| System.out.println("User ->" + u1.getUsername()); |
| } |
| |
| |
| User user1 = new User(); |
| user1.setId(1); |
| user1.setUsername("狗剩"); |
| user1.setAge(15); |
| user1.setSex("男"); |
| SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); |
| user1.setBirthday(sdf1.parse("2021-1-1")); |
| um.update(user1); |
| |
| |
| um.del(1); |
| } |
| |
| } |
| |
| ->插入成功1条信息 |
| id=1 ->小花 |
| User ->小花 |
| User ->大力 |
| 跟新成功1条信息 |
| 删除成功1条信息 |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术