Java学生信息管理系统设计(数据库版)
本文实例为大家分享了数据库版的Java学生信息管理系统,供大家参考,具体内容如下
packageStudent_system; importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; importjava.io.*; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.PreparedStatement; importjava.sql.ResultSet; importjava.sql.Statement; importjava.util.*; /*classStuimplementsjava.io.Serializable{ Stringnumber,name,specialty,grade,borth,sex; publicStu(){}; publicvoidsetNumber(Stringnumber){this.number=number;} publicStringgetNumber(){returnnumber;} publicvoidsetName(Stringname){this.name=name;} publicStringgetName(){returnname;} publicvoidsetSex(Stringsex){this.sex=sex;} publicStringgetSex(){returnsex;} publicvoidsetSpecialty(Stringspecialty){this.specialty=specialty;} publicStringgetSpecialty(){returnspecialty;} publicvoidsetGrade(Stringgrade){this.grade=grade;} publicStringgetGrade(){returngrade;} publicvoidsetBorth(Stringborth){this.borth=borth;} publicStringgetBorth(){returnborth;} }*/ publicclassStudentSystemextendsJFrame{ publicstaticvoidmain(String[]args){ JFrameframe=newJFrame(); frame.setTitle("信息管理系统"); frame.setSize(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Containercontainer=frame.getContentPane(); container.setLayout(newFlowLayout()); JLabellb=newJLabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询"+ "内容改后的保存!"); finalJTextField学号; finalJTextField姓名; finalJTextField专业; finalJTextField年级; finalJTextField出生; finalJRadioButton男; finalJRadioButton女; ButtonGroupgroup=null; JButton录入,查询,删除,修改,显示; JPanelp1,p2,p3,p4,p5,p6,pv,ph; 学号=newJTextField(10); 姓名=newJTextField(10); 专业=newJTextField(10); 年级=newJTextField(10); 出生=newJTextField(10); group=newButtonGroup(); 男=newJRadioButton("男"); 女=newJRadioButton("女"); group.add(男); group.add(女); 录入=newJButton("录入"); 查询=newJButton("查询"); 删除=newJButton("删除"); 修改=newJButton("修改"); 显示=newJButton("显示"); 修改.setEnabled(false); //添加输入框及文本框 p1=newJPanel(); p1.add(newJLabel("学号:",JLabel.CENTER)); p1.add(学号); p2=newJPanel(); p2.add(newJLabel("姓名:",JLabel.CENTER)); p2.add(姓名); p3=newJPanel(); p3.add(newJLabel("性别:",JLabel.CENTER)); p3.add(男); p3.add(女); p4=newJPanel(); p4.add(newJLabel("专业:",JLabel.CENTER)); p4.add(专业); p5=newJPanel(); p5.add(newJLabel("年级:",JLabel.CENTER)); p5.add(年级); p6=newJPanel(); p6.add(newJLabel("出生:",JLabel.CENTER)); p6.add(出生); pv=newJPanel(); pv.setLayout(newGridLayout(6,1)); pv.add(p1); pv.add(p2); pv.add(p3); pv.add(p4); pv.add(p5); pv.add(p6); ph=newJPanel(); ph.add(录入); ph.add(查询); ph.add(修改); ph.add(删除); ph.add(显示); frame.add(lb); frame.add(ph); frame.add(p1); frame.add(p2); frame.add(p3); frame.add(p4); frame.add(p5); frame.add(p6); frame.add(pv); frame.add(ph); classMyListenerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ //判断选中是男是女 /*if(e.getSource().equals(男)){ if(男.isSelected()){ System.out.println("男被选中了"); }else{ System.out.println("男被取消选中了"); } } if(e.getSource().equals(女)){ if(女.isSelected()){ System.out.println("女被选中了"); }else{ System.out.println("女被取消选中了"); } }*/ //录入的功能 if(e.getActionCommand()=="录入"){ Stringtext1=学号.getText().trim(); Stringtext2=姓名.getText(); Stringtext3=专业.getText(); Stringtext4=年级.getText(); Stringtext5=出生.getText(); Stringtext6=null; Stringtext7="-----我是分割线-----"; if(男.isSelected()){ text6="男"; } if(女.isSelected()){ text6="女"; } //用texts包含此次所有录入信息 Stringtexts="\n\n学号:"+text1+"\n\n"+"姓名:"+text2+"\n\n"+"专业:"+text3+"\n\n"+"年级:"+text4+"\n\n"+"出生:"+text5+"\n\n"+"性别:"+text6+"\n\n"; //显示框 intm=JOptionPane.showConfirmDialog(null,"是否录入该条记录:"+texts,"录入",JOptionPane.YES_NO_OPTION);//n=1/0; if(m==0){ insert_Student(text1,text2,text3,text4,text5); }else{ JOptionPane.showMessageDialog(null,"已取消该次录入!!"); } } //显示的功能 if(e.getActionCommand()=="显示"){ intn=JOptionPane.showConfirmDialog(null,"是否显示所有记录","显示",JOptionPane.YES_NO_OPTION);//n=1/0; if(n==0){ try{ Filefile=newFile("D:/file.txt"); InputStreamReaderreader=newInputStreamReader( newFileInputStream(file));//创建一个输入流对象 BufferedReaderbufferReader=newBufferedReader(reader); Stringline=""; Stringlines=null; line=bufferReader.readLine(); while(line!=null){ System.out.println(line); if(lines!=null){ lines=lines+"\n"+line+"\n"; }else{ lines=line+"\n"; } line=bufferReader.readLine(); } //JOptionPane.showMessageDialog(null,lines,"显示",JOptionPane.INFORMATION_MESSAGE); }catch(Exceptione1){ e1.printStackTrace(); } }else{ JOptionPane.showMessageDialog(null,"已取消该次查询"); } } //查询的功能 if(e.getActionCommand()=="查询"){ Filefile=newFile("D:/file.txt"); BufferedReaderreader=null; Stringtext7="-----我是分割线-----"; try{ //InputStreamReaderreader=newInputStreamReader( //newFileInputStream(file));//创建一个输入流对象 reader=newBufferedReader(newFileReader(file)); //暂时仅允许查询学号 Stringtext1=学号.getText(); intn=JOptionPane.showConfirmDialog(null,"查询的学号为:"+text1,"查询",JOptionPane.YES_NO_OPTION);//n=1/0; if(n==0){ Stringline=null; Stringlines=null; while((line=reader.readLine())!=null){ if(line.equals(text1)){ System.out.println(line); while(!(line.equals(text7))){ line=reader.readLine(); if(lines==null){ lines=line+"\n"; }else{ lines=lines+line+"\n"; } System.out.println(line); } }else{ continue; } } JOptionPane.showMessageDialog(null,lines,"查询",JOptionPane.INFORMATION_MESSAGE); reader.close(); }else{ JOptionPane.showMessageDialog(null,"已取消查询功能"); } }catch(Exceptione1){ e1.printStackTrace(); }finally{ if(reader!=null){ try{ reader.close(); }catch(IOExceptione1){ } } } } //删除的功能 if(e.getActionCommand()=="删除"){ Stringtext1=学号.getText().trim(); delete_Student(text1); } } } //------------注册监听------------- MyListenerlistener=newMyListener(); MyListenerlisten=newMyListener(); 显示.addActionListener(listener); 录入.addActionListener(listener); 查询.addActionListener(listener); 删除.addActionListener(listener); 男.addActionListener(listen); 女.addActionListener(listen); frame.setVisible(true); } /* *----------------------功能函数--------------------- */ //获取所有的数据 // publicstaticvoidgetAllMessage(){ try{ //添加JDBC驱动 Class.forName("com.mysql.jdbc.Driver"); Stringurl="jdbc:mysql://localhost/test"; Stringuser="root"; Stringpassword="root"; Connectionconnect=DriverManager.getConnection(url,user,password); Statementstmt=connect.createStatement(); System.out.println("successtoconnect"); //读取所有的数据 Stringsql1="select*fromStudentSystem"; ResultSetrs=stmt.executeQuery(sql1); System.out.println("学号\t姓名\t专业\t年级\t出生"); Stringlines=null; while(rs.next()){ System.out.print(rs.getString(1)+"\t"); System.out.print(rs.getString(2)+"\t"); System.out.print(rs.getString(3)+"\t"); System.out.print(rs.getString(4)+"\t"); System.out.print(rs.getString(5)+"\t"); System.out.println(); if(lines!=null){ lines=lines+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n\n"; }else{ lines="数据库中所有数据:\n\n"+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n"; } } JOptionPane.showMessageDialog(null,lines,"显示",JOptionPane.INFORMATION_MESSAGE); }catch(Exceptione){ e.printStackTrace(); } } /** *插入功能 */ publicstaticvoidinsert_Student(Stringstr1,Stringstr2,Stringstr3,Stringstr4,Stringstr5){ try{ //添加JDBC驱动 Class.forName("com.mysql.jdbc.Driver"); Stringurl="jdbc:mysql://localhost/test"; Stringuser="root"; Stringpassword="root"; //连接数据库 Connectionconnect=DriverManager.getConnection(url,user,password); Statementstmt=connect.createStatement(); System.out.println("successtoconnect"); /** * */ Stringlines=null; Stringsql="select*fromStudentSystem";//要执行的SQL Stringsql2="deletefromStudentSystemwhereid=?"; Stringsql3="insertintoStudentSystem(id,name,study,grade,birthplace)VALUES(?,?,?,?,?)";//SQL命令 PreparedStatementpst=(PreparedStatement)connect.prepareStatement(sql3); pst=connect.prepareStatement(sql3); pst.setNString(1,str1);//1,2,3,为对应上面的参数,切记!!!!!这里”12“传给了第一个问号代表的ID pst.setString(2,str2); pst.setString(3,str3); pst.setString(4,str4); pst.setString(5,str5); pst.executeUpdate(); JOptionPane.showMessageDialog(null,"该次录入成功!!"); ResultSetrs=stmt.executeQuery(sql); System.out.println("学号\t姓名\t专业\t年级\t出生"); while(rs.next()){ System.out.print(rs.getString(1)+"\t"); System.out.print(rs.getString(2)+"\t"); System.out.print(rs.getString(3)+"\t"); System.out.print(rs.getString(4)+"\t"); System.out.print(rs.getString(5)+"\t"); System.out.println(); if(lines!=null){ lines=lines+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n\n"; }else{ lines="数据库中所有数据:\n\n"+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n"; } } JOptionPane.showMessageDialog(null,lines,"显示",JOptionPane.INFORMATION_MESSAGE); }catch(Exceptione){ e.printStackTrace(); } } /** *------------删除功能------------ */ publicstaticvoiddelete_Student(Stringstr){ try{ //添加JDBC驱动 Class.forName("com.mysql.jdbc.Driver"); Stringurl="jdbc:mysql://localhost/test"; Stringuser="root"; Stringpassword="root"; //连接数据库 Connectionconnect=DriverManager.getConnection(url,user,password); Statementstmt=connect.createStatement(); System.out.println("successtoconnect"); //** Stringlines=null; Stringsql="select*fromStudentSystem";//要执行的SQL Stringsql2="deletefromStudentSystemwhereid=?"; PreparedStatementpst=(PreparedStatement)connect.prepareStatement(sql2); pst=connect.prepareStatement(sql2); pst.setString(1,str); pst.executeUpdate(); JOptionPane.showMessageDialog(null,"已删除该条记录","删除",JOptionPane.INFORMATION_MESSAGE); ResultSetrs=stmt.executeQuery(sql); System.out.println("学号\t姓名\t专业\t年级\t出生"); while(rs.next()){ System.out.print(rs.getString(1)+"\t"); System.out.print(rs.getString(2)+"\t"); System.out.print(rs.getString(3)+"\t"); System.out.print(rs.getString(4)+"\t"); System.out.print(rs.getString(5)+"\t"); System.out.println(); if(lines!=null){ lines=lines+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n\n"; }else{ lines="数据库中所有数据:\n\n"+"学号__姓名__专业__年级__出生\n"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3) +""+rs.getString(4)+""+rs.getString(5)+""+"\n"; } } JOptionPane.showMessageDialog(null,lines,"显示",JOptionPane.INFORMATION_MESSAGE); }catch(Exceptione){ e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。