标题:一个成绩管理系统,有问题 ,不知道怎么
只看楼主
蹴鞠者
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:134
注 册:2016-9-26
结帖率:75%
已结贴  问题点数:20 回复次数:4 
一个成绩管理系统,有问题 ,不知道怎么
程序代码:
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编辑过]

搜索更多相关主题的帖子: import sql String new this 
2017-10-09 15:32
kingpc520
Rank: 3Rank: 3
等 级:论坛游侠
威 望:7
帖 子:43
专家分:178
注 册:2017-9-16
得分:5 
JBCUtil是工具类 导包了可以Ctrl+鼠标左键进去看看
2017-10-09 17:07
蹴鞠者
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:134
注 册:2016-9-26
得分:0 
回复 2楼 kingpc520
我用的eclipse里面怎么没有,,Version: Neon.3 Release (4.6.3)
2017-10-09 17:09
calix
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:28
帖 子:249
专家分:1442
注 册:2015-5-4
得分:15 
Connection con = JBCUtil.getConnection();
JBCUtil,缺少的这个类应该是别人自己封装的工具类,就是获取下数据库连接
或者自己写一个,百度下jdbc连接数据库,不难的
2017-10-09 20:19
蹴鞠者
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:82
专家分:134
注 册:2016-9-26
得分:0 
回复 4楼 calix
解决了,谢谢
2017-10-10 11:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481215-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.163679 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved