




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
内蒙古工业大学信息工程学院9、实验结果及测试:图1 主界面 通过6个选项可以实现:对学生信息的添加,删除,修改,分页显示功能。图2 录入学生信息 图2显示了对学生信息的添加以及对异常的处理。图3 修改学生信息图3是对学生信息的修改以及对异常的处理。图4 查找学生信息 图4是查找学生的信息:分为按学号和姓名查找,以及对异常的处理。图5 显示学生信息图5是对数据库里的全部学生信息的分页显示。图6 数据库stuinformation表结构10、程序清单:Student类:package imut.cstd.j09_2.shiyan2;public class Student private int id;private String stunumber;private String stuname;private String stusex;private String stuclass;private String stumajor;private String stucollege;public Student()id=0;stunumber=null;stuname=null;stusex=null;stuclass=null;stumajor=null;stucollege=null;public Student(int id,String stunumber,String stuname,String stusex,String stuclass,String stumajor,String stucollege)this.id=id;this.stunumber=stunumber;this.stuname=stuname;this.stusex=stusex;this.stuclass=stuclass;this.stucollege=stucollege;public int getId()return id;public String getStunumber()return stunumber;public String getStuname()return stuname;public String getStusex()return stusex;public String getStuclass()return stuclass;public String getStumajor()return stumajor;public String getStucollege()return stucollege;public void setId(int id)this.id=id;public void setStunumber(String stunumber)this.stunumber=stunumber;public void setStuname(String stuname)this.stuname=stuname;public void setStusex(String stusex)this.stusex=stusex;public void SetStuclass(String stuclass)this.stuclass=stuclass;public void SetStumajor(String stumajor)this.stumajor=stumajor;public void SetStucollege(String stucollege)this.stucollege=stucollege;public boolean equals(Object s)if(s=null)return false;if(!(s instanceof Student)return false;Student temp=(Student)s;if(id=temp.id)&(stunumber.equals(temp.stunumber)&(stuname.equals(temp.stuname)&(stusex.equals(temp.stusex)&(stuclass.equals(temp.stuclass)&(stumajor.equals(temp.stumajor)&(stucollege.equals(temp.stumajor)return true;else return false;public String toString()String temp=null;temp=ID号t+学号t+ 姓名t+ 性别t+班级t+专业t+学院n+getId()+t+getStunumber()+ +getStuname()+t +getStusex()+t+getStuclass()+t+getStumajor()+t+getStucollege();return temp;Keyboard类:package imut.cstd.j09_2.shiyan2;import java.io.*;public class Keyboard public static int InputMenu()int option = 0;BufferedReader in = new BufferedReader(new InputStreamReader(System.in);tryoption=Integer.parseInt(in.readLine();catch(IOException e)e.printStackTrace();catch(NumberFormatException e)System.out.println(请输入正确的功能选项);return option; public static String InputString()BufferedReader in = new BufferedReader(new InputStreamReader(System.in);String temp=null;trytemp=in.readLine();catch(IOException e)e.getMessage();return temp;public static int InputInt()int temp = 0;BufferedReader in = new BufferedReader(new InputStreamReader(System.in);trytemp=Integer.parseInt(in.readLine();catch(IOException e)e.printStackTrace();catch(NumberFormatException e)System.out.println(请输入Id号);return temp;MyException类:package imut.cstd.j09_2.shiyan2;public class MyException extends Exception/* * */private static final long serialVersionUID = 1L;String message;public MyException(String ErrMessage)message=ErrMessage; public String getMessage()return message;SQLOperate类:package imut.cstd.j09_2.shiyan2;import java.sql.DriverManager;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class SQLOperate private static Student s;public static Connection getconnect()String DBDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver; String DBURL=jdbc:sqlserver:/localhost:1433;databaseName=stuinformation;Connection con= null;tryClass.forName(DBDRIVER);con=DriverManager.getConnection(DBURL,sa,zty);catch(ClassNotFoundException e)System.err.println(ClassNotFoundException:+e.getMessage();System.exit(1);catch(SQLException e)System.err.println(SQLException:+e.getMessage();return con;public static void AddStuInfo()PreparedStatement sql=null;Connection con=null;s = new Student();con=getconnect();boolean flag=false;doSystem.out.println(请输入Id号:); s.setId(Keyboard.InputInt(); if(Check(s.getId()=true) System.out.println(已有Id为+s.getId()+的学生,请重新输入!); flag=true; else break;while(flag);doSystem.out.println(请输入学号:);s.setStunumber(Keyboard.InputString(); if(Check(s.getId()=true) System.out.println(已有学号为+s.getStunumber()+的学生,请重新输入!); flag=true; else break;while(flag);System.out.println(请输入姓名:);s.setStuname(Keyboard.InputString();System.out.println(请输入性别:);s.setStusex(Keyboard.InputString();System.out.println(请输入班级:);s.SetStuclass(Keyboard.InputString();System.out.println(请输入专业:);s.SetStumajor(Keyboard.InputString();System.out.println(请输入学院:);s.SetStucollege(Keyboard.InputString();trysql=con.prepareStatement(insert into stuinfo +values(?,?,?,?,?,?,?);sql.setInt(1,s.getId();sql.setString(2,s.getStunumber();sql.setString(3,s.getStuname();sql.setString(4,s.getStusex();sql.setString(5,s.getStuclass();sql.setString(6,s.getStumajor();sql.setString(7,s.getStucollege(); if(sql.executeUpdate()!=1) throw new MyException(添加错误!); System.out.println(信息添加成功!);catch(SQLException e)System.out.println(e.getMessage();catch(MyException e)System.out.println(e.getMessage();Finally(sql,con);System.out.println(你添加的学生信息是:);System.out.println(s);public static void NameSeekStuInfo()PreparedStatement sql=null;Connection con=null;ResultSet rs=null;String name=null;boolean flag=true;try System.out.println(请输入要查找学生的姓名:); name=Keyboard.InputString(); if(Check(name)=false) throw new MyException(没有姓名是+name+的学生!); catch(MyException e)System.out.println(e.getMessage();flag=false;if(flag)trycon=getconnect(); sql=con.prepareStatement(select * from stuinfo where stuname = +name+); rs=sql.executeQuery(); System.out.println(ID号t+学号t+ 姓名t+ 性别t+班级t+专业t+学院); while(rs.next() System.out.print(rs.getInt(id)+t); System.out.print(rs.getString(stunumber) + ); System.out.print(rs.getString(stuname) + ); System.out.print(rs.getString(stusex) + t); System.out.print(rs.getString(stuclass); System.out.print(rs.getString(stumajor); System.out.println(rs.getString(stucollege); catch(SQLException e) System.err.println(SQLException+e.getMessage(); finally if(rs!=null) try rs.close(); catch(SQLException e) Finally(sql,con); public static void NumberSeekStuInfo() PreparedStatement sql=null;Connection con=null;ResultSet rs=null;String number=null;boolean flag=true;try System.out.println(请输入要查找学生的学号:); number=Keyboard.InputString(); if(Check(number)=false) throw new MyException(没有学号为+number+的学生!); catch(MyException e)System.out.println(e.getMessage();flag=false;if(flag)trycon=getconnect(); sql=con.prepareStatement(select * from stuinfo where stunumber = +number+); rs=sql.executeQuery(); System.out.println(ID号t+学号t+ 姓名t+ 性别t+班级t+专业t+学院); while(rs.next() System.out.print(rs.getInt(id)+t); System.out.print(rs.getString(stunumber) + ); System.out.print(rs.getString(stuname) + ); System.out.print(rs.getString(stusex) + t); System.out.print(rs.getString(stuclass); System.out.print(rs.getString(stumajor); System.out.println(rs.getString(stucollege); catch(SQLException e) System.err.println(SQLException+e.getMessage(); finally if(rs!=null) try rs.close(); catch(SQLException e) Finally(sql,con); public static void NumberDeleteStuInfo()PreparedStatement sql=null;Connection con=null;boolean flag=false;String number=null;doSystem.out.println(请输要删除学生的学号:); number=Keyboard.InputString(); if(Check(number)=false) System.out.println(没有学号为+number+的学生,请重新输入!); flag=true; else break;while(flag);trycon=getconnect();sql=con.prepareStatement(delete from stuinfo where stunumber=+number+);if(sql.executeUpdate()!=1) throw new MyException(删除错误!); System.out.println(信息删除成功!);catch(SQLException e)System.err.println(SQLException+e.getMessage();catch(MyException e)System.out.println(e.getMessage();Finally(sql,con); public static void IdDeleteStuInfo()PreparedStatement sql=null;Connection con=null;boolean flag=false;int id=0;doSystem.out.println(请输要删除学生的ID号:); id=Keyboard.InputInt(); if(Check(id)=false) System.out.println(没有Id号为+id+的学生,请重新输入!); flag=true; else break;while(flag);trycon=getconnect();sql=con.prepareStatement(delete from stuinfo where id=+id+);if(sql.executeUpdate()!=1) throw new MyException(删除错误!); System.out.println(信息删除成功!);catch(SQLException e)System.err.println(SQLException+e.getMessage();catch(MyException e)System.out.println(e.getMessage();Finally(sql,con);public static void AlterStuInfo() PreparedStatement sql=null;Connection con=null;s=new Student(); boolean flag=false;int id=0;doSystem.out.println(请输入要修改学生的Id号:); id=Keyboard.InputInt(); if(Check(id)=false) System.out.println(没有Id为+id+的学生,请重新输入!); flag=true; else break;while(flag);doSystem.out.println(请输入要修改后学生的Id号:); s.setId(Keyboard.InputInt(); if(Check(s.getId() System.out.println(已有Id为+s.getId()+的学生,请重新输入!); flag=true; else break;while(flag);doSystem.out.println(请输入要修改后学生的学号:); s.setStunumber(Keyboard.InputString(); if(Check(s.getStunumber() System.out.println(已有学号为+s.getStunumber()+的学生,请重新输入!); flag=true; else break;while(flag);System.out.println(请输入要修改后学生的姓名:);s.setStuname(Keyboard.InputString();System.out.println(请输入要修改后学生的性别:);s.setStusex(Keyboard.InputString();System.out.println(请输入要修改后学生的班级:);s.SetStuclass(Keyboard.InputString();System.out.println(请输入要修改后学生的专业:);s.SetStumajor(Keyboard.InputString();System.out.println(请输入要修改后学生的学院:);s.SetStucollege(Keyboard.InputString();trycon=getconnect();sql=con.prepareStatement(update stuinfo set id=+s.getId()+,stunumber=+s.getStunumber()+,stuname=+s.getStuname()+,stusex=+s.getStusex()+,stuclass=+s.getStuclass()+,stumajor=+s.getStumajor()+,stucollege=+s.getStucollege()+ where id =+id+);if(sql.executeUpdate()!=1) throw new MyException(修改错误!); System.out.println(信息修改成功!);catch(SQLException e)System.err.println(SQLException+e.getMessage();catch(MyException e)System.out.println(e.getMessage();Finally(sql,con);System.out.println(你修改后学生的信息是:);System.out.println(s);public static void ShowStuInfo()PreparedStatement sql=null;Connection con=null;ResultSet rs=null;int row=5;int count=0;trycon=getconnect(); sql=con.prepareStatement(select * from stuinfo); rs=sql.executeQuery(); System.out.println(tt全部学的信息如下:); System.out.println(ID号t+学号t+ 姓名t+ 性别t+班级t+专业t+学院); while(rs.next() if(count=row) System.out.println(=); System.out.println(ID号t+学号t+ 姓名t+ 性别t+班级t+专业t+学院); count=0; System.out.print(rs.getInt(id)+t); System.out.print(rs.getString(stunumber) + ); System.out.print(rs.getString(stuname) + ); System.out.print(rs.getString(stusex) + t); System.out.print(rs.getString(stuclass); System.out.print(rs.getString(stumajor); System.out.println(rs.getString(stucollege); count+; catch(NullPointerException e) System.out.println(e.getMessage(); catch(SQLException e) System.err.println(SQLException+e.getMessage(); finally if(rs!=null) try rs.close(); catch(SQLException e) Finally(sql,con); public static boolean Check(int id)PreparedStatement sql=null;Connection con=null;ResultSet rs=null;boolean flag=true;trycon=getconnect(); sql=con.prepareStatement(select * from stuinfo where id=+id+); rs=sql.executeQuery(); if(rs.next()=false)flag=false;catch(SQLException e)System.err.println(e.getMessage();finally if(rs!=null) try rs.close(); catch(SQLException e) Finally(sql,con); return flag;public static boolean Check(String temp)PreparedStatement sql=null;Connection con=null;ResultSet rs=null;boolean flag=true;trycon=getconnect(); sql=con.prepareStatement(select * from stuinfo where stunumber=+temp+ or stuname=+temp+); rs=sql.executeQuery(); if(rs.next()=false)flag=false;catch(SQLException e)System.err.println(e.getMessage();finally if(rs!=null) try rs.close(); catch(SQLException e) Finally(sql,con); return flag;public static void Finally(PreparedStatement sql,Connection con)if(sql!=null)trysql.close();catch(SQLException e)if(con!=null)trycon.close();catch(SQLException e) Menu类:package imut.cstd.j09_2.shiyan2;public class Menu public static void mainmenu()doSystem.out.println(=);System.out.println( +欢迎登陆大学生基本信息系统);System.out.println(t+1.录入基本信息);System.out.println(t+2.删除学生信息);Syst
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 幼儿园中班教案《咏柳》
- 2026届西藏自治区日喀则市南木林高中高二化学第一学期期中监测试题含解析
- 高端人才选拔门户:空乘面试题库精 编
- 警校面试实战模拟题:职业素养与能力提升
- 剖腹产术后药水护理规范
- 系统式家庭治疗
- 小学生学科讲解
- 伤口医院感染防控与管理
- 相机工作原理与使用技巧
- 如何构建与维护高效团队
- 矿石运输居间合同范本
- T/CSWSL 002-2018发酵饲料技术通则
- 涉案资金退还协议书
- 安宁疗护之症状管理
- 《神经影像解析》课件
- 电力建设水电工程智慧工地技术规范
- 2025年初级消防员试题及答案
- 2025年四川省成都市锦江区中考数学二诊试卷(含部分答案)
- 食源性疾病防治知识
- 向上沟通培训课件
- 网站篡改演练方案
评论
0/150
提交评论