📄 scoremagdialog.java
字号:
subjectPanel.setBackground(new java.awt.Color(170,
173, 242));
{
viewScrollPane = new JScrollPane();
subjectPanel.add(viewScrollPane);
viewScrollPane.setBounds(7, 28, 490, 357);
{
TableModel viewTableModel = new DefaultTableModel(
row, column);
viewTable = new JTable();
viewScrollPane.setViewportView(viewTable);
viewTable.setRowHeight(24);
viewTable.setModel(viewTableModel);
viewTable
.setPreferredSize(new java.awt.Dimension(
490, 329));
}
}
{
jSeparator2 = new JSeparator();
subjectPanel.add(jSeparator2);
jSeparator2.setBounds(504, 14, 63, 385);
jSeparator2
.setOrientation(SwingConstants.VERTICAL);
}
}
}
}
}
this.setSize(900, 475);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initTable() throws Exception {
// 建立一个JDBC对象
JdbcConnct jdbcConnection = new JdbcConnct();
// 定义Vector变量,存储从数据库查询来的信息
Vector vecData = new Vector();
String[] column = { "学号", "姓名", "科目号", "科目", "考试时间", "分数" };
// 定义SQL语句
String strSQL = "select * from sco_exam_vi";
stuScoreTableModel = new DefaultTableModel(row, column);
this.stuScoreTable.setModel(stuScoreTableModel);
stuScoreTable.getTableHeader().setBounds(0, 0, 490, 28);
// 获得数据
vecData = jdbcConnection.getData(strSQL);
// 依次为各行插入数据
for (int i = 0; i < vecData.size(); i++) {
stuScoreTableModel.addRow((String[]) vecData.get(i));
}
}
/**
* 查询数据
*
* @throws SQLException
*/
private void queryButtonactionPerformed(ActionEvent evt) {
JdbcConnct jdbcConnection = new JdbcConnct();
Vector vecData = new Vector();
String temp = "";
String[] column = { "科目号", "科目", "考试时间", "考试类型", "考试性质", "分数" };
if (this.termComboBox.getSelectedItem().toString().equals("学号")) {
temp = " student.studentid = " + getQueryInfo();
if (getStuID() == null) {
JOptionPane.showMessageDialog(this, "请输入学号");
}
}
if (this.termComboBox.getSelectedItem().toString().equals("姓名")) {
temp = " student.stuname like '%" + getQueryInfo() + "%'";
if (getStuName() == null) {
JOptionPane.showMessageDialog(this, "请输入姓名!");
}
}
// if(getStuID()=="" || getStuName()== ""){
// JOptionPane.showMessageDialog(this, "请输入学号或姓名!");
// }
if (!this.queryTextField.getText().toString().trim().equals("")) {
String sql = "select courseid,coursename,begintime,examtype,examkind,score "
+ " from sco_exam_vii, student "
+ " where sco_exam_vii.studentid = student.studentid and "
+ temp;
// JOptionPane.showMessageDialog(this, "请输入学号或姓名!");
stuScoreTableModel = new DefaultTableModel(row, column);
this.viewTable.setModel(stuScoreTableModel);
viewTable.getTableHeader().setPreferredSize(
new java.awt.Dimension(469, 16));
System.out.println(sql);
// 获得数据
try {
vecData = jdbcConnection.getData(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "输入有误!");
e.printStackTrace();
}
System.out.println(vecData.size());
if (vecData.size() == 0) {
JOptionPane.showMessageDialog(this, "没有要查询的记录!");
}
// 依次为各行插入数据
for (int i = 0; i < vecData.size(); i++) {
stuScoreTableModel.addRow((String[]) vecData.get(i));
}
}else
JOptionPane.showMessageDialog(this, "请注意正确的输入shit~~");
}
private String getStuName() {
// TODO Auto-generated method stub
String name = this.nameTextField.getText().toString().trim();
return name;
}
private String getStuID() {
String id = this.stuIdTextField.getText().toString().trim();
return id;
}
/**
* 查询条件
*/
private String getQueryInfo() {
String info = this.queryTextField.getText().toString().trim();
return info;
}
/**
* 保存学生成绩
*/
private void saveButtonActionPerformed(ActionEvent evt) {
Vector vecData = new Vector();
//更新数据条数
int updateLine;
//对用户输入进行校验
//该门考试是否为该生所学课程,若是,则保存成绩;否则要求用户重新输入
try {
Connection conn = null;
PreparedStatement stmt = null;
conn = JdbcConnct.fetchConnection();
String sql = "insert into stuscore(studentid,examid,examkind,ispresent,score) "
+ "values("
+ getStuID()
+ ","
+ getExamID()
+ ","
+ getKind() + ",'" + getPre() + "'," + getScore() + ")";
stmt = conn.prepareStatement(sql);
updateLine = stmt.executeUpdate(sql);
if(updateLine > 0){
//成功保存
JOptionPane.showMessageDialog(this, "录入成功!!!刷新查看结果...");
//向表格中添加一条数据,首先从数据库中查出一条数据,然后添加
this.stuScoreTableModel.addRow(new String[]{"学号","姓名",
"科目","科目号","考试时间","分数"});
//保存按钮置为false,新增、修改、删除按钮置为true
//保存按钮不可用,新增、修改、删除按钮可用
this.saveStuButton.enable(false);
this.newButton.enable(true);
this.modifyButton.enable(true);
this.deleteButton.enable(true);
}else{
JOptionPane.showMessageDialog(this, "录入失败!!!");
}
} catch (Exception e) {
}
}
private String getKind() {
// TODO Auto-generated method stub
String kind = "";
String temp = this.testKindStuComboBox.getSelectedItem().toString()
.trim();
if (temp.equals("1.正常考试")) {
kind = "1";
}
if (temp.equals("2.补考")) {
kind = "2";
}
if (temp.equals("3.重修")) {
kind = "3";
} else {
kind = "4";
}
return kind;
}
/**
* 获得分数
*
* @return socre
*/
private int getScore() {
int score = 0;
// TODO Auto-generated method stub
try {
score = Integer.parseInt(this.scoreStuTextField.getText()
.toString().toString());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "分数只能是0~100数字!");
return 0;
}
if (score < 0 || score > 100) {
JOptionPane.showMessageDialog(this, "分数只能是0~100的数字!");
} else {
// return score;
return score;
}
System.out.println(score);
return score;
}
/**
* 是否参加了考试
*
* @return Y为参加 N为缺考
*/
private String getPre() {
// TODO Auto-generated method stub
String pre = "";
if (this.isPresentRadioButton.isSelected()) {
pre = "Y";
} else {
pre = "N";
}
return pre;
}
/**
* 得到考试ID 从数据库中获取与所输入的比较 存在与否判断输入是否合法
*
* @return examID
*/
private String getExamID() {
// TODO Auto-generated method stub
// int i = 0;
// String exam[] = {};
String examID = this.examIDTextField.getText().toString().trim();
JdbcConnct jc = new JdbcConnct();
Vector v = new Vector();
// String[] temp = new String[] {};
try {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = JdbcConnct.fetchConnection();
stmt = conn.createStatement();
String sql = "select examid from exam where examid =" + examID;
rs = stmt.executeQuery(sql);
v = jc.getData(sql);
if (v.size() == 0) {
JOptionPane.showMessageDialog(this, "所输入的考试ID不存在\n请核实后重新输入!");
}
rs.close();
stmt.close();
conn.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "错误!");
}
return examID;
}
/**
* 新增学生成绩
* @param evt
*/
private void newButtonActionPerformed(ActionEvent evt) {
//新增学生成绩
this.stuIdTextField.setText("");
this.nameTextField.setText("");
this.subIdStuTextField.setText("");
this.subNameStuTextField.setText("");
this.scoreStuTextField.setText("");
this.scoreStuTextField.setEditable(true);
//保存按钮可用,新增、修改、删除按钮不可用
this.saveStuButton.enable(true);
this.newButton.enable(false);
this.modifyButton.enable(false);
this.deleteButton.enable(false);
}
/**
* 鼠标点击事件
*
* @param evt
*/
private void stuScoreTableMouseClicked(MouseEvent evt) {
// 定义鼠标点击行
int nrow = 0;
// 获取鼠标点击行
nrow = this.stuScoreTable.getSelectedRow();
// 获取行内数据
String stuid = String.valueOf(this.stuScoreTable.getValueAt(nrow, 0));
String stuname = String.valueOf(this.stuScoreTable.getValueAt(nrow, 1));
String subid = String.valueOf(this.stuScoreTable.getValueAt(nrow, 2));
String subName = String.valueOf(this.stuScoreTable.getValueAt(nrow, 3));
String time = String.valueOf(this.stuScoreTable.getValueAt(nrow, 4));
String score = String.valueOf(this.stuScoreTable.getValueAt(nrow, 5));
this.stuIdTextField.setText(stuid);
this.nameTextField.setText(stuname);
this.subIdStuTextField.setText(subid);
this.subNameStuTextField.setText(subName);
this.scoreStuTextField.setText(score);
//保存按钮不可用,新增、修改、删除按钮可用
this.saveStuButton.enable(false);
this.newButton.enable(true);
this.modifyButton.enable(true);
this.deleteButton.enable(true);
}
private void isPresentRadioButtonActionPerformed(ActionEvent evt) {
this.scoreStuTextField.setEditable(true);
}
private void noPresentStuRadioButtonActionPerformed(ActionEvent evt) {
this.scoreStuTextField.setText("0");
this.scoreStuTextField.setEditable(false);
}
private void allButtonActionPerformed(ActionEvent evt) {
try {
initTable();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 退出成绩管理
* @param evt
*/
private void exitButtonActionPerformed(ActionEvent evt) {
this.dispose();
}
/**
* 修改学生成绩
*
* @param evt
*/
private void modifyButtonActionPerformed(ActionEvent evt) {
// 建立一个JDBC对象
JdbcConnct jdbcConnection = new JdbcConnct();
// 定义鼠标点击行
int nrow = 0;
// 定义boolean型变量,用户是否点击行,初始化为False
boolean bisRight = false;
// 定义SQL语句
String sql = "";
// 更新行数 int型 updateLine
int updateLine = 0;
// 获取鼠标点击行
nrow = this.stuScoreTable.getSelectedRow();
// 判断用户是否选择
if (nrow < 0) {
bisRight = false;
JOptionPane.showMessageDialog(this, "您没有选择数据!在修改前请选择一条数据!");
} else {
bisRight = true;
}
if (bisRight) {
sql = "update stuscore set score = " + getScore()
+ " where stuscore.studentid ="
+ this.stuScoreTable.getValueAt(nrow, 0);
// 更新数据库
try {
updateLine = jdbcConnection.updateData(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
if (updateLine > 0) {
// 更新用户界面数据
this.stuScoreTableModel.setValueAt(getScore(), nrow, 5);
JOptionPane.showMessageDialog(this, "修改成功!");
} else {
JOptionPane.showMessageDialog(this, "修改失败!请重试!");
}
}
/**
* 删除
*
* @param evt
*/
private void deleteStuButtonActionPerformed(ActionEvent evt) {
//更新影响行数
int updateLine;
int nrow = this.stuScoreTable.getSelectedRow();
if (nrow != -1) {
try {
Connection conn = null;
String sql = "delete from stuscore "
+ "where stuscore.studentid ="
+ this.stuScoreTable.getValueAt(nrow, 0);
conn = JdbcConnct.fetchConnection();
Statement stmt = conn.createStatement();
updateLine = stmt.executeUpdate(sql);
if (updateLine > 0) {
stuScoreTableModel.removeRow(nrow);
stuScoreTable.setModel(stuScoreTableModel);
JOptionPane.showMessageDialog(this, "删除成功!!");
} else {
JOptionPane.showMessageDialog(this, "删除失败!!");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "没有选定行!!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -