user1461771
user1461771

Reputation: 41

SQL statement is empty error

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

Upvotes: 0

Views: 234

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94635

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.

Upvotes: 0

Jacob
Jacob

Reputation: 14731

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

Upvotes: 1

Related Questions