[Java] 学生成绩管理系统 java桌面版 →→→→→进入此内容的聊天室

来自 , 2019-06-04, 写在 Java, 查看 152 次.
URL http://www.code666.cn/view/b6edc1cd
  1. /**
  2.  * @(#)StudentSystem.java
  3.  *
  4.  *
  5.  * @author
  6.  * @version 1.00 2012/10/31
  7.  */
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.sql.*;
  12. import javax.swing.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.awt.event.*;
  16. import javax.swing.*;
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.sql.*;
  20. import javax.swing.table.JTableHeader;
  21. import javax.swing.*;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.sql.*;
  25. import javax.swing.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.sql.*;
  29. import javax.swing.*;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import java.sql.*;
  33. import javax.swing.*;
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. import java.sql.*;
  37. import javax.swing.table.DefaultTableModel;
  38. import javax.swing.table.JTableHeader;
  39. import javax.swing.*;
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.sql.*;
  43.  
  44. class AddForm extends JFrame implements ActionListener {
  45.         JLabel labName = new JLabel("学号:");
  46.         JLabel labDate = new JLabel("出生日期:");
  47.         JLabel labScore = new JLabel("成绩:");
  48.         JTextField txtName = new JTextField(20);
  49.         JTextField txtDate = new JTextField(18);
  50.         JTextField txtScore = new JTextField(20);
  51.         JButton btnOk = new JButton("确定");
  52.         JButton btnClear = new JButton("清空");
  53.         JPanel pan = new JPanel();
  54.         JPanel pan1 = new JPanel();
  55.         JPanel pan2 = new JPanel();
  56.         JPanel pan3 = new JPanel();
  57.         JPanel pan4 = new JPanel();
  58.  
  59.         Connection cnn;
  60.         Statement stm;
  61.         ResultSet rs;
  62.  
  63.         AddForm() {
  64.                 super("添加数据");
  65.                 setSize(400, 300);
  66.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  67.                 pan.setBorder(BorderFactory.createEtchedBorder());
  68.                 pan1.add(labName);
  69.                 pan1.add(txtName);
  70.                 pan2.add(labDate);
  71.                 pan2.add(txtDate);
  72.                 pan3.add(labScore);
  73.                 pan3.add(txtScore);
  74.                 pan4.add(btnOk);
  75.                 pan4.add(btnClear);
  76.                 pan.setLayout(new GridLayout(3, 1));
  77.                 pan.add(pan1);
  78.                 pan.add(pan2);
  79.                 pan.add(pan3);
  80.                 getContentPane().add(pan, "Center");
  81.                 getContentPane().add(pan4, "South");
  82.                 btnOk.addActionListener(this);
  83.                 btnClear.addActionListener(this);
  84.                 setVisible(true);
  85.                 txtName.requestFocus();
  86.  
  87.         }
  88.  
  89.         public void actionPerformed(ActionEvent ae) {
  90.                 if (ae.getSource() == btnClear) {
  91.                         txtName.setText("");
  92.                         txtDate.setText("");
  93.                         txtScore.setText("");
  94.                         txtName.requestFocus();
  95.                 } else if (ae.getSource() == btnOk) {
  96.                         String strName = txtName.getText();
  97.                         String strDate = txtDate.getText();
  98.                         String strScore = txtScore.getText();
  99.                         if (strName.equals(""))
  100.                                 JOptionPane.showMessageDialog(this, "学号不能为空!", "警告",
  101.                                                 JOptionPane.ERROR_MESSAGE);
  102.                         else if (strDate.equals(""))
  103.                                 JOptionPane.showMessageDialog(this, "出生日期不能为空!", "警告",
  104.                                                 JOptionPane.ERROR_MESSAGE);
  105.                         else if (strScore.equals(""))
  106.                                 JOptionPane.showMessageDialog(this, "成绩不能为空!", "警告",
  107.                                                 JOptionPane.ERROR_MESSAGE);
  108.                         else {
  109.                                 try {
  110.                                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  111.                                 } catch (ClassNotFoundException ex) {
  112.                                         ex.printStackTrace();
  113.                                 }
  114.                                 try {
  115.                                         cnn = DriverManager.getConnection("Jdbc:Odbc:MyDB");
  116.                                         stm = cnn.createStatement();
  117.                                 } catch (SQLException ex) {
  118.                                         ex.printStackTrace();
  119.                                 }
  120.                                 try {
  121.                                         rs = stm.executeQuery("select * from 成绩表 where 学号='"
  122.                                                         + strName + "'");
  123.                                         if (rs.next()) {
  124.                                                 JOptionPane.showMessageDialog(this, "对不起,该成绩信息已存在!");
  125.                                         } else // 否则插入记录
  126.                                         {
  127.                                                 // System.out.println("insert into 成绩表 values('"+strName+"',#"+strDate+"#,"+strScore+")");
  128.                                                 stm.executeUpdate("insert into 成绩表 values('" + strName
  129.                                                                 + "','" + strDate + "'," + strScore + ")");
  130.                                                 JOptionPane.showMessageDialog(null, "记录已经成功添加!");
  131.                                         }
  132.                                         // 断开连接
  133.                                         stm.close();
  134.                                         cnn.close();
  135.                                 } catch (SQLException ex) {
  136.                                         System.out.println("SQLException:" + ex.getMessage());
  137.                                 }
  138.  
  139.                         }
  140.                 }
  141.         }
  142.  
  143.         public static void main(String[] args) {
  144.                 new AddForm();
  145.         }
  146. }
  147.  
  148. class BrowseForm extends JFrame {
  149.         String[] str = { "学号", "出生日期", "成绩" };
  150.         Object[][] data;
  151.         JTable table;
  152.         JTableHeader head;
  153.         JScrollPane jsp;
  154.         Connection conn;
  155.         Statement stmt;
  156.         ResultSet rs;
  157.  
  158.         BrowseForm() {
  159.                 super("浏览数据");
  160.                 setSize(400, 300);
  161.                 int i = 0, j = 0;
  162.                 int row;
  163.                 try {
  164.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  165.                         conn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
  166.                         stmt = conn.createStatement();
  167.  
  168.                         rs = stmt.executeQuery("select COUNT(*) as a from 成绩表");
  169.                         rs.next();
  170.                         row = rs.getInt("a");
  171.                         rs.close();
  172.                         data = new Object[row][3];
  173.                         rs = stmt.executeQuery("select * from 成绩表");
  174.                         while (rs.next()) {
  175.                                 data[i][j++] = rs.getString("学号");
  176.                                 data[i][j++] = rs.getDate("出生日期");
  177.                                 data[i][j] = new Integer(rs.getInt("成绩"));
  178.                                 i++;
  179.                                 j = 0;
  180.                         }
  181.                         table = new JTable(data, str);
  182.                         head = table.getTableHeader();
  183.                         jsp = new JScrollPane(table);
  184.                         getContentPane().add(head, "North");
  185.                         getContentPane().add(jsp, "Center");
  186.                         rs.close();
  187.                         stmt.close();
  188.                         conn.close();
  189.                 } catch (Exception e) {
  190.                         e.printStackTrace();
  191.                 }
  192.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  193.                 setVisible(true);
  194.         }
  195.  
  196.         public static void main(String[] args) {
  197.                 new BrowseForm();
  198.         }
  199. }
  200.  
  201. class DeleteForm extends JFrame implements ActionListener {
  202.         JLabel labName = new JLabel("学号:");
  203.         JLabel labDate = new JLabel("出生日期:");
  204.         JLabel labScore = new JLabel("成绩:");
  205.         JTextField txtName = new JTextField(20);
  206.         JTextField txtDate = new JTextField(18);
  207.         JTextField txtScore = new JTextField(20);
  208.         JButton btnDel = new JButton("删除");
  209.         JButton btnCancel = new JButton("取消");
  210.         JButton btnQuery = new JButton("查询");
  211.         JPanel pan = new JPanel();
  212.         JPanel pan1 = new JPanel();
  213.         JPanel pan2 = new JPanel();
  214.         JPanel pan3 = new JPanel();
  215.         JPanel pan4 = new JPanel();
  216.  
  217.         Connection cnn;
  218.         Statement stm;
  219.         ResultSet rs;
  220.  
  221.         DeleteForm() {
  222.                 super("删除数据");
  223.                 setSize(400, 300);
  224.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  225.                 pan.setBorder(BorderFactory.createEtchedBorder());
  226.                 pan1.add(labName);
  227.                 pan1.add(txtName);
  228.                 pan2.add(labDate);
  229.                 pan2.add(txtDate);
  230.                 pan3.add(labScore);
  231.                 pan3.add(txtScore);
  232.                 pan4.add(btnQuery);
  233.                 pan4.add(btnDel);
  234.                 pan4.add(btnCancel);
  235.                 pan.setLayout(new GridLayout(3, 1));
  236.                 pan.add(pan1);
  237.                 pan.add(pan2);
  238.                 pan.add(pan3);
  239.                 getContentPane().add(pan, "Center");
  240.                 getContentPane().add(pan4, "South");
  241.                 btnQuery.addActionListener(this);
  242.                 btnDel.addActionListener(this);
  243.                 btnCancel.addActionListener(this);
  244.                 btnDel.setEnabled(false);
  245.                 txtDate.setEditable(false);
  246.                 txtScore.setEditable(false);
  247.                 setVisible(true);
  248.                 txtName.requestFocus();
  249.  
  250.         }
  251.  
  252.         public void actionPerformed(ActionEvent ae) {
  253.                 /*
  254.                  * if(ae.getSource()==btnCancel){ try { if(stm!=null) stm.close();
  255.                  * if(cnn!=null) cnn.close(); } catch (SQLException ex) {
  256.                  * ex.printStackTrace(); } this.dispose(); } else
  257.                  * if(ae.getSource()==btnQuery){ try{
  258.                  * Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  259.                  * cnn=DriverManager.getConnection("jdbc:odbc:MyDB","","");
  260.                  * stm=cnn.createStatement(); rs=stm.executeQuery("select * from 成绩表
  261.                  * where 学号 ='"+txtName.getText()+"'"); if(rs.next()){
  262.                  * txtName.setText(rs.getString("学号"));
  263.                  * txtDate.setText(rs.getDate("出生日期").toString()); txtScore.setText(new
  264.                  * Integer(rs.getInt("成绩")).toString ()); btnDel.setEnabled(true);
  265.                  * }else{ JOptionPane.showMessageDialog(this,"不存在该记录!");
  266.                  * btnDel.setEnabled(false); txtName.setText(""); txtScore.setText("");
  267.                  * txtDate.setText(""); } }catch(Exception e){ e.printStackTrace(); }
  268.                  * }else if(ae.getSource()==btnDel){ try {
  269.                  * if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog
  270.                  * (this,"确定要删除该记录?","信息",JOptionPane.YES_NO_OPTION)){
  271.                  * stm.executeUpdate("delete from 成绩表 where 学号
  272.                  * ='"+txtName.getText()+"'"); btnDel.setEnabled(false);
  273.                  * txtName.setText(""); txtScore.setText(""); txtDate.setText(""); } }
  274.                  * catch (SQLException ex) { ex.printStackTrace(); } }
  275.                  */
  276.         }
  277.  
  278.         public static void main(String[] args) {
  279.                 new DeleteForm();
  280.         }
  281. }
  282.  
  283. class LoginForm extends JFrame implements ActionListener {
  284.         JLabel labName = new JLabel("姓名");
  285.         JLabel labPwd = new JLabel("密码");
  286.         JTextField txtName = new JTextField(20);
  287.         JPasswordField txtPwd = new JPasswordField(20);
  288.         JButton btnOk = new JButton("确定");
  289.         JButton btnCancel = new JButton("取消");
  290.         JPanel pan = new JPanel();
  291.         JPanel pan1 = new JPanel();
  292.         JPanel pan2 = new JPanel();
  293.         JPanel pan3 = new JPanel();
  294.         JPanel pan4 = new JPanel();
  295.  
  296.         Connection cnn;
  297.         Statement stm;
  298.         ResultSet rs;
  299.  
  300.         LoginForm() {
  301.                 super("用户登录");
  302.                 setSize(300, 200);
  303.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  304.                 pan.setBorder(BorderFactory.createTitledBorder("登录"));
  305.                 pan.setLayout(new GridLayout(2, 1));
  306.                 pan1.add(labName);
  307.                 pan1.add(txtName);
  308.                 pan2.add(labPwd);
  309.                 pan2.add(txtPwd);
  310.                 pan.add(pan1);
  311.                 pan.add(pan2);
  312.                 pan3.add(btnOk);
  313.                 pan3.add(btnCancel);
  314.                 pan4.add(pan);
  315.                 getContentPane().add(pan4, "Center");
  316.                 getContentPane().add(pan3, "South");
  317.                 txtName.addActionListener(this);
  318.                 txtPwd.addActionListener(this);
  319.                 btnOk.addActionListener(this);
  320.                 btnCancel.addActionListener(this);
  321.                 setVisible(true);
  322.  
  323.                 try {
  324.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  325.                 } catch (ClassNotFoundException ex) {
  326.                         ex.printStackTrace();
  327.                 }
  328.                 try {
  329.                         cnn = DriverManager.getConnection("Jdbc:Odbc:MyDB");
  330.                         stm = cnn.createStatement();
  331.                 } catch (SQLException ex) {
  332.                         ex.printStackTrace();
  333.                 }
  334.                 txtName.requestFocus();
  335.  
  336.         }
  337.  
  338.         public void actionPerformed(ActionEvent ae) {
  339.                 if (ae.getSource() == txtName)
  340.                         txtPwd.requestFocus();
  341.                 else if (ae.getSource() == txtPwd)
  342.                         btnOk.requestFocus();
  343.                 else if (ae.getSource() == btnCancel) {
  344.                         txtName.setText("");
  345.                         txtPwd.setText("");
  346.                         txtName.requestFocus();
  347.                 } else if (ae.getSource() == btnOk) {
  348.                         String str = "select * from 用户表 where 用户名='" + txtName.getText()
  349.                                         + "'and 密码='" + new String(txtPwd.getPassword()) + "'";
  350.                         try {
  351.                                 rs = stm.executeQuery(str);
  352.                         } catch (SQLException ex) {
  353.                                 ex.printStackTrace();
  354.                         }
  355.                         try {
  356.                                 if (rs.next()) {
  357.                                         JOptionPane.showMessageDialog(this, "验证通过!", "信息",
  358.                                                         JOptionPane.INFORMATION_MESSAGE);
  359.                                         rs.close();
  360.                                         stm.close();
  361.                                         cnn.close();
  362.                                         new MainForm().setVisible(true);
  363.                                         this.dispose();
  364.                                 } else {
  365.                                         JOptionPane.showMessageDialog(this, "用户名或密码不正确!", "信息",
  366.                                                         JOptionPane.INFORMATION_MESSAGE);
  367.                                 }
  368.                         } catch (SQLException ex) {
  369.                                 ex.printStackTrace();
  370.                         }
  371.                 }
  372.         }
  373.  
  374.         public static void main(String[] args) {
  375.                 new LoginForm();
  376.         }
  377. }
  378.  
  379. class ModifyForm extends JFrame implements ActionListener {
  380.         JLabel labName = new JLabel("学号:");
  381.         JLabel labDate = new JLabel("出生日期:");
  382.         JLabel labScore = new JLabel("成绩:");
  383.         JTextField txtName = new JTextField(20);
  384.         JTextField txtDate = new JTextField(18);
  385.         JTextField txtScore = new JTextField(20);
  386.         JButton btnModify = new JButton("修改");
  387.         JButton btnCancel = new JButton("取消");
  388.         JButton btnQuery = new JButton("查询");
  389.         JPanel pan = new JPanel();
  390.         JPanel pan1 = new JPanel();
  391.         JPanel pan2 = new JPanel();
  392.         JPanel pan3 = new JPanel();
  393.         JPanel pan4 = new JPanel();
  394.  
  395.         Connection cnn;
  396.         Statement stm;
  397.         ResultSet rs;
  398.  
  399.         ModifyForm() {
  400.                 super("修改数据");
  401.                 setSize(400, 300);
  402.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  403.                 pan.setBorder(BorderFactory.createEtchedBorder());
  404.                 pan1.add(labName);
  405.                 pan1.add(txtName);
  406.                 pan2.add(labDate);
  407.                 pan2.add(txtDate);
  408.                 pan3.add(labScore);
  409.                 pan3.add(txtScore);
  410.                 pan4.add(btnQuery);
  411.                 pan4.add(btnModify);
  412.                 pan4.add(btnCancel);
  413.                 pan.setLayout(new GridLayout(3, 1));
  414.                 pan.add(pan1);
  415.                 pan.add(pan2);
  416.                 pan.add(pan3);
  417.                 getContentPane().add(pan, "Center");
  418.                 getContentPane().add(pan4, "South");
  419.                 btnQuery.addActionListener(this);
  420.                 btnModify.addActionListener(this);
  421.                 btnCancel.addActionListener(this);
  422.                 btnModify.setEnabled(false);
  423.                 txtDate.setEditable(false);
  424.                 txtScore.setEditable(false);
  425.                 setVisible(true);
  426.                 txtName.requestFocus();
  427.  
  428.         }
  429.  
  430.         public void actionPerformed(ActionEvent ae) {
  431.                 if (ae.getSource() == btnCancel) {
  432.                         try {
  433.                                 if (stm != null)
  434.                                         stm.close();
  435.                                 if (cnn != null)
  436.                                         cnn.close();
  437.                         } catch (SQLException ex) {
  438.                                 ex.printStackTrace();
  439.                         }
  440.                         this.dispose();
  441.                 } else if (ae.getSource() == btnQuery) {
  442.                         try {
  443.                                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  444.                                 cnn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
  445.                                 stm = cnn.createStatement();
  446.                                 rs = stm.executeQuery("select * from 成绩表 where 学号='"
  447.                                                 + txtName.getText() + "'");
  448.                                 if (rs.next()) {
  449.                                         txtName.setText(rs.getString("学号"));
  450.                                         txtScore.setText(new Integer(rs.getInt("成绩")).toString());
  451.                                         txtDate.setText(rs.getDate("出生日期").toString());
  452.                                         btnModify.setEnabled(true);
  453.                                         txtDate.setEditable(true);
  454.                                         txtScore.setEditable(true);
  455.                                 } else {
  456.                                         JOptionPane.showMessageDialog(this, "不存在该记录!");
  457.                                         btnModify.setEnabled(false);
  458.                                         txtName.setText("");
  459.                                         txtScore.setText("");
  460.                                         txtDate.setText("");
  461.                                         txtDate.setEditable(false);
  462.                                         txtScore.setEditable(false);
  463.                                 }
  464.                         } catch (Exception e) {
  465.                                 e.printStackTrace();
  466.                         }
  467.                 } else if (ae.getSource() == btnModify) {
  468.                         try {
  469.                                 System.out.println("Update  成绩表 set 出生日期=#" + txtDate.getText()
  470.                                                 + "#,成绩=" + txtScore.getText() + " where 学号='"
  471.                                                 + txtName.getText() + "'");
  472.                                 stm.executeUpdate("Update  成绩表 set 出生日期=#" + txtDate.getText()
  473.                                                 + "#,成绩=" + txtScore.getText() + " where 学号='"
  474.                                                 + txtName.getText() + "'");
  475.                                 JOptionPane.showMessageDialog(this, "记录修改完毕!");
  476.                                 btnModify.setEnabled(false);
  477.                                 txtName.setText("");
  478.                                 txtScore.setText("");
  479.                                 txtDate.setText("");
  480.                                 txtDate.setEditable(false);
  481.                                 txtScore.setEditable(false);
  482.                                 stm.close();
  483.                                 cnn.close();
  484.                         } catch (Exception e) {
  485.                                 e.printStackTrace();
  486.                         }
  487.                 }
  488.         }
  489.  
  490.         public static void main(String[] args) {
  491.                 new ModifyForm();
  492.         }
  493. }
  494.  
  495. class NumberQueryForm extends JFrame implements ActionListener {
  496.         JLabel labName = new JLabel("学号:");
  497.         JLabel labDate = new JLabel("出生日期:");
  498.         JLabel labScore = new JLabel("成绩:");
  499.         JTextField txtName = new JTextField(20);
  500.         JTextField txtDate = new JTextField(18);
  501.         JTextField txtScore = new JTextField(20);
  502.         JButton btnCancel = new JButton("取消");
  503.         JButton btnQuery = new JButton("查询");
  504.         JPanel pan = new JPanel();
  505.         JPanel pan1 = new JPanel();
  506.         JPanel pan2 = new JPanel();
  507.         JPanel pan3 = new JPanel();
  508.         JPanel pan4 = new JPanel();
  509.  
  510.         Connection cnn;
  511.         Statement stm;
  512.         ResultSet rs;
  513.  
  514.         NumberQueryForm() {
  515.                 super("按学号查询");
  516.                 setSize(400, 300);
  517.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  518.                 pan.setBorder(BorderFactory.createEtchedBorder());
  519.                 pan1.add(labName);
  520.                 pan1.add(txtName);
  521.                 pan2.add(labDate);
  522.                 pan2.add(txtDate);
  523.                 pan3.add(labScore);
  524.                 pan3.add(txtScore);
  525.                 pan4.add(btnQuery);
  526.                 pan4.add(btnCancel);
  527.                 pan.setLayout(new GridLayout(3, 1));
  528.                 pan.add(pan1);
  529.                 pan.add(pan2);
  530.                 pan.add(pan3);
  531.                 getContentPane().add(pan, "Center");
  532.                 getContentPane().add(pan4, "South");
  533.                 btnQuery.addActionListener(this);
  534.                 btnCancel.addActionListener(this);
  535.                 txtDate.setEditable(false);
  536.                 txtScore.setEditable(false);
  537.                 setVisible(true);
  538.                 txtName.requestFocus();
  539.  
  540.         }
  541.  
  542.         public void actionPerformed(ActionEvent ae) {
  543.                 if (ae.getSource() == btnCancel) {
  544.                         try {
  545.                                 if (stm != null)
  546.                                         stm.close();
  547.                                 if (cnn != null)
  548.                                         cnn.close();
  549.                         } catch (SQLException ex) {
  550.                                 ex.printStackTrace();
  551.                         }
  552.                         this.dispose();
  553.                 } else if (ae.getSource() == btnQuery) {
  554.                         try {
  555.                                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  556.                                 cnn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
  557.                                 stm = cnn.createStatement();
  558.                                 rs = stm.executeQuery("select * from 成绩表 where 学号='"
  559.                                                 + txtName.getText() + "'");
  560.                                 if (rs.next()) {
  561.                                         txtName.setText(rs.getString("学号"));
  562.                                         txtScore.setText(new Integer(rs.getInt("成绩")).toString());
  563.                                         txtDate.setText(rs.getDate("出生日期").toString());
  564.                                 } else {
  565.                                         JOptionPane.showMessageDialog(this, "不存在该记录!");
  566.                                         txtName.setText("");
  567.                                         txtScore.setText("");
  568.                                         txtDate.setText("");
  569.                                         txtName.requestFocus();
  570.                                 }
  571.                         } catch (Exception e) {
  572.                                 e.printStackTrace();
  573.                         }
  574.  
  575.                 }
  576.         }
  577.  
  578.         public static void main(String[] args) {
  579.                 new NumberQueryForm();
  580.         }
  581. }
  582.  
  583. class ScoreQueryForm extends JFrame implements ActionListener {
  584.         JLabel labScore = new JLabel("请输入成绩:");
  585.         JTextField txtScore = new JTextField(10);
  586.         JButton btnQuery = new JButton("查询");
  587.         JPanel pan1 = new JPanel();
  588.         JPanel pan2 = new JPanel();
  589.         String[] str = { "学号", "出生日期", "成绩" };
  590.         Object[][] data = new Object[10][3];
  591.         JTable table = new JTable(data, str);
  592.         JTableHeader head = table.getTableHeader();
  593.         JScrollPane jsp = new JScrollPane(table);
  594.         Connection conn;
  595.         Statement stmt;
  596.         ResultSet rs;
  597.  
  598.         ScoreQueryForm() {
  599.                 super("按成绩查询");
  600.                 setSize(400, 300);
  601.                 pan1.add(labScore);
  602.                 pan1.add(txtScore);
  603.                 pan1.add(btnQuery);
  604.                 getContentPane().add(pan1, "North");
  605.                 table = new JTable(data, str);
  606.                 pan2.setLayout(new BorderLayout());
  607.                 head = table.getTableHeader();
  608.                 jsp = new JScrollPane(table);
  609.                 pan2.add(head, "North");
  610.                 pan2.add(jsp, "Center");
  611.                 getContentPane().add(pan2, "Center");
  612.                 btnQuery.addActionListener(this);
  613.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  614.                 setVisible(true);
  615.         }
  616.  
  617.         public void actionPerformed(ActionEvent ae) {
  618.                 if (ae.getSource() == btnQuery) {
  619.                         int i, j, row;
  620.                         try {
  621.                                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  622.                                 conn = DriverManager.getConnection("jdbc:odbc:MyDB", "", "");
  623.                                 stmt = conn.createStatement();
  624.  
  625.                                 rs = stmt
  626.                                                 .executeQuery("select COUNT(*) as rowcount from 成绩表 where 成绩="
  627.                                                                 + txtScore.getText());
  628.                                 rs.next();
  629.                                 row = rs.getInt("rowcount");
  630.                                 rs.close();
  631.                                 data = new Object[row][3];
  632.                                 rs = stmt.executeQuery("select * from 成绩表 where 成绩="
  633.                                                 + txtScore.getText());
  634.                                 i = 0;
  635.                                 j = 0;
  636.                                 while (rs.next()) {
  637.                                         data[i][j++] = rs.getString("学号");
  638.                                         data[i][j++] = rs.getDate("出生日期");
  639.                                         data[i][j] = new Integer(rs.getInt("成绩"));
  640.                                         i++;
  641.                                         j = 0;
  642.                                 }
  643.  
  644.                                 pan2.removeAll();
  645.                                 getContentPane().remove(pan2);
  646.  
  647.                                 table = new JTable(data, str);
  648.                                 pan2.setLayout(new BorderLayout());
  649.                                 head = table.getTableHeader();
  650.                                 jsp = new JScrollPane(table);
  651.                                 pan2.add(head, "North");
  652.                                 pan2.add(jsp, "Center");
  653.                                 getContentPane().add(pan2, "Center");
  654.                                 this.validate();
  655.                                 rs.close();
  656.                                 stmt.close();
  657.                                 conn.close();
  658.                         } catch (Exception e) {
  659.                                 e.printStackTrace();
  660.                         }
  661.                 }
  662.         }
  663.  
  664.         public static void main(String[] args) {
  665.                 new ScoreQueryForm();
  666.         }
  667. }
  668.  
  669. class MyPanel extends JPanel {
  670.         Image img = Toolkit.getDefaultToolkit().getImage("c:/a.jpg");
  671.  
  672.         public void paint(Graphics g) {
  673.                 g.drawImage(img, 0, 0, this);
  674.         }
  675. }
  676.  
  677. class MainForm extends JFrame implements ActionListener {
  678.         JMenu mSystem = new JMenu("系统");
  679.         JMenuItem mExit = new JMenuItem("退出");
  680.         JMenu mOperate = new JMenu("数据操作");
  681.         JMenuItem mAdd = new JMenuItem("添加");
  682.         JMenuItem mDel = new JMenuItem("删除");
  683.         JMenuItem mModify = new JMenuItem("修改");
  684.         JMenuItem mBrowse = new JMenuItem("浏览");
  685.         JMenu mQuery = new JMenu("查询");
  686.         JMenuItem mNumber = new JMenuItem("按学号查询");
  687.         JMenuItem mScore = new JMenuItem("按成绩查询");
  688.         JMenu mHelp = new JMenu("帮助");
  689.         JMenuItem mAbout = new JMenuItem("关于");
  690.         JMenuBar mBar = new JMenuBar();
  691.  
  692.         MainForm() {
  693.                 super("学生成绩管理系统");
  694.                 setSize(700, 630);
  695.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  696.                 mSystem.add(mExit);
  697.                 mOperate.add(mAdd);
  698.                 mOperate.add(mDel);
  699.                 mOperate.add(mModify);
  700.                 mOperate.add(mBrowse);
  701.                 mQuery.add(mNumber);
  702.                 mQuery.add(mScore);
  703.                 mHelp.add(mAbout);
  704.                 mBar.add(mSystem);
  705.                 mBar.add(mOperate);
  706.                 mBar.add(mQuery);
  707.                 mBar.add(mHelp);
  708.                 setJMenuBar(mBar);
  709.                 mExit.addActionListener(this);
  710.                 mAdd.addActionListener(this);
  711.                 mDel.addActionListener(this);
  712.                 mModify.addActionListener(this);
  713.                 mBrowse.addActionListener(this);
  714.                 mNumber.addActionListener(this);
  715.                 mScore.addActionListener(this);
  716.                 mAbout.addActionListener(this);
  717.                 setContentPane(new MyPanel());
  718.                 setVisible(true);
  719.         }
  720.  
  721.         public void actionPerformed(ActionEvent ae) {
  722.                 if (ae.getSource() == mExit)
  723.                         System.exit(0);
  724.                 else if (ae.getSource() == mAbout)
  725.                         JOptionPane.showMessageDialog(this,
  726.                                         "学生管理系统 V1.0\n\n重庆邮电大学计算机学院\n\n2011年11月", "关于",
  727.                                         JOptionPane.INFORMATION_MESSAGE);
  728.                 else if (ae.getSource() == mAdd)
  729.                         new AddForm().setVisible(true);
  730.                 else if (ae.getSource() == mDel)
  731.                         new DeleteForm().setVisible(true);
  732.                 else if (ae.getSource() == mModify)
  733.                         new ModifyForm().setVisible(true);
  734.                 else if (ae.getSource() == mBrowse)
  735.                         new BrowseForm().setVisible(true);
  736.                 else if (ae.getSource() == mNumber)
  737.                         new NumberQueryForm().setVisible(true);
  738.                 else if (ae.getSource() == mScore)
  739.                         new ScoreQueryForm().setVisible(true);
  740.  
  741.         }
  742.  
  743.         public static void main(String[] args) {
  744.                 new MainForm();
  745.         }
  746. }
  747.  

回复 "学生成绩管理系统 java桌面版"

这儿你可以回复上面这条便签

captcha