一个成绩管理系统,有问题 ,不知道怎么
程序代码:
package 成绩管理系统; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class AddScore extends JPanel implements ActionListener { String[] columnNames = new String[] { "姓名", "学期", "课程ID", "成绩" }; DefaultTableModel dtmScore = new DefaultTableModel(columnNames, 57); JTable jtAddScore = new JTable(dtmScore); JScrollPane jspScore = new JScrollPane(jtAddScore); JComboBox jcbCourse = new JComboBox(); JComboBox jcbClassName = new JComboBox(); JComboBox jcbTerm = new JComboBox(); Connection con = JBCUtil.getConnection(); PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData meta = null; String sql = null; int cols = 0; int rowCount = 0; public AddScore() { CreateAddScoreGUI(); } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if ("查询".equals(str)) { // 当点击查询时,用searchScore()方法对其进行处理 searchScore(); } else if ("提交".equals(str)) { // 当点击提交时,用tryCommit()方法对其进行处理 tryCommit(); } else if ("退出".equals(str)) { this.setVisible(false); } } public void tryCommit() { if (cols < 1) return; sql = "insert into scorexx values(?,?,?,?)"; try { ps = con.prepareStatement(sql); String[] value = new String[4]; for (int k = 0; k < rowCount; k++) { for (int i = 1; i <= cols + 1; i++) { // 如果成绩为空,取消提交操作,并给出提示。 if (jtAddScore.getValueAt(k, i - 1) != null) { value[i - 1] = jtAddScore.getValueAt(k, i - 1).toString(); } else { if (i == 4) JOptionPane.showMessageDialog(null, "无效成绩输入!成绩不能为空"); return; } // 如果成绩字段里面不是数字,则取消本次操作?? // String str = "^[0-9]{1}[0-9]?[0-9]?$"; // if (i == 4) { // if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){ // if( // Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100) // {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100"); // return;} // } // } System.out.print(value[i - 1]); } ps.setString(1, value[0]); ps.setString(2, value[1]); ps.setString(3, value[2]); ps.setInt(4, Integer.parseInt(value[3])); ps.executeUpdate(); (); System.out.println(); } JOptionPane.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!"); } catch (Exception ew) { ew.printStackTrace(); } } // 自动添加课程下拉列表中的课程 public void addCourseToJCheckBoxCourse() { String courseName = null; sql = "select cname from coursexx"; try { ps = con.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData meta = rs.getMetaData(); int cols = meta.getColumnCount(); while (rs.next()) { for (int i = 1; i <= cols; i++) { courseName = rs.getString(i); System.out.println(courseName); jcbCourse.addItem(courseName); rowCount++; } } (); } catch (Exception ers) { ers.printStackTrace(); try { con.rollback(); } catch (Exception er) { er.printStackTrace(); } } } public void searchScore() { // 每次查询之前清空之前的结果 for (int k = 0; k < 50; k++) { for (int i = 0; i < cols; i++) { jtAddScore.setValueAt(null, k, i); } } // Method1:JDBC Method2:Hibernate String cName = jcbCourse.getSelectedItem().toString().trim(); sql = "select x.sname,k.term, from " + "choicesxx x join Coursexx k " + "on and try { ps = con.prepareStatement(sql); ps.setString(1, cName); rs = ps.executeQuery(); meta = rs.getMetaData(); cols = meta.getColumnCount(); boolean flag = true; String result = null; int k = 1; while (rs.next()) { for (int i = 1; i <= cols; i++) { result = rs.getString(i); jtAddScore.setValueAt(result, k - 1, i - 1); } rowCount = k++; } } catch (Exception exx) { exx.printStackTrace(); } } // 产生用户界面 public void CreateAddScoreGUI() { this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:"); jcbCourse.addActionListener(this); jcbCourse.addItem(new String("请选择课程")); addCourseToJCheckBoxCourse(); this.add(jlCourse); this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:"); // jcbClassName.addActionListener(this); // jcbClassName.addItem(new String("请选择班级")); // addClassNameToJCheckBoxClassName(); // this.add(jlClassName); // this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:"); // jcbTerm.addActionListener(this); // jcbTerm.addItem(new String("请选择学期")); // addTermToJCheckBoxTerm(); // this.add(jlTerm); // this.add(jcbTerm); JButton jbSearch = new JButton("查询"); jbSearch.addActionListener(this); JButton jbOK = new JButton("提交"); jbOK.addActionListener(this); JButton jbExit = new JButton("退出"); jbExit.addActionListener(this); this.add(jbSearch); this.add(jbOK); this.add(jbExit); this.add(jspScore); this.setVisible(true); this.setSize(500, 600); } public static void main(String[] args) { new AddScore(); } }
在网上找的,用来学习一下,空出来的那一行JBCUtil显示不存在,不知道怎么搞,有大神来帮忙看看吗
[此贴子已经被作者于2017-10-9 15:35编辑过]