You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.9 KiB
150 lines
4.9 KiB
2 months ago
|
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<TeamInfo> getAllTeamInfos() {
|
||
|
String sql = "SELECT * FROM TeamInfo";
|
||
|
List<TeamInfo> 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();
|
||
|
}
|
||
|
}
|
||
|
}
|