📄 inputscore.java
字号:
package scoremis;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.table*;
public class InputScore extends JFrame
{
String kc,bj;
JPanel p=new JPanel(); //构造各个组件
JLabel l1=new JLabel("课程");
JComboBox course=new JComoboBox();
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();
JTabel dbtable=new JTabel(model);
JScrollPane jScrollPanel=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());//设置布局管理器的布局方式为边框布局方式
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());
getContentPane().add(p,"North");
model.addColumn("学号");//为表格添加各个列并命名
model.addColumn("姓名");
model.addColumn("成绩");、
getContentPane().add(jScrollPanel,"Center");
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());
}
}
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 Cname='" + kc + "' and Tgreade='"+ bj +"'";
boolean luru=false;
try
{
ResultSet rs=db.Query(sql);
if(rs.next())
{
luru=true;
}
}
catch(Exception el)
{
System.out.println(el.toString);
}
if(e.getSource()==sure)//查询按钮获得焦点的事件处理
{
if(luru==true)
{
JOptionPane.showMessageDialog(null,"成绩已经录入,不能从新录入");
}
else
{
sql="select ID,name from student where greade='"+bj+"'";
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(Excepton 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) //如果"退出"按钮获得焦点,则窗口的可见性设置为false
{
setVisible(false);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -