1、第 1 页 共 24 页天津理工大学实验报告学院(系)名称:计算机与通信工程学院姓名 晴小天 学号 201356xxxx 专业 计算机科学与技术班级 2013 级 2 班 实验项目 实验五:小型信息管理系统设计开发课程名称 Java 语言程序设计 课程代码 0667056实验时间 2015 年 11 月 12 日、20 日、25 日第 3、4 节 实验地点 7-219批改意见 成绩教师签字: 实验内容:1. 参考实验资料,自拟题目,设计制作一个小型信息管理系统。实验目的:1理解并掌握 Java 数据库访问技术。2掌握对数据进行增加、删除、修改、查询。实验要求:1按照要求编制程序;3为增加程序可
2、读性,请在程序中对类成员进行适当注释说明;4整理上机步骤,总结经验和体会;5认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】一、 MIS 系统第 2 页 共 24 页系统名称:学生信息管理系统操作系统: Window8.1Java 环境; jdk 1.7.0_21 、jre 1.7.0_21集成开发环境:MyEclipse Professional 2013数据库软件版本:SQL Server 2010 连接驱动:jdbc-odbc二、 系统主要功能本项目主要实现了 java 界面的登录功能,以及登陆后的增删改查功能,主要是对 student 表实现增加一
3、条学生信息、删除一条学生信息记录、更新学生信息表,以及查询学生信息表的数据三、数据库结构1. 数据库名称:SchoolDB2. 数据表:student 表具体字段信息如下表:字段名 类型 长度 小数点 不是 nullsname(主键) varchar 20 0 sname varchar 20 0 sdept varchar 20 0 sgrade varchar 20 0 saddress varchar 20 0 sname, sname, sdept, sgrade、saddress 分别保存学生的姓名、专业、学院、成绩、家庭住址等学生基本信息,其中 sname 为主键且不为空。四、关键
4、功能界面截图1. 登陆界面用户输入数据库中存在的用户名和密码后,便可登陆成功,否则提示密码错误 正确的用户名,错误的密码登陆模块添加 删除 更新 查找学生信息管理系统功能模块第 3 页 共 24 页正确的用户名正确的密码后进入学生信息管理系统:2. 添加功能:输入界面中的所有数据之后,点击添加,即可向数据库中加入一条学生记录第 4 页 共 24 页3.删除功能:输入姓名,即可把数据库中对应姓名的一条学生信息删除4.更新功能:输入要修改学生的信息,即可修改该学生在数据库中的记录第 5 页 共 24 页5.查找功能:点击查找可以查询数据库中的所有学生信息,每页 9 条记录,其余的学生信息的点击下一
5、页,可以查看,还可以点击上一页,来切换数据点击下一页:第 6 页 共 24 页五、关键代码分析DBConnetion 用于连接数据库:package edu;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBConnection public static Connection getConnection()Connection conn = null;String driver = “sun.jdbc.odbc.JdbcOdbcDriver“
6、;String url = “jdbc:odbc:schoolData“;String username = “sa“;String password = “123456“;try Class.forName(driver);第 7 页 共 24 页conn = DriverManager.getConnection(url,username,password); catch (ClassNotFoundException e) e.printStackTrace(); catch (SQLException e) e.printStackTrace();return conn;登陆的窗体类
7、LoginFrame、JFrame:package edu;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Connection;import java.sql.SQLException;import javax.swing.JButton;import javax.swing.JLabel;import jav
8、ax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.SwingUtilities;public class Jswing implements ActionListenerprivate LoginFrame frame;private JLabel lname;private JLabel lpassword;private JTextField tname;private JPassw
9、ordField tpassword;private JButton byes;private JButton bno;private JPanel p1;private JPanel p2;private JPanel p3;private String userName,password;/* 初始化窗口数据第 8 页 共 24 页*/public void initData() frame=new LoginFrame(“登陆“);frame.setResizable(false);frame.setLayout(new GridLayout(3, 2);Container c=fram
10、e.getContentPane();lname=new JLabel(“用户名:“);lpassword=new JLabel(“密 码: “);tname=new JTextField(8);tpassword=new JPasswordField(8);byes=new JButton(“确定“);bno=new JButton(“取消“);p1=new JPanel();p2=new JPanel();p3=new JPanel();p1.add(lname);p1.add(tname);c.add(p1);p2.add(lpassword);p2.add(tpassword);c.a
11、dd(p2);p3.add(byes);p3.add(bno);c.add(p3);frame.setVisible(true);byes.addActionListener(this);bno.addActionListener(this);userName=null;password=null;Overridepublic void actionPerformed(ActionEvent e) if(e.getSource()=bno)System.exit(1);elseif(e.getSource()=byes)userName=tname.getText().trim();passw
12、ord=new String(tpassword.getPassword();boolean result=false;result=SchoolOperate.login(userName, password);if(result)frame.setVisible(false);SwingUtilities.invokeLater(new Runnable() public void run() 第 9 页 共 24 页SchoolInfoGui application = new SchoolInfoGui();application.getJFrame().setVisible(true
13、););elseString error=null;if(result=false)error=“用户名或密码错误,请重试!“;JOptionPane.showMessageDialog(null, error);public static void main(String args) new Jswing().initData();package edu;import java.awt.HeadlessException;import javax.swing.JFrame;public class LoginFrame extends JFramepublic LoginFrame(Stri
14、ng title) throws HeadlessException super();setTitle(title);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(540,200, 280, 200);学生信息管理系统主界面的 SchoolInfoGui:package edu;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.List;第 10 页
15、共 24 页import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SwingUtilities;public c
16、lass SchoolInfoGui implements ActionListener private JFrame jFrame;private JPanel jContentPane;private JLabel lblName = null;private JTextField tfName = null;private JLabel lblRegion = null;private JComboBox cbRegion = null;private JLabel lblArea = null;private JTextField tfArea = null;private JLabe
17、l lblPopu = null;private JTextField tfPopu = null;private JLabel lblGdp = null;private JTextField tfGDP = null;private JButton btnAdd = null;private JButton btnDel = null;private JButton btnModify = null;private JButton btnQuery = null;private JButton btnUpPage = null;private JButton btnDownPage = n
18、ull;private JTable table = null;TableModel tm = null;JScrollPane jsp = null;private JTextField getTfName() if (tfName = null) tfName = new JTextField();tfName.setBounds(new Rectangle(89, 17, 173, 27);return tfName;private JComboBox getCbRegion() if (cbRegion = null) 第 11 页 共 24 页String str = “请选择“,
19、“计算机科学与技术“, “信息安全“, “网络工程“, “信息与计算科学“, “物流工程“, “软件工程“;cbRegion = new JComboBox(str);cbRegion.setBounds(new Rectangle(360, 17, 229, 28);return cbRegion;private JTextField getTfArea() if (tfArea = null) tfArea = new JTextField();tfArea.setBounds(new Rectangle(89, 61, 173, 27);return tfArea;private JTe
20、xtField getTfPopu() if (tfPopu = null) tfPopu = new JTextField();tfPopu.setBounds(new Rectangle(360, 61, 229, 28);return tfPopu;private JTextField getTfGDP() if (tfGDP = null) tfGDP = new JTextField();tfGDP.setBounds(new Rectangle(89, 105, 173, 27);return tfGDP;private JButton getBtnAdd() if (btnAdd
21、 = null) btnAdd = new JButton();btnAdd.setBounds(new Rectangle(46, 152, 71, 30);btnAdd.addActionListener(this);btnAdd.setText(“添加“);return btnAdd;private JButton getBtnUpPage() if (btnUpPage = null) btnUpPage = new JButton(“上一页“);第 12 页 共 24 页btnUpPage.addActionListener(this);btnUpPage.setBounds(200
22、, 390, 80, 30);return btnUpPage;private JButton getBtnDownPage() if (btnDownPage = null) btnDownPage = new JButton(“下一页“);btnDownPage.addActionListener(this);btnDownPage.setBounds(300, 390, 80, 30);return btnDownPage;private JButton getBtnDel() if (btnDel = null) btnDel = new JButton();btnDel.setBou
23、nds(new Rectangle(189, 152, 71, 30);btnDel.addActionListener(this);btnDel.setText(“删除“);return btnDel;private JButton getBtnModify() if (btnModify = null) btnModify = new JButton();btnModify.setBounds(new Rectangle(342, 152, 71, 30);btnModify.addActionListener(this);btnModify.setText(“更新“);return bt
24、nModify;private JButton getBtnQuery() if (btnQuery = null) btnQuery = new JButton();btnQuery.setBounds(new Rectangle(484, 152, 71, 30);btnQuery.addActionListener(this);btnQuery.setText(“查找“);return btnQuery;第 13 页 共 24 页JFrame getJFrame() if (jFrame = null) jFrame = new JFrame();jFrame.setDefaultClo
25、seOperation(JFrame.EXIT_ON_CLOSE);jFrame.setBounds(new Rectangle(450, 250, 610, 454);jFrame.setContentPane(getJContentPane();jFrame.setTitle(“学生信息管理系统“);return jFrame;private JPanel getJContentPane() if (jContentPane = null) lblGdp = new JLabel();lblGdp.setBounds(new Rectangle(15, 106, 63, 27);lblGd
26、p.setText(“家庭住址“);lblPopu = new JLabel();lblPopu.setBounds(new Rectangle(285, 61, 63, 27);lblPopu.setText(“成绩“);lblArea = new JLabel();lblArea.setBounds(new Rectangle(15, 61, 63, 27);lblArea.setText(“学院“);lblRegion = new JLabel();lblRegion.setBounds(new Rectangle(285, 17, 63, 27);lblRegion.setText(“
27、专业“);lblName = new JLabel();lblName.setBounds(new Rectangle(15, 17, 63, 27);lblName.setText(“姓名“);jContentPane = new JPanel();jContentPane.setLayout(null);jContentPane.add(lblName, null);jContentPane.add(getTfName(), null);jContentPane.add(lblRegion, null);jContentPane.add(getCbRegion(), null);jCont
28、entPane.add(lblArea, null);jContentPane.add(getTfArea(), null);jContentPane.add(lblPopu, null);jContentPane.add(getTfPopu(), null);jContentPane.add(lblGdp, null);jContentPane.add(getTfGDP(), null);jContentPane.add(getBtnAdd(), null);jContentPane.add(getBtnDel(), null);jContentPane.add(getBtnModify()
29、, null);jContentPane.add(getBtnQuery(), null);jContentPane.add(getBtnUpPage(), null);第 14 页 共 24 页jContentPane.add(getBtnDownPage(), null);return jContentPane;public void actionPerformed(ActionEvent e) if (e.getActionCommand().equals(“查找“) selectAll(); else if (e.getActionCommand().equals(“下一页“) sel
30、ectDownPage(); else if (e.getActionCommand().equals(“上一页“) selectUpPage(); else if (e.getActionCommand().equals(“添加“) if (tfName.getText().equals(“)| cbRegion.getSelectedItem().equals(“请选择“)| tfArea.getText().equals(“)| tfPopu.getText().equals(“)| tfGDP.getText().equals(“) JOptionPane.showMessageDia
31、log(jFrame, “请填写相关信息“);else Student co = new Student();co.setSname(tfName.getText();co.setSpro(String) cbRegion.getSelectedItem();co.setSdept(tfArea.getText();co.setSgrade(tfPopu.getText();co.setSaddress(tfGDP.getText();boolean flag = SchoolOperate.add(co);if (flag) JOptionPane.showMessageDialog(jFr
32、ame, “添加成功“); else JOptionPane.showMessageDialog(jFrame, “添加失败“); else if (e.getActionCommand().equals(“删除“) if(!tfName.getText().equals(“)String key = tfName.getText();boolean flag = SchoolOperate.del(key);if (flag) JOptionPane.showMessageDialog(jFrame, “删除成功“); else JOptionPane.showMessageDialog(j
33、Frame, “删除失败“);else JOptionPane.showMessageDialog(jFrame, “请填写一个国家名字“);第 15 页 共 24 页 else if (e.getActionCommand().equals(“更新“) if (!tfName.getText().equals(“)cou.setSname(tfName.getText();cou.setSpro(String)cbRegion.getSelectedItem();cou.setSdept(tfPopu.getText();cou.setSgrade(tfArea.getText();cou.
34、setSaddress(tfGDP.getText();boolean flag = SchoolOperate.modify(cou);if(flag)JOptionPane.showMessageDialog(jFrame, “更新成功“);else JOptionPane.showMessageDialog(jFrame, “更新失败“); else JOptionPane.showMessageDialog(jFrame, “请填写相应的信息“);public void setValues(Object obj) this.tm.setDatavalues(obj);public vo
35、id selectAll() String spro=cbRegion.getSelectedItem().toString();List list = SchoolOperate.queryFirst();Object datavalues = new Objectlist.size()5;for (int i = 0; i list = SchoolOperate.downPage();if (list != null) Object datavalues = new Objectlist.size()5;for (int i = 0; i list = SchoolOperate.upP
36、age();if (list != null) Object datavalues = new Objectlist.size()5;for (int i = 0; i queryFirst() String sql = “select top 9 * from student order by sname“;List list = query(sql);return list;public static List downPage() int y = queryCount();List list = null;int count = 0; / 总页数if (y % 5 = 0) count
37、= y / 5; else count = y / 5 + 1;if (i = count) list = null;return list;public static boolean login(String userName,String password)boolean result=false;第 18 页 共 24 页Connection con = DBConnection.getConnection();String sql=“select passWord from login where userName=?“;PreparedStatement ps = null;Resu
38、ltSet rs = null;tryps=con.prepareStatement(sql);ps.setObject(1, userName);rs=ps.executeQuery();while(rs.next()if(password.equals(rs.getString(“passWord“).trim()result=true;catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();finallytryif(rs!=null)rs.close();catch(SQLException
39、e)e.printStackTrace();try if(ps!=null)ps.close(); catch (SQLException e) e.printStackTrace();tryif(con!=null)con.close();catch(SQLException e)e.printStackTrace();return result;public static int queryCount() int count = 0;Connection conn = DBConnection.getConnection();第 19 页 共 24 页String sql = “selec
40、t count(*) from student“;Statement stmt = null;ResultSet rs = null;try stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next() count = rs.getInt(1); catch (SQLException e) e.printStackTrace(); finally try if (rs != null)rs.close();if (stmt != null)stmt.close();if (conn != null)con
41、n.close(); catch (SQLException e) e.printStackTrace();return count;public static List query(String sql) Connection conn = DBConnection.getConnection();List list = new ArrayList();Statement stmt = null;ResultSet rs = null;try stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next()
42、Student cou = new Student();cou.setSname(rs.getString(1);cou.setSpro(rs.getString(2);cou.setSdept(rs.getString(3);cou.setSgrade(rs.getString(4);cou.setSaddress(rs.getString(5);list.add(cou); catch (SQLException e) 第 20 页 共 24 页e.printStackTrace(); finally try if (rs != null)rs.close();if (stmt != nu
43、ll)stmt.close();if (conn != null)conn.close(); catch (SQLException e) e.printStackTrace();return list;public static List upPage() List list = null;if (i 2) x = (-i) - 1) * 5;String sql = “select top “+ x+ “ * from (select * from student where sname not in(select top “+ x + “ sname from student order
44、 by sname) as A “;list = query(sql); else if (i = 2) list = queryFirst();i-; else if (i 2) list = null;return list;public static boolean add(Student cou) Connection conn = DBConnection.getConnection();PreparedStatement pstmt = null;String sql = “insert into student values(?,?,?,?,?)“;int x = 0;try p
45、stmt = conn.prepareStatement(sql);pstmt.setString(1, cou.getSname();pstmt.setString(2, cou.getSpro();pstmt.setString(3, cou.getSdept();第 21 页 共 24 页pstmt.setString(4, cou.getSgrade();pstmt.setString(5, cou.getSaddress();x = pstmt.executeUpdate(); catch (SQLException e) e.printStackTrace(); finally t
46、ry if (pstmt != null)pstmt.close();if (conn != null) conn.close(); catch (SQLException e) e.printStackTrace();if (x = 1) return true; else return false;public static boolean del(String key) Connection conn = DBConnection.getConnection();PreparedStatement stmt = null;String sql = “delete from student
47、 where sname =?“;int x = 0;try stmt = conn.prepareStatement(sql);stmt.setString(1, key);x = stmt.executeUpdate(); catch (SQLException e) e.printStackTrace(); finally try if (stmt != null)stmt.close();if (conn != null)conn.close(); catch (SQLException e) e.printStackTrace();if (x = 1) 第 22 页 共 24 页re
48、turn true; else return false;public static boolean modify(Student cou) Connection conn = DBConnection.getConnection();PreparedStatement pstmt = null;String sql = “update student set spro=?,sdept=?,sgrade=?,saddress=? where sname =?“;int x = 0;try pstmt = conn.prepareStatement(sql);pstmt.setString(1, cou.getSpro();pstmt.setString(2, cou.getSdept();pstmt.setString(3, cou.getSaddress();pstmt.setString(4, cou.getSgrade();pstmt.setString(5, cou.getSname();x = pstmt.executeUpdate(); catch (SQLExceptio