From 09593c4d8cf9c7371b95ae92a7b135621365e8b3 Mon Sep 17 00:00:00 2001 From: weiliyue <2654450615@qq.com> Date: Thu, 19 Sep 2024 15:41:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TeamInfoDal.java | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 TeamInfoDal.java diff --git a/TeamInfoDal.java b/TeamInfoDal.java new file mode 100644 index 0000000..47bbd24 --- /dev/null +++ b/TeamInfoDal.java @@ -0,0 +1,150 @@ +package cn.fly.jdbc; + +import cn.fly.entity.TeamInfo; + +import java.io.IOException; +import java.sql.*; +import java.util.*; + +public class TeamInfoDal { + // 数据库连接字符串和相关属性 + private static final String DB_URL = "jdbc:mysql://localhost:3306/DBTeam"; + private static final String USER = "root"; + private static final String PASS = "20030815"; + + // JDBC驱动名 + private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; + + // Connection对象 + private Connection conn; + + public static void main(String[] args) throws IOException { + try { + // 加载JDBC驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + // 建立数据库连接 + Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); + System.out.println("连接成功"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // 构造函数 + public TeamInfoDal() { + try { + // 加载JDBC驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + // 建立数据库连接 + conn = DriverManager.getConnection(DB_URL, USER, PASS); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // 添加报名信息 + public boolean addTeamInfo(TeamInfo teamInfo) { + String sql = "INSERT INTO TeamInfo (id, sno, name, sex, tel) VALUES (?, ?, ?, ?, ?)"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, teamInfo.getId()); + stmt.setString(2, teamInfo.getSno()); + stmt.setString(3, teamInfo.getName()); + stmt.setString(4, teamInfo.getSex()); + stmt.setString(5, teamInfo.getTel()); + // 执行插入操作并返回结果 + int rowsAffected = stmt.executeUpdate(); + return rowsAffected > 0; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + // 修改报名信息 + public boolean updateTeamInfo(TeamInfo teamInfo) { + String sql = "UPDATE TeamInfo SET sno=?, name=?, sex=?, tel=? WHERE id=?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, teamInfo.getSno()); + stmt.setString(2, teamInfo.getName()); + stmt.setString(3, teamInfo.getSex()); + stmt.setString(4, teamInfo.getTel()); + stmt.setString(5, teamInfo.getId()); + int rowsAffected = stmt.executeUpdate(); + return rowsAffected > 0; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + // 删除报名信息 + public boolean deleteTeamInfo(String id) { + String sql = "DELETE FROM TeamInfo WHERE id=?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, id); + int rowsAffected = stmt.executeUpdate(); + return rowsAffected > 0; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + // 根据id查询某条信息 + public TeamInfo getTeamInfoById(String id) { + String sql = "SELECT * FROM TeamInfo WHERE id=?"; + TeamInfo teamInfo = null; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, id); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + teamInfo = new TeamInfo(); + teamInfo.setId(rs.getString("id")); + teamInfo.setSno(rs.getString("sno")); + teamInfo.setName(rs.getString("name")); + teamInfo.setSex(rs.getString("sex")); + teamInfo.setTel(rs.getString("tel")); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return teamInfo; + } + + // 查询所有数据 + public List getAllTeamInfos() { + String sql = "SELECT * FROM TeamInfo"; + List list = new ArrayList<>(); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery(sql); + while (rs.next()) { + TeamInfo teamInfo = new TeamInfo(); + teamInfo.setId(rs.getString("id")); + teamInfo.setSno(rs.getString("sno")); + teamInfo.setName(rs.getString("name")); + teamInfo.setSex(rs.getString("sex")); + teamInfo.setTel(rs.getString("tel")); + list.add(teamInfo); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + for (TeamInfo teamInfo : list) { + System.out.println(teamInfo.toString()); + } + + return list; + } + + // 关闭数据库连接 + public void close() { + try { + if (conn != null && !conn.isClosed()) { + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file