package com.roadjava.service.impl;
import com.roadjava.entity.StudentDO;
import com.roadjava.req.StudentRequest;
import com.roadjava.res.TableDTO;
import com.roadjava.service.StudentService;
import com.roadjava.util.DBUtil;
import com.sun.corba.se.impl.ior.ObjectAdapterIdNumber;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class StudentServiceImpl implements StudentService {
public TableDTO retrieveStudents(StudentRequest request) {
StringBuilder sql = new StringBuilder();
sql.append("select * from student2 ");
if (request.getSearchKey() != null && !"".equals(request.getSearchKey().trim())) {
sql.append(" where name like '%" + request.getSearchKey().trim() + "%' ");
}
sql.append("order by id desc limit ").append(request.getStart()).append(",").append(request.getPageSize());
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
TableDTO returnDTO = new TableDTO();
try {
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql.toString());
rs = ps.executeQuery();
//查询记录
returnDTO.setData(fillData(rs));
sql.setLength(0);
sql.append("select count(*) from student2 ");
if (request.getSearchKey() != null && !"".equals(request.getSearchKey().trim())) {
sql.append(" where name like '%" + request.getSearchKey().trim() + "%' ");
}
ps = conn.prepareStatement(sql.toString());
rs = ps.executeQuery();
while (rs.next()) {
int count = rs.getInt(1);
returnDTO.setTotalCount(count);
}
return returnDTO;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(rs);
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
return null;
}
public boolean add(StudentDO studentDo) {
StringBuilder sql = new StringBuilder();
sql.append("insert into student2(name,no,banji,chinese,english,math) ");
sql.append("values(?,?,?,?,?,?) ");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql.toString());
ps.setString(1, studentDo.getName());
ps.setString(2, studentDo.getNo());
ps.setString(3, studentDo.getChinese());
ps.setString(4, studentDo.getEnglish());
ps.setString(5, studentDo.getMath());
ps.setString(6, studentDo.getBanji());
return ps.executeUpdate() == 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
return false;
}
public StudentDO getById(int selectedStudentId) {
StringBuilder sql = new StringBuilder("select * from student2 where id = ? ");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
StudentDO studentDO = new StudentDO();
try {
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql.toString());
ps.setInt(1, selectedStudentId);
rs = ps.executeQuery();
while (rs.next()) {
//处理查出的每一条记录
int id = rs.getInt("id");
String name = rs.getString("name");
String no = rs.getString("no");
String chinese = rs.getString("chinese");
String english = rs.getString("english");
String math = rs.getString("math");
String banji = rs.getString("banji");
studentDO.setId(id);
studentDO.setNo(no);
studentDO.setName(name);
studentDO.setChinese(chinese);
studentDO.setEnglish(english);
studentDO.setMath(math);
studentDO.setBanji(banji);
}
return studentDO;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(rs);
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
return null;
}
public boolean update(StudentDO studentDo) {
StringBuilder sql = new StringBuilder();
sql.append("update student2 set name = ?,banji=?,no= ?,chinese= ?,english= ?,math= ?");
sql.append("where id = ?");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql.toString());
ps.setString(1, studentDo.getName());
ps.setString(2, studentDo.getNo());
ps.setString(3, studentDo.getChinese());
ps.setString(4, studentDo.getEnglish());
ps.setString(5, studentDo.getMath());
ps.setString(7, studentDo.getBanji());
ps.setInt(6, studentDo.getId());
return ps.executeUpdate() == 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
return false;
}
public boolean delete(int[] selectedStudentIds) {
StringBuilder sql = new StringBuilder();
sql.append("delete from student2 where id in (");
int length = selectedStudentIds.length;
for (int i = 0; i < length; i++) {
if (i == (length - 1)) {
sql.append("?");
} else {
sql.append("?,");
}
}
sql.append(")");
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConn();
ps = conn.prepareStatement(sql.toString());
for (int i = 0; i < length; i++) {
//设置参数,从1开始
ps.setInt(i + 1,selectedStudentIds[i]);
}
return ps.executeUpdate() == length;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
return false;
}
private Vector> fillData(ResultSet rs) throws SQLException {
Vector> data = new Vector>();
while (rs.next()) {
//处理查出的每一条记录
Vector
package com.roadjava.handler;
import com.roadjava.entity.AdminDO;
import com.roadjava.service.AdminService;
import com.roadjava.service.StudentService;
import com.roadjava.service.impl.AdminServiceImpl;
import com.roadjava.service.impl.StudentServiceImpl;
import com.roadjava.student.view.AddStudentView;
import com.roadjava.student.view.LoginView;
import com.roadjava.student.view.MainView;
import com.roadjava.student.view.UpdateStudentView;
import sun.applet.Main;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MainViewHandler implements ActionListener {
private MainView mainView;
public MainViewHandler(MainView mainView) {
this.mainView = mainView;
}
public void actionPerformed(ActionEvent e) {
JButton Jbutton = (JButton) e.getSource();
String text = Jbutton.getText();
if ("增加".equals(text)) {
new AddStudentView(mainView);
} else if ("修改".equals(text)) {
int[] selectedStudentIds = mainView.getSelectedStudentIds();
if (selectedStudentIds.length != 1) {
JOptionPane.showMessageDialog(mainView, "一次只能修改一行");
return;
}
new UpdateStudentView(mainView, selectedStudentIds[0]);
} else if ("删除".equals(text)) {
int[] selectedStudentIds = mainView.getSelectedStudentIds();
if (selectedStudentIds.length == 0) {
JOptionPane.showMessageDialog(mainView, "请选择要删除的行");
}
int option = JOptionPane.showConfirmDialog(mainView, "确认要选择删除" + selectedStudentIds.length +
"行吗", "确认删除", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {//确认 执行删除
StudentService studentService = new StudentServiceImpl();
boolean deleteResult = studentService.delete(selectedStudentIds);
if (deleteResult) {
//重新加载表格查到最新数据
mainView.reloadTable();
} else {
JOptionPane.showMessageDialog(mainView,"删除失败");
}
}
} else if ("搜索".equals(text)) {
mainView.setPageNow(1);
mainView.reloadTable();
} else if ("上一页".equals(text)) {
mainView.setPageNow(mainView.getPageNow() - 1);
mainView.reloadTable();
} else if ("下一页".equals(text)) {
mainView.setPageNow(mainView.getPageNow() + 1);
mainView.reloadTable();
}
}
}
你的SQL没有在resultMap 里面描述