java.sql.sql.exception:对空结果集

时间:2016-01-24 16:01:46

标签: java mysql swing jdbc

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

前三帧工作正常但是当我点击显示结果时它显示错误。 这是我在Result JFrame中的代码。如果你想我也可以发送Test JFrame的编码。

这是我的错误

  

java.sql.SQLException:对空结果集的非法操作。           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)           在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)           在com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)           在com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)           在com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)           在com.mysql.jdbc.ResultSetImpl.getFloat(ResultSetImpl.java:2499)           在Test.ResultBTNActionPerformed(Test.java:292)           在Test.access $ 500(Test.java:18)           在Test $ 6.actionPerformed(Test.java:215)           在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)           在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2318)           在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)           在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)           在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)           at java.awt.Component.processMouseEvent(Component.java:6216)           在javax.swing.JComponent.processMouseEvent(JComponent.java:3265)           在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)           在java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

import java.sql.*;
import javax.swing.table.*;
import javax.swing.JOptionPane;
public class Result extends javax.swing.JFrame {
float result;
String ID;
/** Creates new form Result */
public Result() {
initComponents();
}
public Result(float res, String id)
{
initComponents();
result = res;
ID = id;
Score.setText(res + "%");
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 Result where ID = '" + ID + "'";
ResultSet rs = stmt.executeQuery(sql);

Object[] newrow = new Object[5];
newrow[0] = "MAX MARKS";
rs.next();
for(int i=1; i<=4;i++)
newrow = rs.getString(i+1);
DefaultTableModel tm = (DefaultTableModel)scoreTBL.getModel();
tm.addRow(newrow);
}
catch (Exception e) { JOptionPane.showMessageDialog(null,"" + e);
}
}

testJFrame的编码

 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){ JOptionPane.showMessageDialog(null,""+ e);
          }
    }
    private void getQues()
    {
        try
        {
        /*    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ELearning.mdb;";
            Connection conn = DriverManager.getConnection(database, "", "");
            Statement stmt = conn.createStatement(); */
            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){ JOptionPane.showMessageDialog(null,""+ e); }
    }


    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("sun.jdbc.odbc.JdbcOdbcDriver");
            String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ELearning.mdb;";
            Connection conn = DriverManager.getConnection(database, "", "");
            Statement stmt = conn.createStatement(); */
              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){ JOptionPane.showMessageDialog(null,""+ e); }
}                                         

    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);
            }
        });
    }

1 个答案:

答案 0 :(得分:-1)

更改此代码

rs.next();
for(int i=1; i<=4;i++)
newrow = rs.getString(i+1);
DefaultTableModel tm = (DefaultTableModel)scoreTBL.getModel();
tm.addRow(newrow);

DefaultTableModel tm = (DefaultTableModel)scoreTBL.getModel();
while(rs.next())
{
java.util.Vector row = new java.util.Vector();

for(int i=1; i<=4;i++)
{
row.add(rs.getString(i));
}

tm.addRow(row);
}

修改

在您编写的代码中

rs.next();
max = rs.getInt(1);

将其更改为

if(rs.next())
max = rs.getInt(1);

如果上述条件为真,那么只评估下一个语句。