程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> JAVA單機版管理系統源代碼package jettang;

JAVA單機版管理系統源代碼package jettang;

編輯:J2EE
package jettang; import java.lang.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import Java.sql.*; ////////////////////////////////////////////// //主類 public class STU { public STU() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String args[]) { new Frm_Main(); } private void jbInit() throws Exception { } } //主框架,數據庫 class Frm_Main implements ActionListener { //throws IOException public static final int EXIT_ON_CLOSE = 0; public JFrame frame; public Container c; public JMenuBar menuBar; public JMenu mainMenu1; public JMenu mainMenu2; public JMenu mainMenu3; public JMenuItem subMenu1[] = new JMenuItem[5]; public JMenuItem subMenu2[] = new JMenuItem[7]; public JMenuItem subMenu3[] = new JMenuItem[2]; JButton toolBarButton[] = new JButton[8]; public JToolBar toolBar; ////////////////////////////////////////////// String strTip[] = {"姓名查詢...", "添加信息...", "刪除...", "修改...", "修改確認...", "添加確認...", "刪除確認...", "關於產品信息:)"}; ////////////////////////////////////////////// // String id = new String(); String name = new String(); String sex = new String(); String age = new String(); String classid = new String(); String department = new String(); String call = new String(); ////////////////////////////////////////////// Connection con; ResultSet rs; Statement st; // ResultSetMetaData rsmd; // Vector columnHeads=new Vector(); // Vector rows = new Vector(); // Vector currentRow=new Vector(); /////////////////////////////////////////////// public JLabel classidL = new JLabel("班級"); public JLabel nameL = new JLabel("姓名"); public JLabel sexL = new JLabel("性別"); public JLabel ageL = new JLabel("年齡"); public JLabel departmentL = new JLabel("單位"); public JLabel callL = new JLabel("電話"); public JLabel tittleLabel = new JLabel(); ////////////////////////////////////////////// public JTextField classidT = new JTextField(); public JTextField nameT = new JTextField(); public JTextField sexT = new JTextField(); public JTextField ageT = new JTextField(); public JTextField departmentT = new JTextField(); public JTextField callT = new JTextFIEld(); public JTextArea textT = new JTextArea(); ////////////////////////////////////////////// ////////////////////////////////////////////// public Frm_Main() { ///////////////////////////////////////////////// frame = new JFrame("學籍管理系統V1.0"); c = frame.getContentPane(); c.setLayout(null); menuBar = new JMenuBar(); toolBar = new JToolBar(); toolBar.setFloatable(false); frame.setJMenuBar(menuBar); frame.setResizable(false); //////////////////////////////////////////////// mainMenu1 = new JMenu("管理"); String str1[] = {"添加", "刪除", "查詢", " ", "關閉"}; for (int i = 0; i < 5; i++) { if (i == 3) { mainMenu1.addSeparator(); } else { subMenu1[i] = new JMenuItem(str1[i]); subMenu1[i].addActionListener(this); mainMenu1.add(subMenu1[i]); } } menuBar.add(mainMenu1); ///////////////////////////////////////////////// mainMenu2 = new JMenu("操作"); String str2[] = {"查詢", "添加", "刪除", "修改", "提交修改", "提交添加", "提交刪除"}; for (int i = 0; i < 7; i++) { subMenu2[i] = new JMenuItem(str2[i]); subMenu2[i].addActionListener(this); mainMenu2.add(subMenu2[i]); } menuBar.add(mainMenu2); ///////////////////////////////////////////////// mainMenu3 = new JMenu("集體查詢"); String str3[] = {"按班級...", "按單位..."}; for (int i = 0; i < 2; i++) { subMenu3[i] = new JMenuItem(str3[i]); subMenu3[i].addActionListener(this); mainMenu3.add(subMenu3[i]); } menuBar.add(mainMenu3); //////////////////////////////////////////////// String strToolBar[] = {"查詢", "添加", "刪除", "修改", "提交修改", "提交添加", "提交刪除", "關於"}; for (int i = 0; i < 8; i++) { toolBarButton[i] = new JButton(strToolBar[i]); toolBarButton[i].setToolTipText(strTip[i]); toolBarButton[i].addActionListener(this); toolBar.add(toolBarButton[i]); } toolBar.setLocation(0, 0); toolBar.setSize(400, 30); c.add(toolBar); //////////////////////////////////////////////// classidL.setLocation(35, 40); classidL.setSize(40, 20); //classidL.setFont(new Font("",Font.BOLD,12)); c.add(classidL); classidT.setLocation(90, 40); classidT.setSize(200, 20); //classidT.setEnabled(false); c.add(classidT); nameL.setLocation(35, 70); nameL.setSize(40, 20); c.add(nameL); nameT.setLocation(90, 70); nameT.setSize(200, 20); c.add(nameT); sexL.setLocation(35, 100); sexL.setSize(40, 20); c.add(sexL); sexT.setLocation(90, 100); sexT.setSize(200, 20); c.add(sexT); ageL.setLocation(35, 130); ageL.setSize(40, 20); c.add(ageL); ageT.setLocation(90, 130); ageT.setSize(200, 20); c.add(ageT); departmentL.setLocation(35, 160); departmentL.setSize(40, 20); c.add(departmentL); departmentT.setLocation(90, 160); departmentT.setSize(200, 20); c.add(departmentT); callL.setLocation(35, 190); callL.setSize(40, 20); c.add(callL); callT.setLocation(90, 190); callT.setSize(200, 20); c.add(callT); ///////////////////////////////////////////////// tittleLabel.setText("----------集體查詢結果--------"); tittleLabel.setSize(300, 20); tittleLabel.setLocation(10, 210); c.add(tittleLabel); //////////////////////////////////////////// textT.setSize(400, 300); textT.setLocation(10, 240); c.add(textT, 14); /////////////////////////////////////////////// frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 650); frame.setLocation(200, 200); frame.setVisible(true); conDB(); } //以上是框架設計 //JDBC public void conDB() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(null, "數據庫錯誤"); } try { con = DriverManager.getConnection("jdbc:odbc:jettang", "sa", ""); st = con.createStatement(); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "數據庫連接失敗"); } } //關閉連接 public void closeDB() { try { st.close(); con.close(); } catch (SQLException e) { JOptionPane.showMessageDialog(null, "數據庫關閉失敗"); } } //事件響應 public void actionPerformed(ActionEvent e) { ////////////////////////////////////////////// if (e.getSource() == toolBarButton[7]) { JOptionPane.showMessageDialog(null, "當前版本1.0,操作幫助請看使用說明書"); } /////////////////////////////////////////////查詢(按姓名) if (e.getSource() == subMenu2[0] || e.getSource() == toolBarButton[0] || e.getSource() == subMenu1[2]) { String idid = JOptionPane.showInputDialog("請輸入學生姓名"); if (idid.trim() != "") { String strSQL = "select * from stuinfo where name ='" + idid + "'"; try { rs = st.executeQuery(strSQL); int count = 0; while (rs.next()) { classid = rs.getString("classid"); name = rs.getString("name"); department = rs.getString("department"); sex = rs.getString("sex"); age = rs.getString("age"); call = rs.getString("call"); ++count; } if (count == 0) { JOptionPane.showMessageDialog(null, "無此信息"); } else { classidT.setText(classid); nameT.setText(name); sexT.setText(sex); ageT.setText(age); callT.setText(call); departmentT.setText(department); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "查詢失敗"); } } } /////////////////////////////////////////////關閉 if (e.getSource() == subMenu1[4]) { frame.dispose(); closeDB(); } /////////////////////////////////////////////添加 if (e.getSource() == subMenu1[0] || e.getSource() == subMenu2[1] || e.getSource() == toolBarButton[1]) { JOptionPane.showMessageDialog(null, "請輸入你添加的信息再點擊提交添加鍵"); classidT.setEnabled(true); classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); } /////////////////////////////////////////////提交添加 if (e.getSource() == toolBarButton[5] || e.getSource() == subMenu2[5]) { if ((classidT.getText().trim()).equals("") || (nameT.getText().trim()).equals("") || (sexT.getText().trim()).equals("") || (ageT.getText().trim()).equals("") || (callT.getText().trim()).equals("") || (departmentT.getText().trim()).equals("")) { JOptionPane.showMessageDialog(null, "請先點擊添加鍵"); } else { classid = classidT.getText(); name = nameT.getText(); sex = sexT.getText(); age = ageT.getText(); call = callT.getText(); department = departmentT.getText(); String strSQL = "insert into stuinfo(classid,name,sex,age,call,department) values('" + classid + "','" + name + "','" + sex + "','" + age + "','" + call + "','" + department + "')"; try { st.executeUpdate(strSQL); } catch (Exception exx) { JOptionPane.showMessageDialog(null, "添加失敗"); return; } JOptionPane.showMessageDialog(null, "添加成功"); classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); } } ////////////////////////////////////////////提交修改 if (e.getSource() == subMenu2[4] || e.getSource() == toolBarButton[4]) { if ((classidT.getText().trim()).equals("") || (nameT.getText().trim()).equals("") || (sexT.getText().trim()).equals("") || (ageT.getText().trim()).equals("") || (callT.getText().trim()).equals("") || (departmentT.getText().trim()).equals("")) { JOptionPane.showMessageDialog(null, "請先點擊修改鍵"); return; } else { classid = classidT.getText(); name = nameT.getText(); sex = sexT.getText(); age = ageT.getText(); call = callT.getText(); department = departmentT.getText(); String strSQL = "update stuinfo set classid='" + classid + "',sex='" + sex + "',age='" + age + "',call='" + call + "',department='" + department + "'" + "where name='" + name + "'"; try { st.executeUpdate(strSQL); } catch (Exception exx) { JOptionPane.showMessageDialog(null, "修改失敗"); return; } JOptionPane.showMessageDialog(null, "修改成功"); classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); } } ///////////////////////////////////////////////修改 if (e.getSource() == subMenu2[3] || e.getSource() == toolBarButton[3]) { String idid = JOptionPane.showInputDialog("請輸入你要修改的學生姓名後點擊提交修改鍵確認"); if (idid.trim() != "") { String strSQL = "select * from stuinfo where name ='" + idid + "'"; try { rs = st.executeQuery(strSQL); int count = 0; while (rs.next()) { classid = rs.getString("classid"); name = rs.getString("name"); department = rs.getString("department"); sex = rs.getString("sex"); age = rs.getString("age"); call = rs.getString("call"); ++count; } if (count == 0) { JOptionPane.showMessageDialog(null, "無此信息"); } else { classidT.setText(classid); nameT.setText(name); sexT.setText(sex); ageT.setText(age); callT.setText(call); departmentT.setText(department); classidT.setEnabled(false); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "修改失敗"); } } } /////////////////////////////////////////////////刪除 if (e.getSource() == subMenu2[2] || e.getSource() == toolBarButton[2] || e.getSource() == subMenu1[1]) { String idDel = JOptionPane.showInputDialog("請輸入要刪除的學生姓名後點擊提交刪除鍵確認"); if (idDel.trim() != "") { String strSQL = "select * from stuinfo where name ='" + idDel + "'"; try { rs = st.executeQuery(strSQL); int count = 0; while (rs.next()) { classid = rs.getString("classid"); name = rs.getString("name"); department = rs.getString("department"); sex = rs.getString("sex"); age = rs.getString("age"); call = rs.getString("call"); ++count; } if (count == 0) { JOptionPane.showMessageDialog(null, "無此信息"); } else { classidT.setText(classid); nameT.setText(name); sexT.setText(sex); ageT.setText(age); callT.setText(call); departmentT.setText(department); classidT.setEnabled(false); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, "刪除失敗"); } } } //////////////////////////////////////////////////提交刪除 if (e.getSource() == toolBarButton[6] || e.getSource() == subMenu2[6]) { if ((classidT.getText().trim()).equals("") || (nameT.getText().trim()).equals("") || (sexT.getText().trim()).equals("") || (ageT.getText().trim()).equals("") || (callT.getText().trim()).equals("") || (departmentT.getText().trim()).equals("")) { JOptionPane.showMessageDialog(null, "請先點擊刪除鍵"); return; } else { name = nameT.getText(); String strSQL = "delete from stuinfo where name='" + name + "'"; try { st.executeUpdate(strSQL); } catch (Exception exx) { JOptionPane.showMessageDialog(null, "刪除失敗"); return; } JOptionPane.showMessageDialog(null, "刪除成功"); classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); } } //////////////////////////////////////////////集體查詢(按班級) if (e.getSource() == subMenu3[0]) { classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); if ((classidT.getText().trim()).equals("")) { String idclass = JOptionPane.showInputDialog("請輸入班級"); String strSQL = "select * from stuinfo where classid='" + idclass + "'"; try { rs = st.executeQuery(strSQL); displayres(rs); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "查詢失敗"); } } else { String idclass = classidT.getText(); String strSQL = "select * from stuinfo where classid='" + idclass + "'"; try { rs = st.executeQuery(strSQL); displayres(rs); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "查詢失敗"); } } } ////////////////////////////按單位 if (e.getSource() == subMenu3[1]) { classidT.setText(""); nameT.setText(""); sexT.setText(""); ageT.setText(""); callT.setText(""); departmentT.setText(""); if ((departmentT.getText().trim()).equals("")) { String idclass = JOptionPane.showInputDialog("請輸入單位"); String strSQL = "select * from stuinfo where department='" + idclass + "'"; try { rs = st.executeQuery(strSQL); displayres(rs); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "查詢失敗"); } } else { String idclass = departmentT.getText(); String strSQL = "select * from stuinfo where department='" + idclass + "'"; try { rs = st.executeQuery(strSQL); displayres(rs); } catch (Exception ex) { JOptionPane.showMessageDialog(null, "查詢失敗"); } } } } public void displayres(ResultSet rs1) throws SQLException { //定位到達第一條記錄 boolean moreRecords = rs1.next(); //如果沒有記錄,則提示一條消息 if (!moreRecords) { JOptionPane.showMessageDialog(null, "無此記錄"); return; } try { //獲取數據 getNextRow(rs1); //刷新Table c.validate(); } catch (SQLException sqlex) { sqlex.printStackTrace(); } } public void getNextRow(ResultSet rs2) throws SQLException { int count = 1; while (rs2.next()) { name = " 姓名:" + rs2.getString(1); age = " 年齡:" + rs2.getString(2) + " 單位:"; department = rs2.getString(3) + "\n"; textT.append(count + "." + name + age + department); count++; } } }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved