📄 chengjiguanli.java
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class chengjiguanli extends JFrame implements ActionListener
{
Connection con;
ResultSet rs;
Statement st;
JButton button1,button2,button3;
JTextField text1,text3;
Choice kc;
String sid1=new String();
String crname2=new String();
String studentid1=new String();
int count=0;
public void chengjiguanli()
{
text1=new JTextField(10);
kc=new Choice();
text3=new JTextField(10);
button1=new JButton("查询");
button1.addActionListener(this);
button2=new JButton("添加");
button2.addActionListener(this);
button3=new JButton("退出");
button3.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("成绩管理页面",JLabel.CENTER));
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("学号:",JLabel.CENTER));
box2.add(text1);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("课程:",JLabel.CENTER));
box3.add(kc);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("成绩:",JLabel.CENTER));
box4.add(text3);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(button1);
pSouth.add(button2);
pSouth.add(button3);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,50,300,200);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM xuanke");
}catch(SQLException el){}//连接数据库
try{//将后台数据库中的学生所选的课程名显示在kc这个按钮上
String id=new String();
id=text1.getText();
while(rs.next())
{String id1=new String();
id1=rs.getString("studentid");
if(id.equals(id1))
{String kechengming=new String();
kechengming=rs.getString("crname");
kc.add(kechengming);
count++;
}
}
}catch(Exception exx){}
if(count==0){JOptionPane.showMessageDialog(null,"该生没有选修任何课程!");}
}
if(e.getSource()==button2)
{try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM xuanke");
}
catch(SQLException el){}//连接数据库
String id1=new String();//提取学生学号
id1=text1.getText();
int count=0;
String crname1=new String();
crname1=kc.getSelectedItem();
String crcode1=new String();
crcode1=text3.getText();
try{
while(rs.next())
{crname2=rs.getString("crname");
studentid1=rs.getString("studentid");
if((id1.equals(studentid1))&&(crname1.equals(crname2)))
{String strSQL3 ="update xuanke set crcode='"+crcode1+"'"+"where studentid='"+id1+"'and crname='"+crname1+"'";
try
{
st.executeUpdate(strSQL3);
}catch(Exception exx){}
JOptionPane.showMessageDialog(null,"添加成功!");
text3.setText("");
}
}
}catch(Exception exxx){}
}
if(e.getSource()==button3){System.exit(0);}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -