|
|
package com.WR.StudentMS.dao.mysql;
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.PreparedStatement;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
import com.WR.StudentMS.dao.ZhuanyeDaoWR;
|
|
|
import com.WR.StudentMS.model.ZhuanyeWR;
|
|
|
|
|
|
public class ZhuanyeDaoImpWR implements ZhuanyeDaoWR {
|
|
|
|
|
|
@Override
|
|
|
public List<ZhuanyeWR> findAll() {
|
|
|
// TODO 自动生成的方法存根
|
|
|
String sql = "select id,name,jieshao from t_zhuanye";
|
|
|
List<ZhuanyeWR> list = new ArrayList<ZhuanyeWR>();
|
|
|
try(
|
|
|
Connection conn = DBHelper.getConnection();//TODO
|
|
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
|
ResultSet rs = pstmt.executeQuery(); ){
|
|
|
while(rs.next()) {
|
|
|
ZhuanyeWR zhuanyewr = new ZhuanyeWR();
|
|
|
zhuanyewr.setId(rs.getInt("id"));
|
|
|
zhuanyewr.setName(rs.getString("name"));
|
|
|
zhuanyewr.setJieshao(rs.getString("jieshao"));
|
|
|
list.add(zhuanyewr);
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ZhuanyeWR findById(int id) {
|
|
|
// TODO 自动生成的方法存根
|
|
|
Connection conn = null;
|
|
|
PreparedStatement pstmt = null;
|
|
|
ResultSet rs = null;
|
|
|
|
|
|
try {
|
|
|
conn = DBHelper.getConnection();
|
|
|
String sql = "select id,name,jieshao from t_zhuanye where id = ?";
|
|
|
pstmt = conn.prepareStatement(sql);
|
|
|
pstmt.setInt(1, id);
|
|
|
rs = pstmt.executeQuery();
|
|
|
if (rs.next()) {
|
|
|
ZhuanyeWR zhuanyewr = new ZhuanyeWR();
|
|
|
zhuanyewr.setId(rs.getInt("id"));
|
|
|
zhuanyewr.setName(rs.getString("name"));
|
|
|
zhuanyewr.setJieshao(rs.getString("jieshao"));
|
|
|
return zhuanyewr;
|
|
|
}
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
DBHelper.close(rs, pstmt, conn);
|
|
|
} catch (SQLException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public int create(ZhuanyeWR zhuanyewr) {
|
|
|
// 检查ID是否已存在
|
|
|
String checkSql = "SELECT COUNT(*) FROM t_zhuanye WHERE id = ?";
|
|
|
try (Connection conn = DBHelper.getConnection();
|
|
|
PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
|
|
|
checkStmt.setInt(1, zhuanyewr.getId());
|
|
|
ResultSet rs = checkStmt.executeQuery();
|
|
|
if (rs.next() && rs.getInt(1) > 0) {
|
|
|
// 如果ID已存在,则返回提示
|
|
|
return 0; // 0通常表示操作没有影响任何行
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// 继续执行插入操作
|
|
|
String sql = "INSERT INTO t_zhuanye (id, name, jieshao) VALUES (?, ?, ?)";
|
|
|
try (Connection conn = DBHelper.getConnection();
|
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
|
pstmt.setInt(1, zhuanyewr.getId());
|
|
|
pstmt.setString(2, zhuanyewr.getName());
|
|
|
pstmt.setString(3, zhuanyewr.getJieshao());
|
|
|
int executeUpdate = pstmt.executeUpdate();
|
|
|
return executeUpdate;
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int remove(ZhuanyeWR zhuanyewr) {
|
|
|
String sql = "DELETE FROM t_zhuanye WHERE id = ?";
|
|
|
try (
|
|
|
Connection conn = DBHelper.getConnection();
|
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)
|
|
|
) {
|
|
|
pstmt.setInt(1, zhuanyewr.getId()); // 假设stu_id是外键,与专业ID相关联
|
|
|
int affectedRows = pstmt.executeUpdate();
|
|
|
System.out.printf("删除%d条数据。\n", affectedRows);
|
|
|
return affectedRows;
|
|
|
} catch (SQLException e) {
|
|
|
e.printStackTrace();
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
public int modify(ZhuanyeWR zhuanyewr) {
|
|
|
String sql = "UPDATE t_zhuanye SET name = ?, jieshao = ? WHERE id = ?";
|
|
|
try (
|
|
|
Connection conn = DBHelper.getConnection();
|
|
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
|
) {
|
|
|
pstmt.setString(1, zhuanyewr.getName());
|
|
|
pstmt.setString(2, zhuanyewr.getJieshao());
|
|
|
pstmt.setInt(3, zhuanyewr.getId());
|
|
|
int affectedRows = pstmt.executeUpdate();
|
|
|
System.out.printf("更新%d条数据。\n", affectedRows);
|
|
|
return affectedRows;
|
|
|
} catch (SQLException e) {
|
|
|
// 可以在这里添加更详细的错误处理
|
|
|
e.printStackTrace();
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|