package studentSystemGUI;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class StudentSystemGUI implements ActionListener {
JFrame f = null;
public StudentSystemGUI() {
f = new JFrame("学生信息系统");
Container contentPane = f.getContentPane();
JPanel p = new JPanel();
contentPane.add(p, BorderLayout.CENTER);
p.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.CYAN, 2), "学生信息系统",
TitledBorder.CENTER, TitledBorder.TOP));
JButton regedit = new JButton("学生登记");
JButton exit = new JButton("退出系统");
regedit.setToolTipText("学生登记");
exit.setToolTipText("退出系统");
p.add(regedit);
p.add(exit);
regedit.addActionListener(this);
exit.addActionListener(this);
JMenuBar jb = new JMenuBar();
JMenu selection = new JMenu("选项(s)");
JMenu system = new JMenu("系统(y)");
selection.setMnemonic('s');
system.setMnemonic('y');
JMenuItem rt = new JMenuItem("学生登记");
rt.setMnemonic('t');
JMenuItem count = new JMenuItem("学生总人数(c)");
count.setMnemonic('c');
JMenuItem st = new JMenuItem("学生查询(d)");
JMenuItem et = new JMenuItem("退出系统");
et.setMnemonic('e');
st.setMnemonic('d');
selection.add(rt);
selection.add(count);
selection.add(st);
system.add(et);
jb.add(selection);
jb.add(system);
f.setJMenuBar(jb);
rt.addActionListener(this);
count.addActionListener(this);
st.addActionListener(this);
et.addActionListener(this);
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setDefaultLookAndFeelDecorated(true);
f.addWindowListener(new WindowAdapter() {
@SuppressWarnings("unused")
public void windowCloing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("学生登记")) {
new StudentRegedit(f);
}
else if(cmd.equals("退出系统")){
System.exit(0);
}
else if (cmd.equals("学生总人数(c)")) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SIS",
"creactive", "confident");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SIT");
int i = 0;
while (rs.next())
i = i + 1;
JOptionPane.showMessageDialog(f, "共有" + i + "名学生");
stmt.close();
rs.close();
} catch (Exception ex) {
}
} else if (cmd.equals("学生查询(d)")) {
new StudentChaxun(f);
}
}
public static void main(String[] args) {
new StudentSystemGUI();
}
}
class StudentRegedit implements ActionListener {
JDialog dialog;
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t4 = new JTextField();
JTextField t5 = new JTextField();
JTextField t6 = new JTextField();
JTextField t7 = new JTextField();
JTextField t8 = new JTextField();
@SuppressWarnings("deprecation")
StudentRegedit(JFrame f) {
dialog = new JDialog(f, "学生登记", true);
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(new GridLayout(9, 2));
dialogPane.add(new JLabel("排名", SwingConstants.CENTER));
dialogPane.add(t1);
dialogPane.add(new JLabel("学号", SwingConstants.CENTER));
dialogPane.add(t2);
dialogPane.add(new JLabel("姓名", SwingConstants.CENTER));
dialogPane.add(t3);
dialogPane.add(new JLabel("高等数学I", SwingConstants.CENTER));
dialogPane.add(t4);
dialogPane.add(new JLabel("计算机科学技术导论", SwingConstants.CENTER));
dialogPane.add(t5);
dialogPane.add(new JLabel("形势与政策", SwingConstants.CENTER));
dialogPane.add(t6);
dialogPane.add(new JLabel("专业概论", SwingConstants.CENTER));
dialogPane.add(t7);
dialogPane.add(new JLabel("体育", SwingConstants.CENTER));
dialogPane.add(t8);
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
dialogPane.add(b1);
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
dialog.setBounds(400,250,400,250);
dialog.show();
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("确定")) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SIS",
"creactive", "confident");
Statement stmt = con.createStatement();
int 排名= Integer.parseInt(t1.getText());
String 学号= t2.getText();
String 姓名= t3.getText();
int 高等数学I= Integer.parseInt(t4.getText());
int 计算机科学技术导论= Integer.parseInt(t1.getText());
int 形势与政策= Integer.parseInt(t1.getText());
int 专业概论= Integer.parseInt(t1.getText());
int 体育= Integer.parseInt(t1.getText());
String SQLOrder = "INSERT SIT VALUES(" + 排名+ "," + 学号 + "," +姓名
+ "," + 高等数学I + "," +计算机科学技术导论 + "," +形势与政策+ "," +专业概论+ "," +体育+ ")";
stmt.executeUpdate(SQLOrder);
stmt.close();
con.close();
} catch (Exception ex) {
}
} else if (cmd.equals("取消")) {
dialog.dispose();
}
}
}
class StudentChaxun{
JDialog dialog;
@SuppressWarnings("deprecation")
StudentChaxun(JFrame f){
dialog=new JDialog(f,"学生情况",true);
Container dialogPane=dialog.getContentPane();
dialogPane.setLayout(new GridLayout(9,2));
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:SIS",
"creactive", "confident");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SIT");
dialogPane.add(f,"排名" + " " + "学号"
+ " " + "姓名" + " " + "高等数学I"
+ " " + "计算机科学技术导论" + " " + "形势与政策" + " " + "专业概论"
+ " " + "体育");
while(rs.next()){
dialogPane.add(f,rs.getInt("排名") + " ");
dialogPane.add(f,rs.getString("学号") + " ");
dialogPane.add(f,rs.getString("姓名") + " ");
dialogPane.add(f,rs.getInt("高等数学I") + " ");
dialogPane.add(f,rs.getInt("计算机科学技术导论") + " ");
dialogPane.add(f,rs.getInt("形势与政策") + " ");
dialogPane.add(f,rs.getInt("专业概论") + " ");
dialogPane.add(f,rs.getInt("体育") + " ");
}
;
stmt.close();
rs.close();
} catch (Exception ex) {
}
dialog.setBounds(400,250,400,250);
dialog.show();
}
}