1、学生管理系统图形界面Java之欧阳育创编学生管理系统(图形界面)Java时间:2021.02.04创作:欧阳育源代码:package exp10;import javax.swing.JFrame;public class MainClass /* * param args */public static void main(String args) / TODO Auto-generated method stubJFrame myFrame=new JFrame(学生成绩管理系统);myFrame.setLocation(300,100);/myFrame.setContentPane(n
2、ew LoginPane(myFrame);myFrame.pack();myFrame.setVisible(true);package exp10;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DataBase Connection conn=null;public Connection getConn()return this.conn
3、;public void setConn(Connection conn)this.conn=conn;public boolean getConnection()String url=jdbc:mysql:/localhost:3306/ScoreManagement?user=root&password=123;tryClass.forName(org.gjt.mm.mysql.Driver);conn=DriverManager.getConnection(url);return true;catch(SQLException e)System.out.println(e.getMess
4、age();return false; catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();return false;public ResultSet executeQuery(String sql)ResultSet rs=null;Statement stmt=null;if(conn=null)this.getConnection();trystmt=conn.createStatement();rs=stmt.executeQuery(sql);return rs;c
5、atch(SQLException e)return null;public int executeUpdate(String sql)Statement stmt=null;int linecount=0;if(conn=null)this.getConnection();trystmt=conn.createStatement();linecount=stmt.executeUpdate(sql);catch(SQLException e)return linecount;package exp10;import java.awt.event.ActionEvent;import java
6、.awt.event.ActionListener;import java.sql.ResultSet;import java.sql.SQLException;import javax.swing.Box;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JRadioB
7、utton;import javax.swing.JTextField;public class LoginPane extends JPanel implements ActionListener/* * */private static final long serialVersionUID = 1L;JFrame myFrame;JLabel lab_username=new JLabel(用户名);JTextField jt_username=new JTextField(20);JLabel lab_password=new JLabel(密 码);JPasswordField jt
8、_pwd=new JPasswordField(20);JRadioButton jrb_manager=new JRadioButton(管理员);JRadioButton jrb_teacher=new JRadioButton(教师);JRadioButton jrb_student=new JRadioButton(学生);public LoginPane(JFrame myFrame)this.myFrame=myFrame;this.init();public void init()Box box_user=Box.createHorizontalBox();box_user.ad
9、d(lab_username);box_user.add(Box.createHorizontalStrut(20);box_user.add(jt_username);jt_pwd.setEchoChar(#);Box box_pwd=Box.createHorizontalBox();box_pwd.add(lab_password);box_pwd.add(Box.createHorizontalStrut(20);box_pwd.add(jt_pwd);ButtonGroup bg=new ButtonGroup();bg.add(jrb_manager);bg.add(jrb_tea
10、cher);bg.add(jrb_student);Box box_role=Box.createHorizontalBox();box_role.add(jrb_manager);box_role.add(Box.createHorizontalStrut(5);box_role.add(jrb_teacher);box_role.add(Box.createHorizontalStrut(5);box_role.add(jrb_student);JButton jb_login=new JButton(登陆);JButton jb_exit=new JButton(退出);Box box_
11、button=Box.createHorizontalBox();box_button.add(jb_login);box_button.add(Box.createHorizontalStrut(30);box_button.add(jb_exit);jb_login.addActionListener(this);jb_exit.addActionListener(this);jb_login.setActionCommand(login);jb_exit.setActionCommand(exit);Box box_login=Box.createVerticalBox();box_lo
12、gin.add(box_user);box_login.add(Box.createHorizontalStrut(10);box_login.add(box_pwd);box_login.add(Box.createHorizontalStrut(10);box_login.add(box_role);box_login.add(Box.createHorizontalStrut(10);box_login.add(box_button);this.add(box_login);Overridepublic void actionPerformed(ActionEvent e) / TODO
13、 Auto-generated method stubif(e.getActionCommand().equals(login)String username=this.jt_username.getText().trim();String pwd=String.valueOf(this.jt_pwd.getPassword();DataBase db=new DataBase();String sql=select * from User where name=+username+ and password=+pwd+;ResultSet rs=db.executeQuery(sql);if
14、(rs!=null)this.myFrame.setContentPane(new ManagerPane(this.myFrame);myFrame.setSize(800,500);this.myFrame.validate();elsethis.myFrame.dispose();package exp10;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;impo
15、rt javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTable;public class ManagerPane extends JPanel implements ActionListenerJFrame myFrame;public ManagerPane(JFrame myFrame)this.myFrame=myFrame;this.init
16、();public void init()JMenuBar menu=new JMenuBar();JMenu jm_user=new JMenu(人员管理);JMenuItem jmi_add=new JMenuItem(增加人员);JMenuItem jmi_search=new JMenuItem(查询人员);jmi_add.addActionListener(this);jmi_add.setActionCommand(addUser);jmi_search.addActionListener(this);jmi_search.setActionCommand(searchUser);
17、jm_user.add(jmi_add);jm_user.add(jmi_search);JMenu jm_exit=new JMenu(退出);menu.add(jm_user);menu.add(jm_exit);this.add(menu,0);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getActionCommand().equals(searchUser)this.myFrame.setContentPane(new SearchUserPane(t
18、his.myFrame);this.myFrame.validate();package exp10;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Vector;import javax.swing.Box;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLab
19、el;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.table.DefaultTableModel;public class SearchUserPane extends JPanel implements ActionListener /* * */private static final long serialVersionUID = 1L;JFrame myFrame;JLabel jlb_name=new JLabel(查询用户名)
20、;JTextField jt_name=new JTextField(20);JButton btn_search=new JButton(查询);DataBase db=new DataBase();DefaultTableModel tableModel;JTable jt_show;public SearchUserPane(JFrame myFrame)this.myFrame=myFrame;init();public void init()Box box_user=Box.createHorizontalBox();box_user.add(jlb_name);box_user.a
21、dd(Box.createHorizontalStrut(20);box_user.add(jt_name);btn_search.addActionListener(this);box_user.add(btn_search);String colname=编号,姓名,密码,身份;tableModel=new DefaultTableModel(null,colname);jt_show=new JTable(tableModel);Box box_show=Box.createHorizontalBox();box_show.add(jt_show);ResultSet rs=db.exe
22、cuteQuery(select * from User);try while(rs.next()Vector data=new Vector();data.add(rs.getString(id);data.add(rs.getString(name);data.add(rs.getString(password);data.add(rs.getString(role);tableModel.addRow(data); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();Box box_sea
23、rch=Box.createVerticalBox();box_search.add(box_user);box_search.add(Box.createVerticalStrut(50);box_search.add(box_show);this.add(box_search);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource()=this.btn_search)String username=this.jt_name.getText().tr
24、im();ResultSet rs=db.executeQuery(select * from User where name=+username+);try while(rs.next()Vector data=new Vector();data.add(rs.getString(id);data.add(rs.getString(name);data.add(rs.getString(password);data.add(rs.getString(role);tableModel.fireTableDataChanged();tableModel.addRow(data); catch (
25、SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();package exp10;import javax.swing.JTable;import javax.swing.table.DefaultTableModel;public class UserTable extends JTable public UserTable()package exp10;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.
26、SQLException;import java.util.Vector;import javax.swing.table.AbstractTableModel;public class UserTableModel extends AbstractTableModel ResultSet rs;Vector tabledat;public UserTableModel()DataBase db=new DataBase();rs=db.executeQuery(select * from User);tabledat=new Vector();Overridepublic int getCo
27、lumnCount() / TODO Auto-generated method stubtry ResultSetMetaData metadata=rs.getMetaData();return metadata.getColumnCount(); catch (SQLException e) / TODO Auto-generated catch blockreturn 0;Overridepublic int getRowCount() / TODO Auto-generated method stubint rownum=0;try while(rs.next()rownum+; c
28、atch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return rownum;Overridepublic Object getValueAt(int row, int col) / TODO Auto-generated method stubtry if(rs.absolute(row)return rs.getObject(col); catch (SQLException e) / TODO Auto-generated catch blockreturn null;时间:2021.02.04创作:欧阳育
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2