mysql - java.sql.sql.exception :Illegal operation on empty result set -


i have made simple science quiz java app 4 frames: login, menu, test, , result.

the first 3 frames working fine when click on show result shows error. code result jframe. if want can send coding of test jframe also.

this error

java.sql.sqlexception: illegal operation on empty result set. @ 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) @ java.awt.component.processmouseevent(component.java:6216) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3265) @ java.awt.component.processevent(component.java:5981) @ java.awt.container.processevent(container.java:2041) @ java.awt.component.dispatcheventimpl(component.java:4583) @ java.awt.container.dispatcheventimpl(container.java:2099) @ java.awt.component.dispatchevent(component.java:4413) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4556) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4220) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4150) @ java.awt.container.dispatcheventimpl(container.java:2085) @ java.awt.window.dispatcheventimpl(window.java:2475) @ java.awt.component.dispatchevent(component.java:4413) @ java.awt.eventqueue.dispatchevent(eventqueue.java:599) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:269) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:184) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:174) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:169) @ 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 * result 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); } } 

coding of 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) " + 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 * " + subject + " 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 " + subject;             resultset rs = stmt.executequery(sql);             int = 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 result id = '" + id + "'";             rs = stmt.executequery(sql);             rs.next();             if(res > rs.getfloat(1))             {                 sql = "update result set " + subject + "result = " + res + " 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 command line arguments     */     public static void main(string args[]) {         java.awt.eventqueue.invokelater(new runnable() {             public void run() {                 new test().setvisible(true);             }         });     } 

change code

rs.next(); for(int i=1; i<=4;i++) newrow = rs.getstring(i+1); defaulttablemodel tm = (defaulttablemodel)scoretbl.getmodel(); tm.addrow(newrow); 

to

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

edit:

in code written this

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

change

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

if above condition true next statement evaluated.


Comments