当前位置: 首页 > 知识库问答 >
问题:

:对空结果集执行非法操作

屈星腾
2023-03-14

我做了一个简单的科学测验java应用程序,有4个框架:登录、菜单、测试和结果。

前三个帧工作正常,但当我单击显示结果时,它显示错误。这是我在Test JFrame中的代码。如果您愿意,我也可以发送其他帧的编码

这是我的错误

java.sql.SQLException: Illegal operation on empty result set.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
        at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
        at com.mysql.jdbc.ResultSetImpl.getFloat(ResultSetImpl.java:2499)
        at Test.ResultBTNActionPerformed(Test.java:292)
        at Test.access$500(Test.java:18)
        at Test$6.actionPerformed(Test.java:215)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6216)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

测试JFrame编码

import java.sql.*;
import javax.swing.JOptionPane;
public class Test extends javax.swing.JFrame {
    String ID;
    String Subject;
    int index =1;
    int max =0;
    int result = 0;
    char [] answers;
    /** Creates new form Test */
    public Test()
    {
        initComponents();
    }
    public Test(String subject, String id)
    {
        initComponents();
        ID = id;
        Subject = subject;
        PrevBTN.setVisible(false);
        try
          {


            Class.forName("java.sql.Driver");
            String database = "jdbc:mysql://localhost:3306/quizdb";
            Connection conn = DriverManager.getConnection(database, "root", "1234");
            Statement stmt = conn.createStatement();

            String sql = "select max(SNo) from " + Subject;
            ResultSet rs = stmt.executeQuery(sql);
            rs.next();
            max = rs.getInt(1);
            answers = new char[max];
            for(int i=0; i<max;i++)
                answers[i] = 'e';
            rs.close();
            stmt.close();
            conn.close();
            getQues();
             }
             catch(Exception e){ e.printStackTrace();
          }
    }
    private void getQues()
    {
        try
        {

            Class.forName("java.sql.Driver");
            String database = "jdbc:mysql://localhost:3306/quizdb";
            Connection conn = DriverManager.getConnection(database, "root", "1234");
            Statement stmt = conn.createStatement();

            String sql = "select * from " + Subject + " where SNo = " + index;
            ResultSet rs = stmt.executeQuery(sql);
            rs.next();
            QuesTA.setText("\nQ" + index + ".  " + rs.getString(2));
            a.setText(rs.getString("a"));
            b.setText(rs.getString("b"));
            c.setText(rs.getString("c"));
            d.setText(rs.getString("d"));
            a.setSelected(answers[index-1] == 'a');
            b.setSelected(answers[index-1] == 'b');
            c.setSelected(answers[index-1] == 'c');
            d.setSelected(answers[index-1] == 'd');
            e.setSelected(answers[index-1] == 'e');
            rs.close();
            stmt.close();
            conn.close();
        }
         catch(Exception e){ e.printStackTrace(); }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        a = new javax.swing.JRadioButton();
        b = new javax.swing.JRadioButton();
        c = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        QuesTA = new javax.swing.JTextArea();
        PrevBTN = new javax.swing.JButton();
        NextBTN = new javax.swing.JButton();
        ResultBTN = new javax.swing.JButton();
        d = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        e = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quiz Time");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Castellar", 1, 36));
        jLabel2.setForeground(new java.awt.Color(255, 0, 51));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("SCIENCE QUIZ");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(111, 20, 360, -1));

        buttonGroup1.add(a);
        a.setFont(new java.awt.Font("Tahoma", 1, 14));
        a.setForeground(new java.awt.Color(102, 0, 102));
        a.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        a.setOpaque(false);
        a.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aActionPerformed(evt);
            }
        });
        getContentPane().add(a, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 210, -1, -1));

        buttonGroup1.add(b);
        b.setFont(new java.awt.Font("Tahoma", 1, 14));
        b.setForeground(new java.awt.Color(102, 0, 102));
        b.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        b.setOpaque(false);
        b.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bActionPerformed(evt);
            }
        });
        getContentPane().add(b, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 240, -1, -1));

        buttonGroup1.add(c);
        c.setFont(new java.awt.Font("Tahoma", 1, 14));
        c.setForeground(new java.awt.Color(102, 0, 102));
        c.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        c.setOpaque(false);
        c.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cActionPerformed(evt);
            }
        });
        getContentPane().add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 270, -1, -1));

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setAlignmentX(5.0F);
        jScrollPane1.setAlignmentY(5.0F);

        QuesTA.setBackground(new java.awt.Color(248, 241, 244));
        QuesTA.setColumns(20);
        QuesTA.setEditable(false);
        QuesTA.setFont(new java.awt.Font("Tahoma", 0, 18));
        QuesTA.setForeground(new java.awt.Color(0, 153, 153));
        QuesTA.setLineWrap(true);
        QuesTA.setRows(3);
        QuesTA.setWrapStyleWord(true);
        QuesTA.setAutoscrolls(false);
        QuesTA.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(204, 0, 204), new java.awt.Color(204, 0, 204), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
        QuesTA.setSelectionColor(new java.awt.Color(255, 255, 51));
        jScrollPane1.setViewportView(QuesTA);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, 460, 100));

        PrevBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        PrevBTN.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        PrevBTN.setForeground(new java.awt.Color(0, 102, 102));
        PrevBTN.setText("<< Prev");
        PrevBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
        PrevBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PrevBTNActionPerformed(evt);
            }
        });
        getContentPane().add(PrevBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 370, 90, 40));

        NextBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        NextBTN.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        NextBTN.setForeground(new java.awt.Color(0, 102, 153));
        NextBTN.setText("Next >>");
        NextBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
        NextBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextBTNActionPerformed(evt);
            }
        });
        getContentPane().add(NextBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 370, 90, 40));

        ResultBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        ResultBTN.setFont(new java.awt.Font("Castellar", 1, 16)); // NOI18N
        ResultBTN.setForeground(new java.awt.Color(0, 102, 102));
        ResultBTN.setText("GEt RESULT");
        ResultBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
        ResultBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResultBTNActionPerformed(evt);
            }
        });
        getContentPane().add(ResultBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 370, 140, 40));

        buttonGroup1.add(d);
        d.setFont(new java.awt.Font("Tahoma", 1, 14));
        d.setForeground(new java.awt.Color(102, 0, 102));
        d.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        d.setOpaque(false);
        d.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dActionPerformed(evt);
            }
        });
        getContentPane().add(d, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 300, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setForeground(new java.awt.Color(102, 0, 102));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setOpaque(true);
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 430));

        buttonGroup1.add(e);
        getContentPane().add(e, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 450, 10, 10));

        pack();
    }// </editor-fold>                        

    private void PrevBTNActionPerformed(java.awt.event.ActionEvent evt) {                                        
            index--;
            getQues();
            if(index == 1)
                PrevBTN.setVisible(false);
            if(index < max)
                NextBTN.setVisible(true);
}                                       

    private void NextBTNActionPerformed(java.awt.event.ActionEvent evt) {                                        
            index++;
            getQues();
            if(index == max)
                NextBTN.setVisible(false);
            if(index > 1)
                PrevBTN.setVisible(true);
}                                       

    private void bActionPerformed(java.awt.event.ActionEvent evt) {                                  
        answers[index-1] = 'b';
}                                 

    private void ResultBTNActionPerformed(java.awt.event.ActionEvent evt) {                                          
        try
        {

              Class.forName("java.sql.Driver");
            String database = "jdbc:mysql://localhost:3306/quizdb";
            Connection conn = DriverManager.getConnection(database, "root", "1234");
            Statement stmt = conn.createStatement();

            String sql = "select Result from " + Subject;
            ResultSet rs = stmt.executeQuery(sql);
            int i = 0;
            while(rs.next())
            {
                char ans = rs.getString(1).charAt(0);
                if(ans == answers[i])
                    result++;
                i++;
            }
            float res = ((float)result * 100 ) / max;
            sql = "Select " + Subject + "Result from Result where ID = '" + ID + "'";
            rs = stmt.executeQuery(sql);
            rs.next();
            if(res > rs.getFloat(1))
            {
                sql = "update Result set " + Subject + "Result = " + res + " where ID = '" + ID + "'";
                stmt.executeUpdate(sql);
            }
            stmt.close();
            conn.close();
            new Result(res,ID).setVisible(true);
            this.setVisible(false);
        }
         catch(Exception e)
         {  e.printStackTrace();
                               }
}                                         

    private void aActionPerformed(java.awt.event.ActionEvent evt) {                                  
        answers[index-1] = 'a';
    }                                 

    private void cActionPerformed(java.awt.event.ActionEvent evt) {                                  
        answers[index-1] = 'c';
    }                                 

    private void dActionPerformed(java.awt.event.ActionEvent evt) {                                  
        answers[index-1] = 'd';
    }                                 

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
    }

