《学生管理系统java实现.doc》由会员分享,可在线阅读,更多相关《学生管理系统java实现.doc(18页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、学生管理系统java实现准备的软件,我用是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。在对话框的右面出现个小窗体,
2、选Libraries。选择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 student(number int,name char(8),langu
3、age int,math int,english int,birthday date); 4。insert into student values(1717,曹操,80,90,20,1985-01-01); 下面是程序清单!第一部分package DBTest;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.
4、JMenuItem;import javax.swing.JScrollPane;import javax.swing.JTable;public class DBFrame /* * param args */public static void main(String args) / TODO Auto-generated method stubString sql = select * from student;/ sql=insert into student values/ (1717,张云风,56,80,65,1979-02-16);DBFrameWindow db = new D
5、BFrameWindow(sql);db.setDefaultCloseOperation(db.EXIT_ON_CLOSE);db.setVisible(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
6、 JScrollPane(table);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);appendItem.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent
7、e) / TODO Auto-generated method stubappendFrame af = new appendFrame();af.setDefaultCloseOperation(af.EXIT_ON_CLOSE);af.setVisible(true);af.setResizable(false);DBHander.getUpdataTest(););/=Append=Append=/=Remove=Remove=JMenuItem removeItem = new JMenuItem(Remove);removeItem.addActionListener(new Act
8、ionListener()Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubremoveFrame rf=new removeFrame();rf.setDefaultCloseOperation(rf.EXIT_ON_CLOSE);rf.setResizable(false);rf.setVisible(true););/=Remove=Remove=/=Update=Update=JMenuItem updateItem = new JMenuItem(Update)
9、;updateItem.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubupdateFrame uf = new updateFrame();uf.setDefaultCloseOperation(uf.EXIT_ON_CLOSE);uf.setResizable(false);uf.setVisible(true););/=Update=Update=/=Find=Find=JMenuItem findItem =
10、 new JMenuItem(Find);findItem.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubselectFrame sf = new selectFrame();sf.setDefaultCloseOperation(sf.EXIT_ON_CLOSE);sf.setResizable(false);sf.setVisible(true););/=Find=Find=JMenuIt
11、em refreshItem = new JMenuItem(Refresh);refreshItem.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubJTable ta=DBHander.getTable(select * from student;);JScrollPane scroolPane_1=new JScrollPane(ta);add(scroolPane_1);System.
12、out.println(refreshItem);runMenu.add(appendItem);runMenu.add(removeItem);runMenu.add(updateItem);runMenu.add(findItem);runMenu.add(refreshItem);/ =private JScrollPane scroolPane = null;private String sql=;第二部分package DBTest;import java.io.UnsupportedEncodingException;import java.sql.Connection;impor
13、t java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;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
14、(String args) / TODO Auto-generated method stubString 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) System.out.prin
15、tln(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 student;);if (rs = null) System.out.println(table=null); else System.out.println(ta
16、ble!=null);private static String select=null;/ = Connection =public static Connection getConnection() MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();ds.setUrl(jdbc:MySQL:/localhost/scxdb);ds.setUser(root);ds.setPassword(123);try return ds.getConnection(); catch (SQLException
17、e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(conn=null);return null;/ = Connection =/ = Statement =public static Statement getStatement() Connection conn = DBHander.getConnection();try return conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATA
18、BLE); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(stat=null);return null;/ = Statement =/ = ResultSet =public static ResultSet getResult(String sql) try return getStatement().executeQuery(sql); catch (SQLException e) / TODO Auto-generated catch bloc
19、ke.printStackTrace();System.out.println(table=null);return null;/ = ResultSet =/ = getTable =public static JTable getTable(String sql) ResultSet rs = null;String str = null;int cols = 0;try rs = getResult(sql);ResultSetMetaData md = rs.getMetaData();cols = md.getColumnCount();int n = 0;while (rs.nex
20、t() n = n + 1;str = new Stringncols; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();rs = getResult(sql);int n = 0;try while (rs.next() for (int i = 0; i cols; i+) / strni=rs.getString(i+1);strni = new String(rs.getString(i + 1).getBytes(iso-8859-1), GBK);n = n + 1; catch
21、 (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (UnsupportedEncodingException e) / TODO Auto-generated catch blocke.printStackTrace();String s = new String Number, Name, Language, Math, English, Birthday ;JTable table = new JTable(str, s);for (int i = 0; i str.length; i
22、+) for (int j = 0; j stri.length; j+)System.out.print(strij + );System.out.println();select=str;return table;/ = getTable =public static void getUpdataTest(String sql)try Connection conn=getConnection();if(conn=null) System.out.println(null);PreparedStatement ps=conn.prepareStatement(UPDATE student
23、SET number = ? WHERE number=?;);ps.setString(1,2088);ps.setString(2,1717);System.out.println(#); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();/ = getUpdata =public static void getUpdate(String sql) Statement stat;try stat = getConnection().createStatement();int n = sta
24、t.executeUpdate(sql);System.out.println(* + n + *); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();/ = getUpdata =/ = getSelect =public static String getSelect(String sql)getTable(sql);if(select=null) System.out.println(select=null);return select;/ = getSelect =第3部分packa
25、ge DBTest;import java.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.s
26、wing.JTextField;public class appendFrame extends JFrame /* * param args */public static void main(String args) / TODO Auto-generated method stubappendFrame af = new appendFrame();af.setDefaultCloseOperation(af.EXIT_ON_CLOSE);af.setVisible(true);public appendFrame() super.setTitle(AppendFrame);this.s
27、etSize(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( 语文), tf_c);panel.add(tf_c);panel.add(new JLabel( 数学), tf_m);panel.add(tf_m);panel.add(ne
28、w 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(panel, BorderLayout.CENTER);JPanel p = new JPanel();p.setLayout(new GridLayout(1, 2);p.add(smitButton);/ = returnButton =returnButton.addActionListener(new A
29、ctionListener() Overridepublic void actionPerformed(ActionEvent arg0) / TODO Auto-generated method stubappendFrame.this.setVisible(false);appendFrame.this.setEnabled(false););p.add(returnButton);/ = returnButton =/ = smitButton/ =smitButton.addActionListener(new ActionListener() Override/ 本应该在此处添加判断
30、是否为空纪录的,然后有对话框弹出但是,没有想到好的省事的方法,不过是可以实现的!public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubString t1 = + tf_num.getText() + ,;String t2 = + tf_name.getText() + ,;String t3 = + tf_c.getText() + ,;String t4 = + tf_m.getText() + ,;String t5 = + tf_e.getText() + ,;String t6 = +
31、tf_b.getText() + ;sql = insert into student values( + t1 + t2 + t3 + t4 + t5+ t6 + );try sql = new String(sql.getBytes(GBK), iso8859-1); catch (UnsupportedEncodingException e1) / TODO Auto-generated catch blocke1.printStackTrace();System.out.println( + sql);DBHander.getUpdate(sql);appendFrame.this.s
32、etVisible(false);appendFrame.this.setEnabled(false););/ = smitButton/ =this.add(p, BorderLayout.SOUTH);public String getSql() 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
33、 = new JTextField(10);private JTextField tf_e = new JTextField(10);private JTextField tf_b = new JTextField(16);private JButton smitButton = new JButton(确认);private JButton returnButton = new JButton(返回);private String sql = null;第4部分package DBTest;import java.awt.BorderLayout;import java.awt.event.
34、ActionEvent;import java.awt.event.ActionListener;import java.io.UnsupportedEncodingException;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public cl
35、ass removeFrame extends JFrame /* * param args */public static void main(String args) / TODO Auto-generated method stubremoveFrame rf = new removeFrame();rf.setDefaultCloseOperation(rf.EXIT_ON_CLOSE);rf.setVisible(true);private JComboBox comboBox_condition = null;private JComboBox comboBox_field = n
36、ull;private JTextField text_str = new JTextField(10);private JButton smitButton = new JButton(确定);private JButton returnButton = new JButton(返回);private JCheckBox cBox = new JCheckBox(ALL);private String sql = ;public removeFrame() super.setTitle(RemoveFrame);this.setSize(410, 160);setResizable(fals
37、e);String strCondition = new String , =, ;String strField = new String Number, Name, Language, Math, English, Birthday;comboBox_condition = new JComboBox(strCondition);comboBox_field = new JComboBox(strField);JPanel panel = new JPanel();/ panel.setLayout(new GridLayout(2,3);panel.add(comboBox_field);panel.add(comboBox_condition);panel.add(text_str);panel.add(cBox);/ =cBox=cBox.addActionListener(new ActionListener() Overridepublic void actionPerfo
限制150内