Java高级程序设计 - 学生信息管理软件

reno408

贡献于2013-12-07

字数:13701 关键词: Java开发 Java

广东金融学院实验报告 课程名称:Java高级程序设计 实验编号 及实验名称 实验二 班级管理系统 系 别 计科系 姓 名 范海裕 学 号 111542446 班 级 1115424 实验地点 新电504 实验日期 2012-12-13 实验时数 6 指导老师 邹林达 同组其他成员 无 成 绩 一、实验目的 1、巩固java开发基础 2、学习Java数据库开发技术 二、实验环境 1.使用软件:Windows XP、jdk1.6 2.实验设备:微型计算机 三、实验内容及要求 建立班级管理系统,要求能实现数据的更新、插入、删除、管理等功能。 四、实验结果(附程序运行效果图) (通过图片、文字方式详细描述计算器功能及其操作过程) 第 页 共 20 页 第 页 共 20 页 程序列出了所有同学的姓名,旁边有每个同学的信息。有查询,插入,删除,更新的功能。列表上还做了排序功能。 五、实验总结(包括心得体会、问题回答及实验改进意见,可附页) 通过这次实验的制作过程,明白了写java程序是一个不可能很快就完成的任务,只有通过不断的修改,不断的想,不断的查阅,才能写出比较完美的程序。过程是很辛苦的,但是做好之后是很开心的。不能半途而废,需要有强大的毅力。 六、教师评语 1)完成所有规定的实验内容,实验步骤正确,结果正确; 2)完成绝大部分规定的实验内容,实验步骤正确,结果正确; 3)完成大部分规定的实验内容,实验步骤正确,结果正确; 4)基本完成规定的实验内容,实验步骤基本正确,所完成的结果基本正确; 5)未能很好的完成规定的实验内容或实验步骤不正确或结果不正确。 6)其他:___________________________________________ 评定等级:优秀 良好 中等 及格 不及格 教师签名: 第 页 共 20 页 年 月 日 附:程序源代码 import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Color; import java.awt.Rectangle; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.sql.*; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JComboBox; import java.awt.Dimension; public class Constants { public static String DRIVER="sun.jdbc.odbc.JdbcOdbcDriver"; public static String URL="jdbc:odbc:myscott"; public static String USERNAME=""; public static String PASSWORD=""; } public class Dbutil { public static Connection getConnection() throws SQLException{ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:myscott", "", ""); return conn; }catch(Exception e){ throw new SQLException("驱动错误或连接失败!"); } } public static void closeStatement(Statement stmt) throws SQLException{ try{ 第 页 共 20 页 if(stmt!=null) stmt.close(); }catch(SQLException e){ throw new SQLException("关闭Statement失败!"); } } public static void closeResultSet(ResultSet rs) throws SQLException{ try{ if(rs!=null) rs.close(); }catch(SQLException e){ throw new SQLException("关闭ResultSet失败!"); } } public static void closeConnection(Connection conn) throws SQLException{ try{ if(conn!=null) conn.close(); }catch(SQLException e){ throw new SQLException("关闭Connection失败!"); } } } public class shujuku extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JButton jButton = null; private JButton jButton1 = null; private JButton jButton2 = null; private JTextField jTextField1 = null; private JTextField jTextField2 = null; private JTextField jTextField3 = null; private JLabel jLabel = null; private JLabel jLabel1 = null; private JLabel jLabel2 = null; private JLabel jLabel3 = null; private JLabel jLabel4 = null; private JTextField jTextField4 = null; private JTextField jTextField5 = null; private JButton jButton3 = null; 第 页 共 20 页 private static JList jList = null; private JScrollPane jScrollPane = null; private JComboBox jComboBox = null; private JLabel jLabel5 = null; private JPanel jPanel = null; private JButton jButton4 = null; /** * This is the default constructor */ public shujuku() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(587, 303); this.setContentPane(getJContentPane()); this.setTitle("学生信息管理系统"); } public void diaoyong() { try { Connection conn=Dbutil.getConnection(); String selectSql11="SELECT name FROM student"; PreparedStatement pastmt=conn.prepareStatement(selectSql11); ResultSet rs=pastmt.executeQuery(); ArrayList list = new ArrayList(); while(rs.next()){ list.add(rs.getString(1)); } jList.setListData(list.toArray(new Object[0])); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); 第 页 共 20 页 } } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jLabel5 = new JLabel(); jLabel5.setBounds(new Rectangle(24, 2, 37, 29)); jLabel5.setText("排序"); jLabel4 = new JLabel(); jLabel4.setBounds(new Rectangle(360, 175, 45, 25)); jLabel4.setText("英语:"); jLabel3 = new JLabel(); jLabel3.setBounds(new Rectangle(360, 135, 45, 25)); jLabel3.setText("数学:"); jLabel2 = new JLabel(); jLabel2.setBounds(new Rectangle(360, 95, 45, 25)); jLabel2.setText("语文:"); jLabel1 = new JLabel(); jLabel1.setBounds(new Rectangle(360, 55, 45, 25)); jLabel1.setText("姓名:"); jLabel = new JLabel(); jLabel.setBounds(new Rectangle(360, 15, 45, 25)); jLabel.setText("学号:"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.setBackground(new Color(173, 232, 227)); jContentPane.add(getJButton(), null); jContentPane.add(getJButton1(), null); jContentPane.add(getJButton2(), null); jContentPane.add(getJButton3(), null); jContentPane.add(getJTextField1(), null); jContentPane.add(getJTextField2(), null); jContentPane.add(getJTextField3(), null); jContentPane.add(jLabel, null); jContentPane.add(jLabel1, null); jContentPane.add(jLabel2, null); 第 页 共 20 页 jContentPane.add(jLabel3, null); jContentPane.add(jLabel4, null); jContentPane.add(jLabel5, null); jContentPane.add(getJTextField4(), null); jContentPane.add(getJTextField5(), null); jContentPane.add(getJList(), null); jContentPane.add(getJScrollPane(), null); jContentPane.add(getJComboBox(), null); jContentPane.add(getJButton4(), null); } return jContentPane; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new Rectangle(240, 35, 74, 30)); jButton.setText("查询"); jButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try { Connection conn=Dbutil.getConnection(); String xuehao=jTextField1.getText(); String selectSql="SELECT * FROM student WHERE sno=?"; PreparedStatement astmt=conn.prepareStatement(selectSql); astmt.setString(1, xuehao); ResultSet rs=astmt.executeQuery(); while(rs.next()){ jTextField1.setText(rs.getString(1)); jTextField2.setText(rs.getString(2)); jTextField3.setText(rs.getString(3)); jTextField4.setText(rs.getString(4)); 第 页 共 20 页 jTextField5.setText(rs.getString(5)); } } catch (SQLException e1) { e1.printStackTrace(); } } }); } return jButton; } /** * This method initializes jButton1 * * @return javax.swing.JButton */ private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setBounds(new Rectangle(240, 95, 74, 30)); jButton1.setText("插入"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { JOptionPane.showMessageDialog(jButton1,"插入成功!", "提示",JOptionPane.INFORMATION_MESSAGE); try { Connection conn=Dbutil.getConnection(); String a=jTextField1.getText(); String b=jTextField2.getText(); String c=jTextField3.getText(); String d=jTextField4.getText(); String f=jTextField5.getText(); String insertSql="INSERT INTO student VALUES(?,?,?,?,?)"; PreparedStatement stmt=conn.prepareStatement(insertSql); stmt.setString(1,a); stmt.setString(2,b); 第 页 共 20 页 stmt.setString(3,c); stmt.setString(4,d); stmt.setString(5,f); stmt.executeUpdate(); Dbutil.closeConnection(conn); diaoyong(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } return jButton1; } private JButton getJButton2() { if (jButton2 == null) { jButton2 = new JButton(); jButton2.setBounds(new Rectangle(240, 155, 74, 30)); jButton2.setText("删除"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { int r=JOptionPane.showConfirmDialog(jButton2, "确定删除吗?", "删除", JOptionPane.YES_NO_OPTION); if(r==JOptionPane.YES_OPTION){ try { Connection conn=Dbutil.getConnection(); String number=jTextField1.getText(); String deleteSql="DELETE FROM student WHERE sno=?"; PreparedStatement pstmt=conn.prepareStatement(deleteSql); pstmt.setString(1, number); pstmt.executeUpdate(); Dbutil.closeConnection(conn); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); 第 页 共 20 页 } try { Connection conn=Dbutil.getConnection(); String bb=(String) jList.getSelectedValue(); String deleteSql1="DELETE FROM student WHERE name=?"; PreparedStatement pstmt=conn.prepareStatement(deleteSql1); pstmt.setString(1, bb); pstmt.executeUpdate(); Dbutil.closeConnection(conn); diaoyong(); jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField4.setText(""); jTextField5.setText(""); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); } return jButton2; } /** * This method initializes jTextField1 * * @return javax.swing.JTextField */ private JTextField getJTextField1() { if (jTextField1 == null) { jTextField1 = new JTextField(); jTextField1.setBounds(new Rectangle(410, 10, 110, 34)); } return jTextField1; 第 页 共 20 页 } /** * This method initializes jTextField2 * * @return javax.swing.JTextField */ private JTextField getJTextField2() { if (jTextField2 == null) { jTextField2 = new JTextField(); jTextField2.setBounds(new Rectangle(410, 50, 110, 34)); } return jTextField2; } /** * This method initializes jTextField3 * * @return javax.swing.JTextField */ private JTextField getJTextField3() { if (jTextField3 == null) { jTextField3 = new JTextField(); jTextField3.setBounds(new Rectangle(410, 90, 110, 34)); } return jTextField3; } /** * This method initializes jTextField4 * * @return javax.swing.JTextField */ private JTextField getJTextField4() { if (jTextField4 == null) { jTextField4 = new JTextField(); jTextField4.setBounds(new Rectangle(410, 130, 110, 34)); } return jTextField4; } /** * This method initializes jTextField5 * 第 页 共 20 页 * @return javax.swing.JTextField */ private JTextField getJTextField5() { if (jTextField5 == null) { jTextField5 = new JTextField(); jTextField5.setBounds(new Rectangle(410, 170, 110, 34)); } return jTextField5; } /** * This method initializes jButton3 * * @return javax.swing.JButton */ private JButton getJButton3() { if (jButton3 == null) { jButton3 = new JButton(); jButton3.setBounds(new Rectangle(431, 213, 74, 30)); jButton3.setText("清空"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField4.setText(""); jTextField5.setText(""); } }); } return jButton3; } /** * This method initializes jList * * @return javax.swing.JList */ private JList getJList() { if (jList == null) { jList = new JList(); 第 页 共 20 页 jList.setBounds(new Rectangle(12, 5, 178, 237)); try { Connection conn=Dbutil.getConnection(); String selectSql11="SELECT name FROM student order by sno"; PreparedStatement pastmt=conn.prepareStatement(selectSql11); ResultSet rs=pastmt.executeQuery(); ArrayList list = new ArrayList(); while(rs.next()){ list.add(rs.getString(1)); } jList.setListData(list.toArray(new Object[0])); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent e) { try { Connection conn=Dbutil.getConnection(); String aa=(String) jList.getSelectedValue(); String selectSql="SELECT * FROM student WHERE name=?"; PreparedStatement ppastmt=conn.prepareStatement(selectSql); ppastmt.setString(1, aa); ResultSet rs=ppastmt.executeQuery(); while(rs.next()){ jTextField1.setText(rs.getString(1)); jTextField2.setText(rs.getString(2)); jTextField3.setText(rs.getString(3)); jTextField4.setText(rs.getString(4)); jTextField5.setText(rs.getString(5)); } } catch (SQLException e1) { // TODO Auto-generated catch block 第 页 共 20 页 e1.printStackTrace(); } } }); } return jList; } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getJScrollPane() { if (jScrollPane == null) { jScrollPane = new JScrollPane(jList); jScrollPane.setBounds(new Rectangle(10, 37, 193, 217)); } return jScrollPane; } static class IL implements ItemListener{ public void itemStateChanged(ItemEvent e){ JComboBox cb=(JComboBox) e.getSource(); String aaa=(String)cb.getSelectedItem(); try { Connection conn=Dbutil.getConnection(); String selectSql11=""; if(aaa=="学号") selectSql11="SELECT name FROM student order by sno"; else if(aaa=="姓名") selectSql11="SELECT name FROM student order by name"; else if(aaa=="语文成绩") selectSql11="SELECT name FROM student order by chinese"; else if(aaa=="数学成绩") selectSql11="SELECT name FROM student order by math"; else if(aaa=="英语成绩") selectSql11="SELECT name FROM student order by english"; PreparedStatement pastmt=conn.prepareStatement(selectSql11); ResultSet rs=pastmt.executeQuery(); ArrayList list = new ArrayList(); while(rs.next()){ 第 页 共 20 页 list.add(rs.getString(1)); } jList.setListData(list.toArray(new Object[0])); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } /** * This method initializes jComboBox * * @return javax.swing.JComboBox */ private JComboBox getJComboBox() { if (jComboBox == null) { String[] choose={"学号","姓名","语文成绩","数学成绩","英语成绩"}; jComboBox = new JComboBox(choose); jComboBox.setBounds(new Rectangle(59, 1, 139, 29)); jComboBox.addItemListener(new IL()); } return jComboBox; } /** * This method initializes jButton4 * * @return javax.swing.JButton */ private JButton getJButton4() { if (jButton4 == null) { jButton4 = new JButton(); jButton4.setBounds(new Rectangle(240, 215, 74, 30)); jButton4.setText("更新"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try { Connection conn=Dbutil.getConnection(); 第 页 共 20 页 String number=jTextField1.getText(); String deleteSql="DELETE FROM student WHERE sno=?"; PreparedStatement pstmt=conn.prepareStatement(deleteSql); pstmt.setString(1, number); pstmt.executeUpdate(); Dbutil.closeConnection(conn); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Connection conn=Dbutil.getConnection(); String bb=(String) jList.getSelectedValue(); String deleteSql1="DELETE FROM student WHERE name=?"; PreparedStatement pstmt=conn.prepareStatement(deleteSql1); pstmt.setString(1, bb); pstmt.executeUpdate(); Dbutil.closeConnection(conn); diaoyong(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Connection conn=Dbutil.getConnection(); String a=jTextField1.getText(); String b=jTextField2.getText(); String c=jTextField3.getText(); String d=jTextField4.getText(); String f=jTextField5.getText(); String insertSql="INSERT INTO student VALUES(?,?,?,?,?)"; PreparedStatement stmt=conn.prepareStatement(insertSql); stmt.setString(1,a); stmt.setString(2,b); stmt.setString(3,c); stmt.setString(4,d); 第 页 共 20 页 stmt.setString(5,f); stmt.executeUpdate(); Dbutil.closeConnection(conn); diaoyong(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } return jButton4; } /** * This method initializes jPanel * * @return javax.swing.JPanel */ } // @jve:decl-index=0:visual-constraint="9,-10" 第 页 共 20 页 第 页 共 20 页 第 页 共 20 页

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ]
1 人已下载

下载文档

相关文档