共有3个答案

丁俊爽
2023-03-14

正如在其他答案中所说,只有当rs.next()返回true时,才能调用列getter。

在您的情况下:

int max = rs.next() ? rs.getInt(1) : 0;


float x = rs.next() ? rs.getFloat(1) : 0f;

也许SQL查询也不是最合适的。

在堆栈跟踪中,您会看到对getFloat的调用引发了错误。

包谭三
2023-03-14

您需要检查结果而不是原始的rs.next()(如果没有更多的行,它会返回false)......类似于,

if (rs.next()) {
    max = rs.getInt(1);
}
曹鸿风
2023-03-14

之前未检查您的结果集是否为空。尝试使用如下循环:

  While(rs.next()){
   max = rs.getInt(1);
      ...
 类似资料:
  • 我有这样的代码 我试图与kategorikode_kategori,但出现错误,请帮助我

  • 问题内容: 我想做的是: 接受用户名(uname)和密码(passw)作为用户输入。 使用,检索唯一的元组,数据库中的用户名和用户适合的用户名均指向该元组。该元组将包含用户名和密码。 如果用户提供的密码也适合数据库中的密码,则显示以下信息:两种认证均正确,否则其中一种错误。 除一种情况外,一切正常。当用户名本身错误时,mysql将根本找不到该属性,并且会出现此错误: 代码是: 我有什么办法可以一次

  • 我是java和SQL新手,我在重复一个我不知道如何避免的问题: 假设我想创建两个执行查询,一个在getRequest estsFromDB方法中的另一个内部,我创建第一个执行查询,在第二个方法中,我创建第二个执行查询: 第二个功能: 在第二个循环中,我得到了一个SQLException:“ResultSet关闭后不允许操作”,我尝试了:最后用result关闭结果。close()但我也得到异常nul

  • 问题内容: 有一个对话列表,每个对话都有一个消息列表。每个消息都有一个不同的字段和一个字段。我们需要考虑的是,在对话的第一条消息中使用了动作,在几条消息中使用了动作之后,过了一会儿,依此类推(有一个聊天机器人意图列表)。 将对话的消息动作分组将类似于: 问题: 我需要使用ElasticSearch创建一个报告,该报告将返回每次会话的;接下来,我需要对类似的东西进行分组并添加一个计数;最终将导致as

  • 问题内容: 我希望将空结果集的总计设置为0。我尝试了以下方法: 结果: 子问题:上面的工作在Oracle中行不通吗? 问题答案: 在有关聚合函数的文档页面中: 应该注意的是,除了这些函数, 当没有选择任何行时 ,这些函数将 返回空值 。特别是,没有行返回空值,而不是预期的零值。必要时,该函数可用于将零替换为null。 所以,如果你想保证返回的值,适用于 结果 的,而不是它的参数: 至于Oracle