Avoid Exception Error on msql.query()

edited January 2017 in Library Questions

The following Code Snippet gives run-time error

 //===Read From MySQL==========================
  if (msql.connect()) {
    //int recCount = msql.query("SELECT * FROM %s  WHERE %s IS NOT NULL", "interface", "ToSerial");
    //if (recCount > 0) { 

    msql.query("SELECT * FROM %s  WHERE %s IS NOT NULL", "interface", "ToSerial");
    try {
      String serialTxt = msql.getString("ToSerial");
      if (serialTxt != null) {
        cp5.get(Textfield.class, "Transmit").setText(serialTxt);
        int deleteID = msql.getInt("ID");
        msql.query( "DELETE FROM %s WHERE  %s = %s", "interface", "ID", deleteID );
      }
    }
    catch (RuntimeException ex) {
      // println(ex);
    }
  }

The Error on the Processing 2.2.1 IDE is as follows:-

SQL.getString(): java.sql.SQLException.
java.sql.SQLException: Illegal operation on empty result set.
    at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:693)
    at com.mysql.jdbc.ResultSet.getStringInternal(ResultSet.java:4832)
    at com.mysql.jdbc.ResultSet.getString(ResultSet.java:4810)
    at com.mysql.jdbc.ResultSet.getString(ResultSet.java:4826)
    at de.bezier.data.sql.SQL.getString(Unknown Source)
    at CIOMSerial.draw(CIOMSerial.java:107)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Unknown Source)

After sometime, it fails to connect to the database. I couldn't find a way to check if the Query() has returned any records or not. Please anyone, kindly provide a way out.

Answers

  • Answer ✓

    I have resolved the problem myself.

    But I would like to know if it is correct or Safe to keep the connection with Database Open continuously or does Connection needs to be opened and closed for each transaction (Read/Write operation).

Sign In or Register to comment.