1、学生管理系统(图形界面)Java源代码: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(new LoginPane(myFrame);myFrame.pack();my
2、Frame.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;public void setConn(Connection conn)th
3、is.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.getMessage();return false; catch (ClassNotFoun
4、dException 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;catch(SQLException e)return null;public
5、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.awt.event.ActionListener;import java.s
6、ql.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.JRadioButton;import javax.swing.JTextField;pub
7、lic 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_pwd=new JPasswordField(20);JRadioButto
8、n 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.add(lab_username);box_user.add(Box.create
9、HorizontalStrut(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_teacher);bg.add(jrb_student);Box box_role=
10、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_button=Box.createHorizontalBox();box_bu
11、tton.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_login.add(box_user);box_login.add(Box.cre
12、ateHorizontalStrut(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 Auto-generated method stubif(e.getActi
13、onCommand().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(rs!=null)this.myFrame.setContentPane(n
14、ew 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;import javax.swing.JFrame;import javax.swin
15、g.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();public void init()JMenuBar menu=new
16、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);jm_user.add(jmi_add);jm_user.add(jmi_se
17、arch);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(this.myFrame);this.myFrame.validate();pa
18、ckage 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.JLabel;import javax.swing.JPanel;import jav
19、ax.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(查询用户名);JTextField jt_name=new JTextField(20);
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.add(Box.createHorizontalStrut(20);box_us
21、er.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.executeQuery(select * from User);try while
22、(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_search=Box.createVerticalBox();box_search.
23、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().trim();ResultSet rs=db.executeQuery(selec
24、t * 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 (SQLException e1) / TODO Auto-generated
25、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.SQLException;import java.util.Vector;im
26、port 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 getColumnCount() / TODO Auto-generated metho
27、d 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+; catch (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;
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100