实验9学生管理系统(选项卡的利用).doc_第1页
实验9学生管理系统(选项卡的利用).doc_第2页
实验9学生管理系统(选项卡的利用).doc_第3页
实验9学生管理系统(选项卡的利用).doc_第4页
实验9学生管理系统(选项卡的利用).doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java程序设计课程实验报告(第9次)学院:数学与计算机学院姓名: 学号:专业:计算机科学与技术班级:15级计本实验地点:分析测试中心6A-4实验时间:指导教师: 实验名称:学生管理系统所使用的开发工具及环境:JDK1.8+Eclpise实验目的:1. 利用 JDBC 执行基本 SQL 操作,如插入记录、删除记录、更新记录 : 2. 了解 Statement 接口的作用和常用的方法 :3. 了解PreparedStatement接口的作用和常用的方法 : 4. 比较 Statement 接口和PreparedStatement的主要特点。实验内容:1.(1) 创建一个名为mydb数据库,并建立如下所示的数据库表student,如图1所示图1 student表结构(2)在Student表中录入6条数据101 张三 89.0 78.0 77.0102 李四 66.0 77.0 88.0103 王五 88.0 99.0 88.0104 张丽 77.0 78.0 65.0105 王琴 66.0 77.0 90.0106 李斯文 88.0 99.0 67.0(3)使用con=DriverManager.getConnection(jdbc:mysql:/localhost:3306/mydb,root,root);与数据库表建立连接。(4)创建 Statement 对象。Statement statement=connection.createStatement(); (5)实现数据查询。ResultSetrs=statementexecuteQuery(select * from student);(6)在控制台展现查询结果。while(rs.next()System.out.println(rs.getString(姓名);2.创建文件 studentManager.java,该程序通过Statement 接口实现对数据表 student 的数据录入 ( 插入 ) 。运行结果如图2所示。图2 录入记录3. 修改和完善2中的程序StudentManagerment.java 程序。同样利用 Statement 接口的对象stmt实现数据的 浏览 、 更新 和 删除 。 浏览 功能是通过选择 浏览 按钮,然后将数据表中的记录显示出来,运行结果如图3所示。图3浏览记录 更新 操作是通过用户输入一个学生的学号,然后显示该生的信息,如果需要修改,在界面修改后,单击 修改 按钮,就可以执行修改记录信息,学生记录更新后学生显示记录的面板内容清除。运行结果如图4所示。图4修改记录 删除 操作是通过用户输入一个学生的学号后,按回车键,判断该生是否存在,如果存在显示记录信息。如果单击 删除 按钮,就可以将数据表 student 中的该条记录删除,并将记录显示清除。运行结果如图5所示。图5删除记录实验结果及代码:1.创建的表:2.连接数据库以及其他功能相关代码:/MyConnection类和Student类importjava.sql.*;publicclassMyConnection Connection con=null;Statement stmt=null;PreparedStatementpsmt=null;publicvoidgetConnection()try Class.forName(com.mysql.jdbc.Driver);con = DriverManager.getConnection(jdbc:mysql:/:3306/mydb, root, 123456); catch (ClassNotFoundExceptione) / TODO Auto-generated catch blocke.printStackTrace(); catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();publicvoid insert(Object stu)getConnection();try psmt=con.prepareStatement(insert into student values(?,?,?,?,?);psmt.setInt(1, (Student) stu).getId();psmt.setString(2,(Student) stu).getName();psmt.setDouble(3, (Student) stu).getMath();psmt.setDouble(4, (Student) stu).getPhysical();psmt.setDouble(5, (Student) stu).getEnglish();psmt.executeUpdate(); catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();finallytry if(psmt!=null)psmt.close();psmt=null;if(con!=null)con.close();con=null; catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();publicResultSet query()getConnection();ResultSetrs=null;try stmt=con.createStatement();rs=stmt.executeQuery(select * from student); catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();returnrs;publicResultSet query(intid)getConnection();ResultSetrs=null;try stmt=con.createStatement();rs=stmt.executeQuery(select * from student where 学号=+id+); catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();returnrs;publicvoid update(Student stu)getConnection();try psmt=con.prepareStatement(update student set 姓名=?,数学成绩=?,物理成绩=?,英语成绩=? where 学号=?);psmt.setString(1,stu.getName();psmt.setDouble(2, stu.getMath();psmt.setDouble(3, stu.getPhysical();psmt.setDouble(4, stu.getEnglish();psmt.setInt(5,stu.getId();psmt.executeUpdate(); catch (SQLExceptione) System.out.println(错误);importjava.io.Serializable;publicclass Studentintid;String name;doublemath;doublephysical;doubleenglish;public Student(intid, String name, doublemath, doublephysical, doubleenglish) super();this.id = id; = name;this.math = math;this.physical = physical;this.english = english;publicintgetId() returnid;publicvoidsetId(intid) this.id = id;public String getName() returnname;publicvoidsetName(String name) = name;publicdoublegetMath() returnmath;publicvoidsetMath(doublemath) this.math = math;publicdoublegetPhysical() returnphysical;publicvoidsetPhysical(doublephysical) this.physical = physical;publicdoublegetEnglish() returnenglish;publicvoidsetEnglish(doubleenglish) this.english = english;Overridepublic String toString() returnStudent id= + id + , name= + name + , math= + math + , physical= + physical + , english=+ english + ;3.主窗口以及录入记录功能代码:/MainWindow类和InsertPanel类importjava.awt.*;importjavax.swing.*;importjavax.swing.event.*;publicclassMainWindowextendsJFrameimplementsChangeListener JTabbedPaneTabbedPane=newJTabbedPane(JTabbedPane.TOP) ; InsertPanelp1=newInsertPanel(); QueryPanelp2=newQueryPanel();ModifyPanelp3=newModifyPanel();DeletePanelp4=newDeletePanel(); publicvoidinit()TabbedPane.add(录入记录, p1) ;TabbedPane.add(浏览记录, p2) ;TabbedPane.add(修改记录, p3) ;TabbedPane.add(删除记录, p4) ; this.add(TabbedPane,BorderLayout.CENTER); TabbedPane.addChangeListener(this);publicMainWindow()init();this.setTitle(学生管理系统);this.setVisible(true);this.setBounds(200, 200, 400, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);OverridepublicvoidstateChanged(ChangeEventarg0) if(TabbedPane.getSelectedIndex()=0)TabbedPane.setEnabledAt(0, true);elseif(TabbedPane.getSelectedIndex()=1)TabbedPane.setEnabledAt(1, true);elseif(TabbedPane.getSelectedIndex()=2)TabbedPane.setEnabledAt(2, true);elseif(TabbedPane.getSelectedIndex()=3)TabbedPane.setEnabledAt(3, true);publicstaticvoid main(String args) newMainWindow();importjava.awt.event.*;importjavax.swing.*;publicclassInsertPanelextendsJPanelimplementsActionListener JTextFieldidTxt,nameTxt,mathTxt,physiTxt,englishTxt;JButtonaddButton;JLabelidLabel,nameLabel,mathLable,physiLabel,englishLabel;Box box,box1,box2;publicInsertPanel()init();publicvoidinit()idTxt=newJTextField(11);nameTxt=newJTextField(11);mathTxt=newJTextField(11);physiTxt=newJTextField(11);englishTxt=newJTextField(11);addButton =newJButton(录入);idLabel=newJLabel(学号);nameLabel=newJLabel(姓名);mathLable=newJLabel(数学成绩);physiLabel=newJLabel(物理成绩);englishLabel=newJLabel(英语成绩);box1=Box.createVerticalBox();box1.add(idLabel);box1.add(Box.createVerticalStrut(10);box1.add(nameLabel);box1.add(Box.createVerticalStrut(10);box1.add(mathLable);box1.add(Box.createVerticalStrut(10);box1.add(physiLabel);box1.add(Box.createVerticalStrut(10);box1.add(englishLabel);box2=Box.createVerticalBox();box2.add(newJLabel( );box2.add(newJLabel( );box2.add(idTxt);box2.add(Box.createVerticalStrut(5);box2.add(nameTxt);box2.add(Box.createVerticalStrut(5);box2.add(mathTxt);box2.add(Box.createVerticalStrut(5);box2.add(physiTxt);box2.add(Box.createVerticalStrut(5);box2.add(englishTxt);box2.add(Box.createVerticalStrut(5);box2.add(addButton);box=Box.createHorizontalBox();box.add(box1);box.add(Box.createHorizontalStrut(15);box.add(box2);this.add(box);addButton.addActionListener(this);OverridepublicvoidactionPerformed(ActionEventarg0) intid=Integer.parseInt(idTxt.getText().trim(); String name=nameTxt.getText().trim(); doublemath=Double.parseDouble(mathTxt.getText().trim();doublephysical=Double.parseDouble(physiTxt.getText().trim();doubleenglish=Double.parseDouble(englishTxt.getText().trim(); Student student=new Student(id, name, math, physical, english);MyConnectionMC=newMyConnection();MC.insert(student);idTxt.setText();nameTxt.setText();mathTxt.setText();physiTxt.setText();englishTxt.setText();4.浏览记录功能代码:/QueryPanel类importjava.awt.*;importjava.awt.event.*;importjava.sql.*;importjava.util.*;importjavax.swing.*;publicclassQueryPanelextendsJPanelimplementsActionListener JTabletable;Object name=学号,姓名,数学,物理,英语;Object a;ResultSetresultSet=null;MyConnectionMC=newMyConnection();LinkedListlist=null;JButtonbutton=newJButton(刷新);publicvoidgetList()resultSet= MC.query();list=newLinkedList();if(resultSet!=null)try while(resultSet.next()intid=resultSet.getInt(1);String name=resultSet.getString(2);doublemath=resultSet.getDouble(3);doublephysical=resultSet.getDouble(4);doubleenglish=resultSet.getDouble(5);Student student=new Student(id, name, math, physical, english);list.add(student); catch (SQLExceptione) e.printStackTrace();publicvoidshowTable()getList();a=new Objectlist.size()name.length;for(inti=0;ilist.size();i+)Student s=list.get(i);ai0=s.getId();ai1=s.getName();ai2=s.getMath();ai3=s.getPhysical();ai4=s.getEnglish();this.removeAll();table=newJTable(a, name); add(newJScrollPane(table),BorderLayout.CENTER);this.add(button, BorderLayout.SOUTH);publicQueryPanel()this.setLayout(newBorderLayout();this.add(button,BorderLayout.SOUTH);resultSet= MC.query();if(resultSet=null)a=new Object2name.length;table=newJTable(a, name);add(newJScrollPane(table),BorderLayout.CENTER);elseshowTable();try if(resultSet!=null) resultSet.close();resultSet=null; catch (SQLExceptione) / TODO Auto-generated catch blocke.printStackTrace();button.addActionListener(this);OverridepublicvoidactionPerformed(ActionEvente) showTable();4.修改记录功能代码:/ModifyPanel类importjava.awt.event.*;importjava.sql.*;importjava.sql.SQLException;importjavax.swing.*;publicclassModifyPanelextendsJPanelimplementsActionListenerJTextFieldidTxt,nameTxt,mathTxt,physiTxt,englishTxt,updateIdTxt;JButtonupdateButton,query;JLabelidLabel,nameLabel,mathLable,physiLabel,englishLabel,updateIdLable;Box box,box1,box2;MyConnectionMC=newMyConnection();publicModifyPanel()init();publicvoidinit()idTxt=newJTextField(11);nameTxt=newJTextField(11);mathTxt=newJTextField(11);physiTxt=newJTextField(11);englishTxt=newJTextField(11);updateIdTxt=newJTextField(11);updateButton =newJButton(修改);query=newJButton(查询);idLabel=newJLabel(学号);nameLabel=newJLabel(姓名);mathLable=newJLabel(数学成绩);physiLabel=newJLabel(物理成绩);englishLabel=newJLabel(英语成绩);updateIdLable=newJLabel(查询学号);box1=Box.createVerticalBox();box1.add(idLabel);box1.add(Box.createVerticalStrut(10);box1.add(nameLabel);box1.add(Box.createVerticalStrut(10);box1.add(mathLable);box1.add(Box.createVerticalStrut(10);box1.add(physiLabel);box1.add(Box.createVerticalStrut(10);box1.add(englishLabel);box1.add(Box.createVerticalStrut(10);box1.add(updateIdLable);query.setSize(1, 1);box1.add(query);box2=Box.createVerticalBox();box2.add(idTxt);box2.add(Box.createVerticalStrut(5);box2.add(nameTxt);box2.add(Box.createVerticalStrut(5);box2.add(mathTxt);box2.add(Box.createVerticalStrut(5);box2.add(physiTxt);box2.add(Box.createVerticalStrut(5);box2.add(englishTxt);box2.add(Box.createVerticalStrut(5);box2.add(updateIdTxt);box2.add(Box.createVerticalStrut(5);box2.add(updateButton);box=Box.createHorizontalBox();box.add(box1);box.add(Box.createHorizontalStrut(15);box.add(box2);this.add(box);query.addActionListener(this);updateButton.addActionListener(this);publicvoidupdateStudent()intid=Integer.parseInt(idTxt.getText().trim(); String name=nameTxt.getText().trim();doublemath=Double.parseDouble(mathTxt.getText().trim();doublephysical=Double.parseDouble(physiTxt.getText().trim();doubleenglish=Double.parseDouble(englishTxt.getText().trim(); Student student=new Student(id, name, math, physical, english);MC.update(student);OverridepublicvoidactionPerformed(ActionEvente) if(e.getSource()=query)intid=Integer.parseInt(updateIdTxt.getText().trim();ResultSetrs=MC.query(id);try rs.next();if(rs!=null)idTxt.setText(id+);nameTxt.setText(rs.getString(2);mathTxt.setText(rs.getDouble(3)+);physiTxt.setText(rs.getDouble(4)+);englishTxt.setText(rs.getDouble(5)+);elseif(rs=null)System.out.println(id); catch (SQLExceptione1) e1.printStackTrace();elseif(e.getSource()=updateButton)updateStudent();5.删除记录功能代码:/DeletePanel类importjava.awt.event.*;importjava.sql.*;importjavax.swing.*;publicclassDeletePanelextendsJPanelimplementsActionListenerJTextFieldidTxt,nameTxt,mathTxt,physiTxt,englishTxt,DeleteIdTxt;JButtonDeleteButton,query;JLabelidLabel,nameLabel,mathLable,physiLabel,englishLabel,updateIdLable;Box box,box1,box2;MyConnectionMC=newMyConnection();publicDeletePanel()init();publicvoidinit()idTxt=newJTextField(11);nameTxt=newJTextField(11);mathTxt=newJTextField(11);physiTxt=newJTextField(11);englishTxt=newJTextField(11);DeleteIdTxt=newJTextField(11);DeleteButton =newJButton(删除);query=newJButton(查询);idLabel=newJLabel(学号);nameLabel=newJLabel(姓名);mathLable=newJLabel(数学成绩);physiLabel=newJLabel(物理成绩);englishLabel=newJLabel(英语成绩);updateIdLable=newJLabel(查询学号);box1=Box.createVerticalBox();box1.add(idLabel);box1.add(Box.createVerticalStrut(10);box1.add(nameLabel);box1.add(Box.createVerticalStrut(10);box1.add(mathLable);box1.add(Box.createVerticalStrut(10);box1.add(physiLabel);box1.add(Box.createVerticalStrut(10);box1.add(englishLabel);box1.add(Box.createVerticalStrut(10);box1.add(updateIdLable);query.setSize(1, 1);box1.add(query);box2=Box.createVerticalBox();box2.add(idTxt);box2.add(Box.createVerticalStrut(5);box2.add(nameTxt);box2.add(Box.createVerticalStrut(5);box2.add(mathTxt);box2.add(Box.createVerticalStrut(5);box2.add(physiTxt);box2.add(Box.createVerticalStrut(5);box2.add(en

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论