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.
167 lines
5.3 KiB
167 lines
5.3 KiB
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.Tchengjidaolzh;
|
|
import com.WR.StudentMS.model.Registrationwmm;
|
|
import com.WR.StudentMS.model.Tchengjilzh;
|
|
|
|
|
|
public class Tchengjidaoimplzh implements Tchengjidaolzh {
|
|
|
|
@Override
|
|
public List<Tchengjilzh> findAll() {
|
|
// TODO Auto-generated method stub
|
|
String sql = "select id, stu_id, kecheng_id, chengji, xuenian, stu_xuehao, kecheng_name from t_chengji"; List<Tchengjilzh> list = new ArrayList<Tchengjilzh>();
|
|
|
|
try(
|
|
Connection conn = DBHelper.getConnection();
|
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
ResultSet rs = pstmt.executeQuery(); ){
|
|
//6.遍历结果集
|
|
while(rs.next()) {
|
|
|
|
Tchengjilzh p = new Tchengjilzh();
|
|
|
|
p.setId(rs.getInt("id"));
|
|
p.setStu_id(rs.getInt("stu_id"));
|
|
p.setKecheng_id(rs.getInt("kecheng_id"));
|
|
p.setChengji(rs.getInt("chengji"));
|
|
p.setXuenian(rs.getString("xuenian"));
|
|
p.setStu_xuehao(rs.getString("stu_xuehao"));
|
|
p.setKecheng_name(rs.getString("kecheng_name"));
|
|
|
|
|
|
list.add(p);
|
|
}
|
|
|
|
}catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return list; }
|
|
|
|
@Override
|
|
public Tchengjilzh findById(String tchengjilzh) {
|
|
// TODO Auto-generated method stub
|
|
Connection conn = null;
|
|
PreparedStatement pstmt = null;
|
|
ResultSet rs = null;
|
|
|
|
String sql = "select id,stu_id,kecheng_id,chengji,xuenian,stu_xuehao,kecheng_name from t_chengji where id = ?";
|
|
|
|
try {
|
|
conn = DBHelper.getConnection();
|
|
pstmt = conn.prepareStatement(sql);
|
|
pstmt.setString(1, tchengjilzh);
|
|
rs = pstmt.executeQuery();
|
|
if (rs.next()) {
|
|
Tchengjilzh p = new Tchengjilzh();
|
|
|
|
p.setId(rs.getInt("id"));
|
|
p.setStu_id(rs.getInt("stu_id"));
|
|
p.setKecheng_id(rs.getInt("kecheng_id"));
|
|
p.setChengji(rs.getInt("chengji"));
|
|
p.setXuenian(rs.getString("xuenian"));
|
|
p.setStu_xuehao(rs.getString("stu_xuehao"));
|
|
p.setKecheng_name(rs.getString("kecheng_name"));
|
|
|
|
return p;
|
|
}
|
|
|
|
} 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(Tchengjilzh tchengjilzh) {
|
|
// TODO Auto-generated method stub
|
|
String sql = "insert into t_chengji (id, stu_id, kecheng_id, chengji, xuenian, stu_xuehao, kecheng_name) values (?,?,?,?,?,?,?)";
|
|
try(
|
|
Connection conn = DBHelper.getConnection();
|
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
)
|
|
{
|
|
pstmt.setInt(1, tchengjilzh.getId());
|
|
pstmt.setInt(2, tchengjilzh.getStu_id());
|
|
pstmt.setInt(3, tchengjilzh.getKecheng_id());
|
|
pstmt.setInt(4, tchengjilzh.getChengji());
|
|
pstmt.setString(5, tchengjilzh.getXuenian());
|
|
pstmt.setString(6, tchengjilzh.getStu_xuehao());
|
|
pstmt.setString(7, tchengjilzh.getKecheng_name());
|
|
|
|
int executeUpdate = pstmt.executeUpdate();
|
|
System.out.printf("成功插入%d数据\n",executeUpdate);
|
|
return executeUpdate;
|
|
} catch (SQLException e) {
|
|
// TODO: handle exception
|
|
e.printStackTrace();
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
@Override
|
|
public int modify(Tchengjilzh tchengjilzh) {
|
|
// TODO Auto-generated method stub
|
|
String sql = "UPDATE t_chengji SET id = ?, stu_id = ?, kecheng_id = ?, chengji = ?, xuenian = ?, stu_xuehao = ?, kecheng_name = ? WHERE id = ?";
|
|
|
|
try (
|
|
Connection conn = DBHelper.getConnection();
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)
|
|
) {
|
|
// 设置新值
|
|
pstmt.setInt(1, tchengjilzh.getId());
|
|
pstmt.setInt(2, tchengjilzh.getStu_id());
|
|
pstmt.setInt(3, tchengjilzh.getKecheng_id());
|
|
pstmt.setInt(4, tchengjilzh.getChengji());
|
|
pstmt.setString(5, tchengjilzh.getXuenian());
|
|
pstmt.setString(6, tchengjilzh.getStu_xuehao());
|
|
pstmt.setString(7, tchengjilzh.getKecheng_name());
|
|
// 设置旧的主键值,用于定位要更新的记录
|
|
pstmt.setInt(8, tchengjilzh.getId());
|
|
int affectedRows = pstmt.executeUpdate();
|
|
System.out.printf("成功更新%d条数据。\n", affectedRows);
|
|
return affectedRows;
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int remove(Tchengjilzh tchengjilzh) {
|
|
String sql = "DELETE FROM t_chengji WHERE id = ?";
|
|
try (Connection conn = DBHelper.getConnection();
|
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
pstmt.setInt(1, tchengjilzh.getId());
|
|
int affectedRows = pstmt.executeUpdate();
|
|
if (affectedRows > 0) {
|
|
System.out.printf("成功删除%d条数据。\n", affectedRows);
|
|
return affectedRows; // 返回受影响的行数
|
|
} else {
|
|
System.out.println("未找到对应的记录或删除失败。");
|
|
return 0; // 没有记录被删除
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
return -1; // 发生异常
|
|
}
|
|
}
|
|
}
|
|
|
|
|