0

I saw a lot of guides here, that says

if (!res.next())

should check if statement is empty, but when I do it I get this error

org.apache.jasper.JasperException: An exception occurred processing JSP page /irasyti.jsp at line 73

70:     String rezervbusena=""; // rezervo busenos info
71:     String uzsakymas="";    // uzsakymas info
72:     
73:     if( !res.next()){ //jei kambariai neuzimti
74:         try {

I don't know what is wrong; maybe I need to import some utility? :/ my code:

    ResultSet res = null;
    Statement stmt = null;
        try {
            stmt = connection.createStatement();
            //res=stmt.executeQuery
            String tas=("SELECT * from table");  
            res=stmt.executeQuery(tas);
            out.println("<br>tas: "+tas + "<br>");
        }catch(Exception ex){
            out.println("Klaida skaityme: "+ex);    
        }

    if( !res.next()){ //jei kambariai neuzimti
            try {}
               catch(Exception ex){
            out.println("error: "+ex);
        }
}

error was that res resultset was null and after mysql return empty statment it stays null so i had to check if its null first if(res!=null)

thank yuo all for answering

2 Answers2

1

Can you try with the following code and see whether you are getting any errors?

ResultSet res = null;
    Statement stmt = null;
        try {
            stmt = connection.createStatement();                        
            //res=stmt.executeQuery
            String tas=("SELECT * from table");  
            res=stmt.executeQuery(tas);
            out.println("<br>tas: "+tas + "<br>");       

    if(res.next()){ //jei kambariai neuzimti

            out.println("rs value "+rs.getString(1);
        }
}
catch(Exception e){
e.printStackTrace();
}
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • Issue-1. The *next* method returns true when that row has *data*. Issue-2. In finally block, `close()` methods throws an exception. – KV Prajapati Aug 25 '12 at 03:23
  • java.lang.NullPointerException org.apache.jsp.irasyti_jsp._jspService(irasyti_jsp.java:164) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:332) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) – user1461771 Aug 26 '12 at 13:15
  • @user1461771 1) Can you make sure that `SELECT * from table` returns any rows? 2) Check line number 164 in `irasyti_jsp.java`. And if possible post your full error stacktrace. – Jacob Aug 26 '12 at 13:43
0

Note sure what Exception you've got but here are some explanations/tips.

Firstly you've written Java code in JSPs and it is not consider as good practice.

Secondly the method boolean ResultSet.next() returns true if fetched row has data; returns false otherwise.

and finally, JDBC API classes located at java.sql package so either import this package or just use classes/interfaces along with package name - java.sql.Connection etc.

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186