当前位置: 移动技术网 > IT编程>开发语言>Java > Java学生信息管理系统设计(数据库版)

Java学生信息管理系统设计(数据库版)

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了数据库版的java学生信息管理系统,供大家参考,具体内容如下

package student_system; 
 
 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.sql.connection; 
import java.sql.drivermanager; 
import java.sql.preparedstatement; 
import java.sql.resultset; 
import java.sql.statement; 
import java.util.*; 
/*class stu implements java.io.serializable{ 
 string number,name,specialty,grade,borth,sex; 
 public stu(){}; 
 public void setnumber(string number){ this.number=number;} 
 public string getnumber(){ return number;} 
 public void setname(string name){ this.name=name;} 
 public string getname(){ return name;} 
 public void setsex(string sex){ this.sex=sex;} 
 public string getsex(){ return sex;} 
 public void setspecialty(string specialty){ this.specialty=specialty;} 
 public string getspecialty(){ return specialty;} 
 public void setgrade(string grade){ this.grade=grade;} 
 public string getgrade(){ return grade;} 
 public void setborth(string borth){ this.borth=borth;} 
 public string getborth(){ return borth;} 
}*/ 
 
public class studentsystem extends jframe{ 
 public static void main(string[] args){ 
  jframe frame = new jframe(); 
  frame.settitle("信息管理系统"); 
  frame.setsize(500, 500); 
  frame.setdefaultcloseoperation(jframe.exit_on_close); 
  container container = frame.getcontentpane(); 
  container.setlayout(new flowlayout()); 
 jlabel lb=new jlabel("录入请先输入记录,查询、删除请先输入学号,修改是对查询" + 
   "内容改后的保存!"); 
 final jtextfield 学号; 
final jtextfield 姓名; 
final jtextfield 专业; 
final jtextfield 年级; 
final jtextfield 出生; 
 final jradiobutton 男; 
final jradiobutton 女; 
 buttongroup group=null; 
 jbutton 录入,查询,删除,修改,显示; 
 jpanel p1,p2,p3,p4,p5,p6,pv,ph; 
 学号=new jtextfield(10); 
 姓名=new jtextfield(10); 
 专业=new jtextfield(10); 
 年级=new jtextfield(10); 
 出生=new jtextfield(10); 
 group=new buttongroup(); 
 男=new jradiobutton("男"); 
 女=new jradiobutton("女"); 
 group.add(男); 
 group.add(女); 
 录入=new jbutton("录入"); 
 查询=new jbutton("查询"); 
 删除=new jbutton("删除"); 
 修改=new jbutton("修改"); 
 显示=new jbutton("显示"); 
 修改.setenabled(false); 
  
 //添加输入框及文本框 
 p1=new jpanel(); 
 p1.add(new jlabel("学号:",jlabel.center)); 
 p1.add(学号); 
 p2=new jpanel(); 
 p2.add(new jlabel("姓名:",jlabel.center)); 
 p2.add(姓名); 
 p3=new jpanel(); 
 p3.add(new jlabel("性别:",jlabel.center)); 
 p3.add(男); 
 p3.add(女); 
 p4=new jpanel(); 
 p4.add(new jlabel("专业:",jlabel.center)); 
 p4.add(专业); 
 p5=new jpanel(); 
 p5.add(new jlabel("年级:",jlabel.center)); 
 p5.add(年级); 
 p6=new jpanel(); 
 p6.add(new jlabel("出生:",jlabel.center)); 
 p6.add(出生); 
 pv=new jpanel(); 
 pv.setlayout(new gridlayout(6,1)); 
 pv.add(p1); 
 pv.add(p2); 
 pv.add(p3); 
 pv.add(p4); 
 pv.add(p5); 
 pv.add(p6); 
 ph=new jpanel(); 
 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); 
 
  
 class mylistener implements actionlistener { 
  public void actionperformed (actionevent e) { 
    
    
   //判断选中是男是女 
    /*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() == "录入") { 
     
    string text1 = 学号.gettext().trim(); 
    string text2 = 姓名.gettext(); 
    string text3 = 专业.gettext(); 
    string text4 = 年级.gettext(); 
    string text5 = 出生.gettext(); 
    string text6 = null; 
    string text7 = "- - - - -我是分割线 - - - - -"; 
    if(男.isselected()) { 
     text6 = "男"; 
    } 
     
    if(女.isselected()) { 
     text6 = "女"; 
    } 
     
     
    //用texts包含此次所有录入信息 
    string texts = "\n\n学号:" + text1 + "\n\n" + "姓名:" + text2 + "\n\n" + "专业:" + text3 + "\n\n" + "年级:" + text4 + "\n\n" + "出生:" + text5 + "\n\n" + "性别:" + text6 + "\n\n" ; 
     
    //显示框 
    int m = 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() == "显示") { 
     
     
    int n = joptionpane.showconfirmdialog(null, "是否显示所有记录" , "显示" , joptionpane.yes_no_option);//n = 1/0; 
     
    if(n ==0 ) { 
    try { 
      
     file file = new file("d:/file.txt"); 
     inputstreamreader reader = new inputstreamreader( 
       new fileinputstream(file));//创建一个输入流对象 
      
     bufferedreader bufferreader = new bufferedreader(reader); 
      
     string line = ""; 
     string lines = 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 (exception e1) { 
     e1.printstacktrace(); 
    } 
    } else { 
     joptionpane.showmessagedialog(null, "已取消该次查询"); 
    } 
     
   } 
    
   //查询 的功能 
   if(e.getactioncommand() == "查询") { 
    file file = new file("d:/file.txt"); 
    bufferedreader reader = null; 
    string text7 = "- - - - -我是分割线 - - - - -"; 
    try { 
      
     //inputstreamreader reader = new inputstreamreader( 
      // new fileinputstream(file));//创建一个输入流对象 
      
      reader = new bufferedreader(new filereader(file)); 
      
     //暂时仅允许查询学号 
     string text1 = 学号.gettext(); 
     int n = joptionpane.showconfirmdialog(null, "查询的学号为:" + text1 , "查询" , joptionpane.yes_no_option);//n = 1/0; 
     if(n == 0){ 
     string line = null; 
     string lines = 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 (exception e1) { 
     e1.printstacktrace(); 
    } finally { 
     if(reader != null) { 
      try { 
       reader.close(); 
      }catch (ioexception e1){ 
        
      } 
     } 
    } 
   } 
    
    
   //删除 的功能 
   if(e.getactioncommand() == "删除") { 
    string text1 = 学号.gettext().trim(); 
    delete_student(text1); 
   } 
    
  }  
 } 
 
 //------------注册监听------------- 
  
 mylistener listener = new mylistener(); 
 mylistener listen = new mylistener(); 
 显示.addactionlistener(listener); 
 录入.addactionlistener(listener); 
 查询.addactionlistener(listener); 
 删除.addactionlistener(listener); 
 男.addactionlistener(listen); 
 女.addactionlistener(listen); 
 frame.setvisible(true); 
 } 
  
 /* 
  * ---------------------- 功能函数 --------------------- 
 */  
  
 //获取所有的数据 
 // 
 public static void getallmessage(){ 
 try { 
  //添加jdbc驱动 
  class.forname("com.mysql.jdbc.driver"); 
  string url = "jdbc:mysql://localhost/test"; 
  string user = "root"; 
  string password = "root"; 
  connection connect = drivermanager.getconnection(url,user,password); 
  statement stmt = connect.createstatement(); 
  system.out.println("success to connect "); 
   
  //读取所有的数据 
    
  string sql1 = "select * from studentsystem"; 
  resultset rs = stmt.executequery(sql1); 
  system.out.println("学号\t姓名\t专业\t年级\t出生"); 
  string lines = 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 (exception e) { 
  e.printstacktrace(); 
 } 
  
 } 
  
 /** 
  * 插入功能 
  */ 
  
  public static void insert_student(string str1,string str2,string str3,string str4,string str5){ 
   try { 
    //添加jdbc驱动 
    class.forname("com.mysql.jdbc.driver"); 
    string url = "jdbc:mysql://localhost/test"; 
    string user = "root"; 
    string password = "root"; 
    //连接数据库 
    connection connect = drivermanager.getconnection(url,user,password); 
    statement stmt = connect.createstatement(); 
    system.out.println("success to connect ");     
 
    /** 
     * 
     */ 
  
    string lines = null; 
    string sql = "select * from studentsystem";//要执行的sql 
    string sql2 = "delete from studentsystem where id =?"; 
    string sql3 = "insert into studentsystem(id,name,study,grade,birthplace)values(?,?,?,?,?)";//sql命令 
     
     
    preparedstatement pst = (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, "该次录入成功!!"); 
     
    resultset rs = 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 (exception e) { 
    e.printstacktrace(); 
   } 
    
  } 

  /** 
   * ------------删除功能------------ 
   */ 
  public static void delete_student(string str){ 
   try { 
    //添加jdbc驱动 
    class.forname("com.mysql.jdbc.driver"); 
    string url = "jdbc:mysql://localhost/test"; 
    string user = "root"; 
    string password = "root"; 
    //连接数据库 
    connection connect = drivermanager.getconnection(url,user,password); 
    statement stmt = connect.createstatement(); 
    system.out.println("success to connect "); 
     
  
    //** 
    string lines = null; 
    string sql = "select * from studentsystem";//要执行的sql 
    string sql2 = "delete from studentsystem where id =?"; 
    preparedstatement pst = (preparedstatement)connect.preparestatement(sql2); 
    pst = connect.preparestatement(sql2); 
    pst.setstring(1,str); 
    pst.executeupdate(); 
     
    joptionpane.showmessagedialog(null, "已删除该条记录", "删除" ,joptionpane.information_message); 
     
    resultset rs = 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 (exception e) { 
    e.printstacktrace(); 
   } 
    
  } 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网