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
  • have you checked that res is not null? Commented Aug 24, 2012 at 18:29
  • What exception description? Commented Aug 25, 2012 at 3:26

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Issue-1. The next method returns true when that row has data. Issue-2. In finally block, close() methods throws an exception.
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 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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.