📄 inputscore.java~63~
字号:
package scoremis;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class InputScore extends JFrame{
String kc,bj;
JPanel p=new JPanel(); //构造个各组件
JLabel l1=new JLabel("课程:");
JComboBox course=new JComboBox();
JLabel l2=new JLabel("班级:");
JComboBox tgreade=new JComboBox();
JLabel l3=new JLabel("学期:");
JComboBox term=new JComboBox();
JButton sure=new JButton("查询");
JButton tijiao=new JButton("提交");
JButton quit=new JButton("退出");
String tname="",psd,sql="";
ResultSet rs=null;
Vector tempvector=new Vector(1,1);
DefaultTableModel model=new DefaultTableModel();
JTable dbtable=new JTable(model);
JScrollPane jScrollPane1=new JScrollPane(dbtable);
public InputScore(String uname,String psd) {
try{
tname=uname;
this.psd=psd;
jbInit();
}catch(Exception exception){
exception.printStackTrace();
}
}
private void jbInit() throws Exception{
getContentPane().setLayout(new BorderLayout());//添加各组件
l1.setFont(new java.awt.Font("Dialog", 0, 12));
course.setFont(new java.awt.Font("Dialog", 0, 12));
l2.setFont(new java.awt.Font("Dialog", 0, 12));
tgreade.setFont(new java.awt.Font("Dialog", 0, 12));
l3.setFont(new java.awt.Font("Dialog", 0, 12));
term.setFont(new java.awt.Font("Dialog", 0, 12));
sure.setFont(new java.awt.Font("Dialog", 0, 12));
tijiao.setFont(new java.awt.Font("Dialog", 0, 12));
quit.setFont(new java.awt.Font("Dialog", 0, 12));
jScrollPane1.setFont(new java.awt.Font("Dialog", 0, 12));
p.add(l1);p.add(course);p.add(l2);p.add(tgreade);p.add(l3);p.add(term);
p.add(sure);p.add(tijiao);p.add(quit);
sure.addActionListener(new chaxun());//给组件添加监视器
tijiao.addActionListener(new chaxun());
quit.addActionListener(new chaxun());
model.addColumn("学号");//给表格添加各列
model.addColumn("姓名");
model.addColumn("成绩");
getContentPane().add(jScrollPane1,"Center");
this.getContentPane().add(p,BorderLayout.NORTH);
course.addItem("请选择课程");
course.addActionListener(new cquery());
tgreade.addItem("请选择班级");
term.addItem("请选择学期");
sql="select distinct T_C.cname from T_C,teacher where teacher.name='"+tname+"' and teacher.ID=T_C.tID";
try{ //给课程添加项目
dbconn db=new dbconn();
rs=db.Query(sql);
while(rs.next()){
course.addItem(rs.getString(1));
}
}catch(Exception er){System.out.println(er.toString());}
}
class cquery implements ActionListener{ //给”班级“和”学期“添加项目
public void actionPerformed(ActionEvent e){
dbconn db=new dbconn();
if(e.getSource()==course){
kc=(String)course.getSelectedItem();
sql="select T_C.Cgreade,T_C.Term from T_C,teacher where teacher.name='"+tname+"' and teacher.ID=T_C.tID and T_C.cname='"+kc+"'";
try{
tgreade.removeAllItems();
tgreade.addItem("请选择班级");
rs=db.Query(sql);
while(rs.next()){
tgreade.addItem(rs.getString(1));
term.addItem(rs.getString(2));
}
}catch(Exception e2){System.out.println(e2.toString()); }
}
}
}
class chaxun implements ActionListener{ //用于处理查询事件:查询学生
public void actionPerformed(ActionEvent e){
dbconn db=new dbconn();
kc=(String)course.getSelectedItem();
bj=(String)tgreade.getSelectedItem();
sql="select * from S_C where CID='"+kc+"' and Tgreade='"+bj+"'";
boolean luru=false;
try{
ResultSet rs=db.Query(sql);
if(rs.next()){
luru=true;
}
}catch(Exception e1){System.out.println(e1.toString());}
if(e.getSource()==sure){ //录入前先查询
if(luru==true){
//JOptionPane.showMessageDialog(null,"成绩未录入!!");
JOptionPane.showMessageDialog(null,"成绩已经录入,不能再重新录入!!");
}else {
sql="select ID,name from student where greade='"+bj+"'";
//sql="select student.ID,student.name,S_C.score from S_C,student where student.greade='"+bj+"' and student.greade=S_C.Tgreade";
try{ //删除表格中原有的数据
int j=model.getRowCount();
if(j>0){
for(int i=0;i<j;i++)
model.removeRow(0);
}
rs=db.Query(sql);
while(rs.next()){
tempvector=new Vector(1,1);
tempvector.add(rs.getString(1));
tempvector.add(rs.getString(2));
model.addRow(tempvector);
}
}catch(Exception e2){System.out.println(e2.toString());}
}
}else if(e.getSource()==tijiao){ //用于处理 提交 事件:录入成绩
int n=model.getRowCount();
PreparedStatement updatestmt;
String xq=(String)term.getSelectedItem();
if(luru==false){
sql="insert into S_C values(?,?,'"+bj+"','"+xq+"','"+kc+"',?)";
try{
if (model.getValueAt(n-1, 2)!=null) {
updatestmt = db.conn.prepareStatement(sql);
for (int i=0;i<n;i++){
updatestmt.clearParameters();
updatestmt.setString(1, (String) model.getValueAt(i, 0));
updatestmt.setString(2, (String) model.getValueAt(i, 1));
updatestmt.setFloat(3, Float.parseFloat((String)model.getValueAt(i, 2)));
updatestmt.executeUpdate();
}JOptionPane.showMessageDialog(null, "添加成功");
}else
JOptionPane.showMessageDialog(null, "未录入完数据");
db.close();
}catch(Exception e3){
System.out.println(e3.toString());
}
}
}else if(e.getSource()==quit){ //处理”退出“事件
setVisible(false);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -