weixin_53201607
2021-05-12 12:48
采纳率: 81.8%
浏览 50
已结题

java查询mysql数据库时rs.next()异常

想先在jpanel上输出数据库第一条的内容,再添加jbutton并给jbutton设置监听器,通过点击jbutton显示数据库下一条内容,
输出第一条内容正常,数据库中下一条数据不为空,但点击jbutton就会报错
代码如下 
public Mark(String s){
    this.setVisible(true);
    this.setResizable(true);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.setTitle("试卷批阅");
    this.setBounds(360,110,800, 600);

        dbconn = new QuestionBank();
        conn = dbconn.getConnection();
        String sql = "select * from "+s;
        PreparedStatement pstm = null;
        try {
            pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            rs.next();
            name = rs.getString("name");
            chooseGrade = rs.getString("chooseGrade");
            answer1 = rs.getString("answer1");
            answer2 = rs.getString("answer2");
           
        }
        catch (SQLException ex) {
            ex.printStackTrace();
        }



        temp = new JPanel();
        temp.setLayout(new GridLayout(2,10));
        exam = new JScrollPane(temp);
        this.add(exam);

        name1 = new JPanel();
        name1.setLayout(new GridLayout(6,1));
        temp.add(name1);
        name1.add(new JLabel("姓名 "+name));
        name1.add(new JLabel("选择题分数 "+chooseGrade));
        name1.add(new JLabel("主观题分数"));
        zggr = new JTextField();
        name1.add(zggr);
        next = new JButton("下一个");
        name1.add(next);
        temp.add(new JTextArea(answer1));
        temp.add(new JTextArea(answer2));
        
        next.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try {
                    showAnswer();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }});



}

public void showAnswer() throws SQLException {
        if(rs.next()){
        name = rs.getString("name");
        chooseGrade = rs.getString("chooseGrade");
        answer1 = rs.getString("answer1");
        answer2 = rs.getString("answer2");
        
        temp.removeAll();
        temp.repaint();
        temp.validate();

        name1 = new JPanel();
        name1.setLayout(new GridLayout(6, 1));
        temp.add(name1);
        name1.add(new JLabel("姓名 " + name));
        name1.add(new JLabel("选择题分数 " + chooseGrade));
        name1.add(new JLabel("主观题分数"));
        zggr = new JTextField();
        name1.add(zggr);
        next = new JButton("下一个");
        name1.add(next);
        next.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try {
                    showAnswer();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
        temp.add(new JTextArea(answer1));
        temp.add(new JTextArea(answer2));
        
       }

 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • 最佳回答

    String sql = "select * from "+s;

    打印sql出来看看,拿到数据库里面执行一下,看看SQL语句是否合法,查询失败了。

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)