青岛理工JAVA实验(共17页).doc
精选优质文档-倾情为你奉上课程实验报告课程名称Java应用技术班级计算121实验日期姓名蔡笃骁学号实验成绩实验名称实验5:数据库应用设计实验目的及要求1掌握如何创建数据源;2掌握JDBC-ODBC桥接器和JDBC的概念;3掌握查询记录;4掌握更新、添加、删除记录。实验环境操作系统:WindowsIDE:JDK 或者 MyEclipse实验内容(1)学生信息管理系统。学生成绩表Student(Sno 字符串,长度9, Sname 字符串,长度10, Class 字符串,长度10, Age 整型, Sex 字符串,长度2)实现如下功能,要求用图形化界面去实现:A输入若干个学生的信息到Student表;B修改制定学号学生的基本信息;C删除指定学号的学生基本信息;D按照性别找出所有相应的学生基本信息;E按照年龄段找出所有相应的学生基本信息,例如19,21的学生。调试过程及实验结果实验执行结果:运行程序,出现界面,确定进入,进入后进行选择 1、点击“输入信息”,出现界面,输入信息,确定后有提示框 输入多条信息后在数据库中打开表为:2、点击“修改信息”, 出现界面,输入信息,确定后有提示框 修改后打开在数据库中的表为:3、点击“删除信息”, 出现界面,输入删除学号,确定后有提示框删除后打开在数据库中的表为4、对学生性别进行查找,在数据库中显示为:5、对年龄段进行查找,在数据库中显示为: 总结通过本次试验,知道了如何创建数据源,和JDBC-ODBC桥接器和JDBC的概念,理解了查询记录和更新、添加、删除记录。但是还有许多不足的地方,以后还得要加强这方法的学习。专心-专注-专业附录import java.sql.*;import javax.swing.JFrame;import java.awt.FlowLayout;import java.awt.GridLayout;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JOptionPane;import javax.swing.JTextField;public class z extends JFrame public String JDriver ="com.microsoft.sqlserver.jdbc.SQLServerDriver" public String conURL ="jdbc:sqlserver:/localhost:1433;databaseName=test;user=sa;password=123"class Display extends JFrame /菜单界面 JButton b1=new JButton("输入信息");JButton b2=new JButton("修改信息");JButton b3=new JButton("删除信息");JButton b4=new JButton("按照性别查找学生信息");JButton b5=new JButton("按照年龄查找学生信息");JLabel a=new JLabel("请选择想要进行的操作 ");Display() super("学生信息管理系统");setSize(500,400);setLocation(400,100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(null);b1.setLocation(50,150);b2.setLocation(200,150);b3.setLocation(350,150);b4.setLocation(25,250);b5.setLocation(250,250);a.setLocation(150,50);b1.setSize(100,50);b2.setSize(100,50);b3.setSize(100,50);b4.setSize(200,50);b5.setSize(200,50);a.setSize(500,50);add(b1);add(b2);add(b3);add(b4);add(b5);add(a);listen1(); void listen1() /监听 b1.addActionListener(new ActionListener()/输入信息 public void actionPerformed(ActionEvent e )Create a=new Create();a.setVisible(true););b2.addActionListener(new ActionListener() /修改信息 public void actionPerformed(ActionEvent e )Update b=new Update();b.setVisible(true););b3.addActionListener(new ActionListener() / 删除指定学生信息public void actionPerformed(ActionEvent e )Delete c=new Delete();c.setVisible(true););b4.addActionListener(new ActionListener() /按照性别查找public void actionPerformed(ActionEvent e )Sex d= new Sex();d.setVisible(true););b5.addActionListener(new ActionListener() /按照年龄查找public void actionPerformed(ActionEvent e ) Age f= new Age();f.setVisible(true);); class Create extends JFrame /输入信息JButton OK=new JButton ("确定");JLabel a1=new JLabel("Sno");JLabel a2=new JLabel("Sname");JLabel a3=new JLabel("Class");JLabel a4=new JLabel("Age");JLabel a5=new JLabel("Sex");JTextField t1=new JTextField(10);JTextField t2=new JTextField(10);JTextField t3=new JTextField(10);JTextField t4=new JTextField(10);JTextField t5=new JTextField(10);Create()super("学生信息管理系统");setSize(300,300);setLocation(400,400);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(null);a1.setLocation(0,20);a1.setSize(50,20);t1.setLocation(50,20);t1.setSize(200,20);a2.setLocation(0,40);a2.setSize(50,20);t2.setLocation(50,40);t2.setSize(200,20);a3.setLocation(0,60);a3.setSize(50,20);t3.setLocation(50,60);t3.setSize(200,20);a4.setLocation(0,80);a4.setSize(50,20);t4.setLocation(50,80);t4.setSize(200,20);a5.setLocation(0,100);a5.setSize(50,20);t5.setLocation(50,100);t5.setSize(200,20);OK.setSize(100,20);OK.setLocation(150,150);add(a1); add(t1);add(a2); add(t2);add(a3); add(t3);add(a4); add(t4);add(a5); add(t5);add(OK); listen2();void listen2()OK.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e )try Class.forName(JDriver); catch (java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL); Statement s=con.createStatement(); String CreateTable = "create table Student(Sno char(9), Sname char(10) , Class char(10), Age smallint, Sex char(2)" s.executeQuery(CreateTable);s.close();con.close();catch ( SQLException f) System.out.println("SQLException:"+f.getMessage();tryConnection con =DriverManager.getConnection(conURL);Statement s=con.createStatement(); String find = "INSERT INTO Student VALUES('" + t1.getText() + "','" + t2.getText() + "','" + t3.getText()+ "','"+t4.getText()+"','" + t5.getText() + "')"PreparedStatement ps=con.prepareStatement(find);ps.executeUpdate(); s.close();con.close();catch ( SQLException f) System.out.println("SQLException:"+f.getMessage();String ss="输入成功!" JOptionPane.showMessageDialog(null, ss););class Update extends JFrame /修改指定学号学生信息 JButton OK=new JButton ("确定");JLabel a1=new JLabel("要修改的学号:");JTextField t1=new JTextField(10);JLabel a2=new JLabel("修改后该学生姓名为:");JTextField t2=new JTextField(10);JLabel a3=new JLabel("修改后该学生班级为:");JTextField t3=new JTextField(10);JLabel a4=new JLabel("修改后该学生年龄为:");JTextField t4=new JTextField(10);JLabel a5=new JLabel("修改后该学生性别为:");JTextField t5=new JTextField(10);Update() super("学生信息管理系统");setSize(400,300);setLocation(400,400);setLayout(null);a1.setLocation(0,20);a1.setSize(130,20);t1.setLocation(130,20);t1.setSize(200,20);a2.setLocation(0,40);a2.setSize(130,20);t2.setLocation(130,40);t2.setSize(200,20);a3.setLocation(0,60);a3.setSize(130,20); t3.setLocation(130,60);t3.setSize(200,20);a4.setLocation(0,80);a4.setSize(130,20); t4.setLocation(130,80);t4.setSize(200,20); a5.setLocation(0,100);a5.setSize(130,20);t5.setLocation(130,100);t5.setSize(200,20);OK.setSize(100,20); OK.setLocation(200,150); add(a1); add(t1); add(a2); add(t2); add(a3); add(t3); add(a4); add(t4); add(a5); add(t5); add(OK); listen3();void listen3() OK.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e )try Class.forName(JDriver);catch (java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL); Statement s=con.createStatement(); String find = "UPDATE Student SET Sno = '" + t1.getText() + "',Sname = '" + t2.getText() + "',Class = '" + t3.getText() + "',Age = '" + t4.getText() + "',Sex = '" + t5.getText() + "' WHERE Student.Sno = '" + t1.getText() + "'"PreparedStatement ps=con.prepareStatement(find); ps.executeUpdate(); s.close();con.close(); catch ( SQLException f) System.out.println("SQLException:"+f.getMessage();String ss="修改完成!"JOptionPane.showMessageDialog(null, ss););class Delete extends JFrame /删除指定学号学生信息 JButton OK=new JButton ("确定");JLabel a=new JLabel("要删除的学生学号:");JTextField t=new JTextField(10);Delete() super("学生信息管理系统"); setSize(300,200);setLocation(400,400);setLayout(null);a.setSize(150,20);a.setLocation(0,50);t.setSize(150,20);t.setLocation(0,70);OK.setSize(100,20);OK.setLocation(150,100); add(a);add(t); add(OK);listen4();void listen4()OK.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e )try Class.forName(JDriver); catch(java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL);Statement s=con.createStatement();String find = "DELETE FROM Student WHERE Sno = '" + t.getText() + "'"PreparedStatement ps=con.prepareStatement(find);ps.executeUpdate(); s.close(); con.close(); catch ( SQLException f) System.out.println("SQLException:"+f.getMessage(); String ss="删除成功!" JOptionPane.showMessageDialog(null, ss););class Sex extends JFrame /按性别找所有相应的学生信息JLabel a=new JLabel("按性别查找:");JButton b1=new JButton ("查找男生的信息");JButton b2=new JButton ("查找女生的信息");Sex()super("学生信息管理系统");setSize(300,200);setLocation(400,400);setLayout(null);a.setSize(300,20);a.setLocation(0,20);b1.setSize(200,20);b1.setLocation(50,50);b2.setSize(200,20);b2.setLocation(50,100);add(a);add(b1);add(b2); listen51();listen52();void listen51()b1.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e )try Class.forName(JDriver); catch (java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL);Statement s=con.createStatement();String find = "SELECT * FROM Student WHERE Student.Sex = '男'" ResultSet rs=s.executeQuery(find);while(rs.next() System.out.println(rs.getString("Sno")+"t"+rs.getString("Sname")+"t"+rs.getString("Class")+"t"+rs.getInt("Age")+"t"+rs.getString("Sex"); s.close(); con.close(); catch ( SQLException f) System.out.println("SQLException:"+f.getMessage(); String ss="查找男生的信息成功!" JOptionPane.showMessageDialog(null, ss););void listen52()b2.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e )try Class.forName(JDriver); catch (java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL);Statement s=con.createStatement();String find = "SELECT * FROM Student WHERE Student.Sex = '女'"ResultSet rs=s.executeQuery(find);while(rs.next()System.out.println(rs.getString("Sno")+"t"+rs.getString("Sname")+"t"+rs.getString("Class")+"t"+rs.getInt("Age")+"t"+rs.getString("Sex"); s.close(); con.close(); catch ( SQLException f) System.out.println("SQLException:"+f.getMessage();String ss="查找女生的信息成功!"JOptionPane.showMessageDialog(null, ss););class Age extends JFrame /按年龄查找相应的学生信息JButton OK=new JButton ("确定");JLabel a=new JLabel("按年龄段查找: ");JTextField t1=new JTextField(3);JButton b2=new JButton ("至");JTextField t2=new JTextField(3);Age() super("学生信息管理系统"); setSize(300,150); setLocation(400,400);setLayout(null);a.setSize(300,20);a.setLocation(0,20);b2.setSize(50,20);b2.setLocation(100,50);t1.setSize(50,20);t1.setLocation(50,50);t2.setSize(50,20);t2.setLocation(150,50);OK.setSize(100,20);OK.setLocation(80,80);add(a);add(t1);add(b2);add(t2);add(OK);listen6();void listen6()OK.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e ) int n1=Integer.parseInt(t1.getText();int n2=Integer.parseInt(t2.getText();try Class.forName(JDriver); catch (java.lang.ClassNotFoundException f) System.out.println("forname:"+f.getMessage();try Connection con =DriverManager.getConnection(conURL);Statement s=con.createStatement();ResultSet rs=s.executeQuery("select * from Student where Age>="+n1+ "and Age<= "+n2);while(rs.next()System.out.println(rs.getString("Sno")+"t"+rs.getString("Sname")+"t"+rs.getString("Class")+"t"+rs.getInt("Age")+"t"+rs.getString("Sex"); s.close(); con.close(); catch ( SQLException f)System.out.println("SQLException:"+f.getMessage();String ss="按年龄查找成功!"JOptionPane.showMessageDialog(null, ss););/界面JButton button1 =new JButton("进入");JButton button2 =new JButton("退出");JLabel label1=new JLabel("欢迎进入学生信息管理系统 "); JPasswordField key=new JPasswordField(15);public z() /构造函super("学生信息管理系统");setSize(400,400);setLocation(400,100);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(null);button1.setLocation(50,200);button2.setLocation(200,200);button1.setSize(100,50);button2.setSize(100,50);label1.setLocation(50,50);label1.setSize(400,100);add(label1);add(button1);add(button2);button1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e ) Display dis=new Display();dis.setVisible(true););button2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e ) System.exit(0););public static void main(String args)z frame=new z();frame.setVisible(true);