📄 stuscore.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Stuscore extends JFrame{
private String name;
private int id;
private JLabel lbl_cno,lbl_sno,lbl_name,lbl_sname,lbl_score;
private JComboBox cmb_cno,cmb_sno;
private JTextField txt_score;
private JButton btn_query,btn_back,btn_ok,btn_mode;
private JPanel panel;
private boolean isAdd=true;
private boolean haveCourse=false;
private int cou_id;
private int stu_id;
public Stuscore(String name,int id){
super("成绩管理系统");
this.name=name;
this.id=id;
lbl_cno=new JLabel("课号");
lbl_sno=new JLabel("学号");
lbl_name=new JLabel("姓名");
lbl_sname=new JLabel();
lbl_score=new JLabel("成绩");
cmb_cno=new JComboBox();
cmb_sno=new JComboBox();
btn_query=new JButton("查询");
btn_back=new JButton("返回");
btn_ok=new JButton("提交");
btn_mode=new JButton("添加模式");
txt_score=new JTextField();
try{
Connection conn=null;
DateSource date=new DateSource();
conn=date.getconnection();
String sql="SELECT id FROM course WHERE tech_id=?";
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1,id);
ResultSet rs=stmt.executeQuery();
while(rs.next()){
cmb_cno.addItem(rs.getString(1));
haveCourse=true;
}
rs.close();
conn.close();
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,exc.getMessage(),"错误",1);
}
if(!haveCourse)cmb_cno.addItem("您没有课程");
else{
try{
Connection conn=null;
DateSource date=new DateSource();
conn=date.getconnection();
String sql="SELECT name FROM course WHERE id=?";
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1,Integer.parseInt(cmb_cno.getItemAt(0).toString()));
ResultSet rs=stmt.executeQuery();
if(rs.next())cmb_cno.setToolTipText(rs.getString(1));
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,exc.getMessage(),"错误",1);
}
}
panel=new JPanel();
panel.setLayout(new GridLayout(6,2,10,10));
panel.add(lbl_cno);
panel.add(cmb_cno);
panel.add(btn_query);
panel.add(btn_back);
panel.add(lbl_sno);
panel.add(cmb_sno);
panel.add(lbl_name);
panel.add(lbl_sname);
panel.add(lbl_score);
panel.add(txt_score);
panel.add(btn_ok);
panel.add(btn_mode);
btn_ok.hide();
if(!haveCourse)btn_query.hide();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,25,30));
getContentPane().add(panel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
pack();
setLocation(400,250);
setVisible(true);
btn_back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
returnMenu();
}
});
btn_query.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cmb_sno.removeAllItems();
cmb_sno.addItem("请选择学生");
btn_ok.hide();
lbl_sname.setText("");
txt_score.setText("");
repaint();
try{
int index=cmb_cno.getSelectedIndex();
cou_id=Integer.parseInt(cmb_cno.getItemAt(index).toString());
String sql="";
if(isAdd)sql="SELECT stu_id from score where Cou_id=? and score is NULL ";
else sql="SELECT stu_id from score where Cou_id=? and score is NOT NULL ";
Connection conn=null;
DateSource date=new DateSource();
conn=date.getconnection();
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1,cou_id);
ResultSet rs=stmt.executeQuery();
while(rs.next()){
cmb_sno.addItem(rs.getString(1));
}
rs.close();
conn.close();
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,exc.getMessage(),"错误",1);
}
}
});
cmb_cno.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
int index=cmb_cno.getSelectedIndex();
if(haveCourse){
if(event.getStateChange()==ItemEvent.SELECTED){
try{
Connection conn=null;
DateSource date=new DateSource();
conn=date.getconnection();
String sql="SELECT name FROM course WHERE id=?";
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setInt(1,Integer.parseInt(cmb_cno.getItemAt(index).toString()));
ResultSet rs=stmt.executeQuery();
if(rs.next())cmb_cno.setToolTipText(rs.getString(1));
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,exc.getMessage(),"错误",1);
}
}
}
}
});
cmb_sno.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
int index=cmb_sno.getSelectedIndex();
if(index!=0){
if(event.getStateChange()==ItemEvent.SELECTED){
stu_id=Integer.parseInt(cmb_sno.getItemAt(index).toString());
try{
Connection conn1=null;
DateSource date1=new DateSource();
conn1=date1.getconnection();
String sql="SELECT name from students where id=?";
PreparedStatement stmt1=conn1.prepareStatement(sql);
stmt1.setInt(1,stu_id);
ResultSet rs1=stmt1.executeQuery();
if(rs1.next())lbl_sname.setText(rs1.getString(1));
rs1.close();
conn1.close();
btn_ok.show();
if(!isAdd){
Connection conn2=null;
DateSource date2=new DateSource();
conn2=date2.getconnection();
sql="SELECT score from score where Stu_id=? and Cou_id=?";
PreparedStatement stmt2=conn2.prepareStatement(sql);
stmt2.setInt(1,stu_id);
stmt2.setInt(2,cou_id);
ResultSet rs2=stmt2.executeQuery();
if(rs2.next())txt_score.setText(rs2.getString(1));
rs2.close();
conn2.close();
}
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,exc.getMessage(),"错误",1);
}
}
}
}
});
btn_ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if(Double.parseDouble(txt_score.getText())>100||Double.parseDouble(txt_score.getText())<0){
JOptionPane.showMessageDialog(cmb_sno,"成绩范围为0~100","错误",1);
}
else{
Connection conn=null;
DateSource date=new DateSource();
conn=date.getconnection();
String sql="UPDATE score SET score=? where Stu_id=? and Cou_id=?";
PreparedStatement stmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.setDouble(1,Double.parseDouble(txt_score.getText()));
stmt.setInt(2,stu_id);
stmt.setInt(3,cou_id);
stmt.executeUpdate();
JOptionPane.showMessageDialog(cmb_sno,"提交成功","提示",1);
conn.close();
if(isAdd){
cmb_sno.removeItemAt(cmb_sno.getSelectedIndex());
cmb_sno.setSelectedIndex(0);
lbl_sname.setText("");
txt_score.setText("");
btn_ok.hide();
repaint();
}
}
}catch(Exception exc){
JOptionPane.showMessageDialog(cmb_sno,"成绩范围为0~100","错误",1);
txt_score.setText("");
}
}
});
btn_mode.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(isAdd){
isAdd=false;
btn_mode.setText("修改模式");
btn_ok.hide();
txt_score.setText("");
lbl_sname.setText("");
cmb_sno.removeAllItems();
repaint();
}
else{
isAdd=true;
btn_mode.setText("添加模式");
btn_ok.hide();
txt_score.setText("");
lbl_sname.setText("");
cmb_sno.removeAllItems();
repaint();
}
}
});
txt_score.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
btn_ok.doClick();
}
});
}
private void returnMenu(){
Cmenu user=new Cmenu("老师",name,id);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -