java(swing)+ mysql实现学生信息管理系统源码
本文实例为大家分享了java实现学生信息管理系统源码,供大家参考,具体内容如下
importjava.awt.BorderLayout; importjava.awt.Color; importjava.awt.Font; importjava.awt.GridLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.ResultSet; importjava.sql.SQLException; importjava.sql.Statement; importjava.util.ArrayList; importjava.util.List; importjavax.swing.BorderFactory; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JOptionPane; importjavax.swing.JPanel; importjavax.swing.JScrollPane; importjavax.swing.JTable; importjavax.swing.JTextArea; importjavax.swing.table.AbstractTableModel; importjavax.swing.text.BadLocationException; /* DROPDATABASEIFEXISTS`myproject`; CREATEDATABASEmyprojectDEFAULTCHARSETutf8COLLATEutf8_general_ci; USEABC; SETNAMESutf8mb4; SETFOREIGN_KEY_CHECKS=0; DROPTABLEIFEXISTS`student`; CREATETABLE`student`( `id`varchar(36)NOTNULL, `name`varchar(36)NOTNULL, `age`varchar(36)NOTNULL )ENGINE=InnoDBDEFAULTCHARSET=utf8; SETFOREIGN_KEY_CHECKS=1; * * */ publicclassTestextendsJFrame{ privatestaticfinallongserialVersionUID=1L; privateJTabletable; privateJPanelpanel; privateJScrollPanescrollpane; privateJButtonbutton1,button2,button3; privateJTextAreatext1,text2,text3; privateListstu; publicTest()throwsBadLocationException,SQLException{ super("学生信息"); this.setSize(500,340); this.add(getJScrollPane(stu),BorderLayout.CENTER); this.add(getJPanel(),BorderLayout.SOUTH); this.setResizable(true); this.setLocation(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //设置JScrollPane方法 privateJScrollPanegetJScrollPane(List stu)throwsSQLException{ if(scrollpane==null){ scrollpane=newJScrollPane(); scrollpane.setViewportView(getJTable(stu)); } returnscrollpane; } //设置JPanel方法 privateJPanelgetJPanel(){ if(panel==null){ panel=newJPanel(); panel.setLayout(newGridLayout(2,3)); text1=newJTextArea(); text2=newJTextArea(); text3=newJTextArea(); button1=newJButton("添加"); button2=newJButton("删除"); button3=newJButton("更新"); button1.addActionListener(newinsert()); button2.addActionListener(newdelete()); button3.addActionListener(newupdate()); text1.setBorder(BorderFactory.createLineBorder(Color.gray,2)); text2.setBorder(BorderFactory.createLineBorder(Color.gray,2)); text3.setBorder(BorderFactory.createLineBorder(Color.gray,2)); text1.setFont(newFont("宋体",Font.BOLD,16)); text2.setFont(newFont("宋体",Font.BOLD,16)); text3.setFont(newFont("宋体",Font.BOLD,16)); text1.setText("id"); text2.setText("name"); text3.setText("age"); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(button1); panel.add(button2); panel.add(button3); } returnpanel; } //设置Jtable方法 privatevoidsetJTable(JTabletable){ table.setFont(newFont("宋体",Font.BOLD,18)); table.setRowHeight(30); } //获取Jtable对象方法(该方法具体就是获得jtable对象的时候一并从数据取出学生信息并放入Jtable表格中) privateJTablegetJTable(List stu)throwsSQLException{ if(table==null){ JDBCDaoImpljdbc=newJDBCDaoImpl(); ResultSetrs=jdbc.search(); stu=select(rs); jdbc.closeConnection(); table=newJTable(newTable(stu)); setJTable(table); } returntable; } //设置学生信息方法(该方法是用户增加删除更新用户操作的具体实现方法包含了完整性检查) privateStudentsetStu(){ if(text1.getText().equals("")||text2.getText().equals("")||text3.getText().equals("")){ returnnull; }else{ Studentsd=newStudent(); sd.setId(text1.getText()); sd.setName(text2.getText()); sd.setAge(text3.getText()); returnsd; } } //重置输入框为空 privatevoidresetText(){ text1.setText(""); text2.setText(""); text3.setText(""); } //刷新学生信息方法(该方法是重新读取数据库学生的信息然后返回一个学生的集合用于刷新Jtable表格对象中的数据) privateList select(ResultSetrs)throwsSQLException{ List st=newArrayList (); while(rs.next()){ Students=newStudent(); s.setId(rs.getString(1)); s.setName(rs.getString(2)); s.setAge(rs.getString(3)); st.add(s); } returnst; } //添加按钮-监听器(该方法是对添加按钮实现的具体方法) classinsertimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ stu=newArrayList (); Studentsd=newStudent(); JDBCDaoImpljdbc=newJDBCDaoImpl(); sd=setStu(); if(sd!=null){ jdbc.insert(sd); ResultSetrs=jdbc.search(); try{ stu=select(rs); }catch(SQLExceptione1){ e1.printStackTrace(); } jdbc.closeConnection(); JTabletable=newJTable(newTable(stu));//新建一个Jtable对象用来盛放增加后的学生信息 setJTable(table);//设置Jtable信息 Test.this.scrollpane.setViewportView(table);//把Jtable设置到Panel resetText(); }else{ JOptionPane.showMessageDialog(Test.this,"输入数据不完整"); } } } //删除按钮-监听器(该方法是对删除按钮实现的具体方法) classdeleteimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ stu=newArrayList (); Studentsd=newStudent(); JDBCDaoImpljdbc=newJDBCDaoImpl(); sd=setStu(); if(sd!=null){ jdbc.delete(sd); ResultSetrs=jdbc.search(); try{ stu=select(rs); }catch(SQLExceptione1){ e1.printStackTrace(); } jdbc.closeConnection(); JTabletable=newJTable(newTable(stu));//新建一个Jtable对象用来盛放增加后的学生信息 setJTable(table);//设置Jtable信息 Test.this.scrollpane.setViewportView(table);//把Jtable设置到Panel resetText(); }else{ JOptionPane.showMessageDialog(Test.this,"输入数据不完整"); } } } //更新按钮-监听器(该方法是对更新按钮实现的具体方法) classupdateimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ stu=newArrayList (); Studentsd=newStudent(); JDBCDaoImpljdbc=newJDBCDaoImpl(); sd=setStu(); if(sd!=null){ jdbc.update(sd); ResultSetrs=jdbc.search(); try{ stu=select(rs); }catch(SQLExceptione1){ e1.printStackTrace(); } jdbc.closeConnection(); JTabletable=newJTable(newTable(stu));//新建一个Jtable对象用来盛放增加后的学生信息 setJTable(table);//设置Jtable信息 Test.this.scrollpane.setViewportView(table);//把Jtable设置到Panel resetText(); }else{ JOptionPane.showMessageDialog(Test.this,"输入数据不完整"); } } } //Student类(用于封装数据信息和数据库表进行映射) publicclassStudent{ //学生的idnameage信息 privateStringid; privateStringname; privateStringage; //get&set方法 publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetAge(){ returnage; } publicvoidsetAge(Stringage){ this.age=age; } } //JTable表模式类(JTable对象初始化的时候通过这个Table获取表格的行数、列数、列标题、以及每个单元格存放的数据具体使用原因放在开头的备注了) publicclassTableextendsAbstractTableModel{ List stu=newArrayList (); publicTable(Lists){ this.stu=s; } publicList getStu(){ returnstu; } publicvoidsetStu(List stu){ this.stu=stu; } @Override //获取行数 publicintgetRowCount(){ returnstu.size(); } @Override //获取列数 publicintgetColumnCount(){ //TODOAuto-generatedmethodstub return3; } @Override publicbooleanisCellEditable(introwIndex,intcolumnIndex){ returntrue; } @Override //获取列名字 publicStringgetColumnName(intcol){ Stringres=""; switch(col){ case0: res="ID"; break; case1: res="Name"; break; case2: res="Age"; break; default: break; } returnres; } @Override //获取具体值 publicObjectgetValueAt(introwIndex,intcolumnIndex){ //TODOAuto-generatedmethodstub Objectres=""; Studenttemp=stu.get(rowIndex); switch(columnIndex){ case0: res=temp.getId(); break; case1: res=temp.getName(); break; case2: res=temp.getAge(); break; default: break; } returnres; } } //JDBCDAO类配置连接数据的信息,链接释放操作和基本增删改查操作 publicclassJDBCDaoImpl{ Stringdriver="com.mysql.jdbc.Driver"; Stringurl="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false"; Stringuser="root"; Stringpasswd="123456"; Connectionconn=null; Statementstmt=null; ResultSetrs=null; //数据库连接开始 publicConnectiongetConnection(){ try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url,user,passwd); stmt=conn.createStatement(); }catch(Exceptione){ e.printStackTrace(); } returnconn; } //数据库连接释放 publicvoidcloseConnection(){ if(rs!=null){ try{ rs.close(); stmt.close(); conn.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(rs==null){ try{ stmt.close(); conn.close(); }catch(SQLExceptione){ e.printStackTrace(); } } } //查找操作 publicResultSetsearch(){ getConnection(); try{ Stringsql="SELECT*FROMstudent"; rs=stmt.executeQuery(sql); }catch(SQLExceptione){ e.printStackTrace(); } returnrs; } //添加操作 publicvoidinsert(Studentsd){ //TODOAuto-generatedmethodstub getConnection(); try{ Stringsql="INSERTINTOstudent(id,name,age)"+"VALUES('"+sd.getId()+"','"+sd.getName()+"','" +sd.getAge()+"')"; intcount=stmt.executeUpdate(sql); }catch(SQLExceptione){ e.printStackTrace(); } } //删除操作 publicvoiddelete(Studentsd){ //TODOAuto-generatedmethodstub getConnection(); try{ Stringsql="DELETEFROMstudentWHEREid='"+sd.getId()+"'"; intcount=stmt.executeUpdate(sql); }catch(SQLExceptione){ e.printStackTrace(); } } //更新操作 publicvoidupdate(Studentsd){ //TODOAuto-generatedmethodstub getConnection(); try{ Stringsql="UPDATEstudentSETname='"+sd.getName()+"',age='"+sd.getAge()+"'WHEREid='" +sd.getId()+"'"; intcount=stmt.executeUpdate(sql); }catch(SQLExceptione){ e.printStackTrace(); } } } //main方法 publicstaticvoidmain(String[]args)throwsBadLocationException,SQLException{ newTest().setVisible(true); } }
关于管理系统的更多内容请点击《管理系统专题》进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。