1、学生管理系统java实现 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 2 个人收集整理 勿做商业用途 学生管理系统java实
2、现 准备的软件,我用是eclipse.exe,MySql,还有mysql jdbc 5.0 数据库。网上很容易找到华军上就有的。 第一步,安装eclipse。exe,MySql。MySql的密码请设置为123,因为我的程序上的密码就是123。以后你看懂了可以修改的! 把下载的mysql jdbc 5.0 数据库。zip通常是个压缩包解压到一个地方。然后运行eclipse.exe。 建立个项目,名字自己随便叫即可。然后在菜单栏上Project就是Run旁边的那个菜单,按下左键选择Properties就是最下面的一项。选择Java Build Path。在对话框的右面出现个小窗体,选Libr
3、aries。选择Add External JARs。..。选择你mysql jdbc 5.0 数据库,解压出来的mysql-connector-java-5。0.0-beta—bin。jar 也许名字有出入. 如下面的图 添加完成后就是这个样子了 第二步 安装Mysql数据库 请把密码设置为123 就是带password的地方。我的程序上密码设置就是123为了运行方便. 然后运行Mysql执行下面四个步骤 。 四部的命令分别是 1.create database scxdb; 2。use scxdb; 3。create table stud
4、ent( number int, name char(8), language int, math int, english int, birthday date); 4。insert into student values('1717',’曹操’,’80’,’90’,'20',’1985—01-01'); 下面是程序清单! //============================================================================== 第一部分 package DBTest; import java.awt。even
5、t。ActionEvent; import java.awt.event.ActionListener; import javax。swing。JFrame; import javax.swing。JMenu; import javax。swing。JMenuBar; import javax。swing.JMenuItem; import javax。swing.JScrollPane; import javax.swing.JTable; public class DBFrame { /** * @param args */ public static
6、void main(String[] args) { // TODO Auto-generated method stub String sql = ”select * from student;”; // sql=”insert into student values // ('1717’,'张云风’,'56’,'80’,'65’,’1979-02—16’);"; DBFrameWindow db = new DBFrameWindow(sql); db。setDefaultCloseOperation(db。EXIT_ON_CLOSE); db。s
7、etVisible(true); db。setResizable(false); } } class DBFrameWindow extends JFrame { public DBFrameWindow(String sql) { super.setTitle("DBFrameWindow"); this.sql=sql; this.setSize(500, 380); JTable table = DBHander。getTable(this。sql); scroolPane = new JScrollPane(table);
8、this。add(scroolPane); // ============================================ JMenu runMenu = new JMenu(”Run"); JMenuBar mb = new JMenuBar(); mb。add(runMenu); this。setJMenuBar(mb); //=============Append===============Append=============== JMenuItem appendItem = new JMenuItem("Append");
9、 appendItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub appendFrame af = new appendFrame(); af.setDefaultCloseOperation(af。EXIT_ON_CLOSE); af.setVisible(true); af.setResizable(fa
10、lse); DBHander.getUpdataTest(”"); } }); //=============Append===============Append=============== //=============Remove===============Remove=============== JMenuItem removeItem = new JMenuItem("Remove”); removeItem.addActionListener(new ActionListener(){个人收集整理,勿做商业用途本文为互联
11、网收集,请勿用作商业用途 @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub removeFrame rf=new removeFrame(); rf。setDefaultCloseOperation(rf.EXIT_ON_CLOSE); rf.setResizable(false); rf。setVisible(true); } }); //========
12、Remove===============Remove=============== //=============Update===============Update=============== JMenuItem updateItem = new JMenuItem(”Update”); updateItem。addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-genera
13、ted method stub updateFrame uf = new updateFrame(); uf。setDefaultCloseOperation(uf。EXIT_ON_CLOSE); uf。setResizable(false); uf.setVisible(true); } }); //=============Update===============Update=============== //=============Find===============Find========
14、 JMenuItem findItem = new JMenuItem(”Find”); findItem。addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto—generated method stub selectFrame sf = new selectFrame(); sf.setDefaultCloseOperation(sf.EXIT_ON_CL
15、OSE); sf.setResizable(false); sf.setVisible(true); }}); //=============Find===============Find=============== JMenuItem refreshItem = new JMenuItem(”Refresh"); refreshItem。addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ar
16、g0) { // TODO Auto-generated method stub JTable ta=DBHander.getTable(”select * from student;”); JScrollPane scroolPane_1=new JScrollPane(ta); add(scroolPane_1); System.out.println(”refreshItem”); } }); runMenu。add(appendItem); runMen
17、u。add(removeItem); runMenu。add(updateItem); runMenu.add(findItem); runMenu.add(refreshItem); // ============================================ } private JScrollPane scroolPane = null; private String sql=""; } //===================================================================
18、 //============================================================================== 第二部分 package DBTest; import java。io.UnsupportedEncodingException; import java.sql.Connection; import java。sql.PreparedStatement; import java。sql。ResultSet; import java。sql。ResultSetMetaData;
19、 import java。sql。SQLException; import java。sql。Statement; import javax.swing。JTable; import com.mysql.jdbc。jdbc2.optional。MysqlConnectionPoolDataSource; public class DBHander { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Strin
20、g sql = "insert into student values ('1717','张云风’,'56’,'80',’65’,’1979-02—16');”; Connection conn = getConnection(); if (conn == null) { System.out.println(”conn==null”); } else { System。out.println(”conn!=null"); } Statement stat = getStatement(); if (stat == null) { S
21、ystem.out.println(”stat==null”); } else { System.out。println(”stat!=null”); } ResultSet rs = getResult(”select * from student;”); if (rs == null) { System.out.println(”rs==null”); } else { System。out.println("rs!=null”); } JTable table = getTable("select * from stude
22、nt;”); if (rs == null) { System.out.println(”table==null"); } else { System。out。println(”table!=null”); } } private static String[][] select=null; // =================== Connection =================================== public static Connection getConnection() { MysqlConnect
23、ionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setUrl("jdbc:MySQL://localhost/scxdb"); ds.setUser(”root”); ds。setPassword("123”); try { return ds.getConnection(); } catch (SQLException e) { // TODO Auto—generated catch block e.printStackTrace(); } Sys
24、tem.out.println("conn==null"); return null; }文档为个人收集整理,来源于网络本文为互联网收集,请勿用作商业用途 // =================== Connection =================================== // =================== Statement =================================== public static Statement getStatement() { Connection conn = DBHander.g
25、etConnection(); try { return conn。createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet。CONCUR_UPDATABLE); } catch (SQLException e) { // TODO Auto-generated catch block e。printStackTrace(); } System。out.println("stat==null”); return null; } // ============
26、 Statement =================================== // =================== ResultSet =================================== public static ResultSet getResult(String sql) { try { return getStatement()。executeQuery(sql); } catch (SQLException e) { // TODO Auto—generated catch block
27、 e。printStackTrace(); } System。out。println(”table==null"); return null; } // =================== ResultSet =================================== // =================== getTable =================================== public static JTable getTable(String sql) { ResultSet rs = null;
28、String[][] str = null; int cols = 0; try { rs = getResult(sql); ResultSetMetaData md = rs.getMetaData(); cols = md。getColumnCount(); int n = 0; while (rs。next()) { n = n + 1; } str = new String[n][cols]; } catch (SQLException e) { // TODO Auto—generated
29、catch block e.printStackTrace(); } rs = getResult(sql); int n = 0; try { while (rs。next()) { for (int i = 0; i < cols; i++) { // str[n][i]=rs.getString(i+1); str[n][i] = new String(rs.getString(i + 1)。getBytes( ”iso—8859—1"), ”GBK"); } n = n + 1
30、 } } catch (SQLException e) { // TODO Auto—generated catch block e。printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e。printStackTrace(); }本文为互联网收集,请勿用作商业用途文档为个人收集整理,来源于网络 String[] s = new String[] { "Number”, "Name", "L
31、anguage”, ”Math", "English”, "Birthday" }; JTable table = new JTable(str, s); for (int i = 0; i < str.length; i++) { for (int j = 0; j 〈 str[i].length; j++) System.out.print(str[i][j] + " ”); System。out.println(); } select=str; return table; } // ===================
32、getTable =================================== public static void getUpdataTest(String sql){ try { Connection conn=getConnection(); if(conn==null) System。out。println(”null”); PreparedStatement ps=conn.prepareStatement(”UPDATE student SET number = ? WHERE number=?;”); ps。setStri
33、ng(1,”2088”); ps.setString(2,"1717"); System。out。println(”#################"); } catch (SQLException e) { // TODO Auto-generated catch block e。printStackTrace(); } } // =================== getUpdata =================================== public static void getUpdate(S
34、tring sql) { Statement stat; try { stat = getConnection()。createStatement(); int n = stat.executeUpdate(sql); System。out。println("*********” + n + "***********”); } catch (SQLException e) { // TODO Auto-generated catch block e。printStackTrace(); }本文为互联网收集,请勿用作商业用途本文为互
35、联网收集,请勿用作商业用途 } // =================== getUpdata =================================== // =================== getSelect =================================== public static String[][] getSelect(String sql){ getTable(sql); if(select==null) System.out。println(”select=null"); return select;
36、 } // =================== getSelect =================================== } //============================================================================== //============================================================================== 第3部分 package DBTest; import jav
37、a。awt。BorderLayout; import java.awt。GridLayout; import java。awt。event。ActionEvent; import java.awt.event.ActionListener; import java。io。UnsupportedEncodingException; import javax。swing。JButton; import javax.swing.JFrame; import javax。swing.JLabel; import javax.swing。JPanel; import javax.swi
38、ng。JTextField; public class appendFrame extends JFrame { /** * @param args */ public static void main(String[] args) { // TODO Auto—generated method stub appendFrame af = new appendFrame(); af。setDefaultCloseOperation(af。EXIT_ON_CLOSE); af。setVisible(true); } public append
39、Frame() { super。setTitle("AppendFrame"); this。setSize(150, 220); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(6, 2)); panel.add(new JLabel(" 学号"), tf_num); panel.add(tf_num); panel。add(new JLabel(" 姓名”), tf_name); panel。add(tf_name); panel。add(new JLabel
40、 语文"), tf_c); panel.add(tf_c); panel。add(new JLabel(" 数学”), tf_m); panel。add(tf_m); panel.add(new JLabel(" 外语"), tf_e); panel。add(tf_e); panel.add(new JLabel(” 生日"), tf_b); panel.add(tf_b); tf_c.setText(”0"); tf_m。setText("0"); tf_e.setText("0”); this.add(p
41、anel, BorderLayout。CENTER); JPanel p = new JPanel(); p.setLayout(new GridLayout(1, 2)); p。add(smitButton); // ============== returnButton ============================ returnButton。addActionListener(new ActionListener() {个人收集整理,勿做商业用途个人收集整理,勿做商业用途 @Override public void actionPe
42、rformed(ActionEvent arg0) { // TODO Auto—generated method stub appendFrame.this。setVisible(false); appendFrame.this.setEnabled(false); } }); p.add(returnButton); // =============== returnButton =========================== // ===================== smitButton // =====
43、 smitButton.addActionListener(new ActionListener() { @Override // 本应该在此处添加判断是否为空纪录的,然后有对话框弹出但是,没有想到好的省事的方法,不过是可以实现的! public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String t1 = ”’” + tf_num.getText() + "',";
44、 String t2 = ”’" + tf_name。getText() + "’,"; String t3 = "'" + tf_c.getText() + ”’,"; String t4 = "’" + tf_m。getText() + "’,"; String t5 = ”'” + tf_e。getText() + "’,”; String t6 = ”'" + tf_b。getText() + "’"; sql = "insert into student values(” + t1 + t2 + t3 + t4 + t5
45、 + t6 + ");"; try { sql = new String(sql.getBytes("GBK"), ”iso8859-1"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(”@@@@@@@@@@@@@@@@@@@@@@@@ " + sql); DBH
46、ander。getUpdate(sql); appendFrame。this.setVisible(false); appendFrame.this.setEnabled(false); } });文档为个人收集整理,来源于网络个人收集整理,勿做商业用途 // ===================== smitButton // ======================================= this.add(p, BorderLayout。SOUTH); } public String getSql()
47、{ return sql; } private JTextField tf_num = new JTextField(10); private JTextField tf_name = new JTextField(10); private JTextField tf_c = new JTextField(10); private JTextField tf_m = new JTextField(10); private JTextField tf_e = new JTextField(10); private JTextField tf_b = new JT
48、extField(16); private JButton smitButton = new JButton("确认”); private JButton returnButton = new JButton("返回”); private String sql = null; } //============================================================================== //==========================================================
49、 第4部分 package DBTest; import java.awt.BorderLayout; import java。awt。event。ActionEvent; import java.awt。event.ActionListener; import java。io。UnsupportedEncodingException; import javax。swing。JButton; import javax.swing。JCheckBox; import javax.swing.JComboBox; import javax
50、swing。JFrame; import javax.swing。JOptionPane; import javax.swing。JPanel; import javax.swing.JTextField; public class removeFrame extends JFrame { /** * @param args */ public static void main(String[] args) { // TODO Auto—generated method stub removeFrame rf = new removeFrame();






