user900481
user900481

Reputation:

Resultset handling error in servlet code

I write a code in servlet for login checking I don't know why I get an error like java.sql.SQLException: No data found, if I had not commented out the String s4 = rs.getString(1) and out.println(s4) line if I commented out this lines I did not get any error.

Why do I get an error like this? I cannot find out the answer.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class login extends HttpServlet {

    Connection conn;
    Statement stmt;
    ResultSet rs;
    String s = "";

    public void init() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("Jdbc:Odbc:edsn");
            s = "Your information is connected ......";
        } catch (Exception e) {
            s = "Exception 1....." + e.getMessage();
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        res.setContentType("text/html;charset=UTF-8");
        PrintWriter out = res.getWriter();
        out.println(s);
        try {

            String ID = req.getParameter("T1");
            String query = "select * from user_db ";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(query);
            out.println("user" + " " + "pass");
            while (rs.next()) {

                try {
                    if ((rs.getString(1)).equals(ID)) {
                        String s4 = rs.getString(1);
                        out.println(s4);

                        out.println("<html><body><h> login Pass.....:(</h></body></html>");

                    }

                } catch (Exception e) {
                    out.println(e);
                }
            }

        } catch (Exception e) {
            out.println("Unable To Show the info... . . ." + e.getMessage());
        }

    }
}

Upvotes: 0

Views: 888

Answers (2)

yael alfasi
yael alfasi

Reputation: 682

Why write the code like this ? It's very wasteful going over the whole table... The IO alone... Why not change to this:

ResultSet rs = null;
PreparedStatement st = null;
try {...
String ID = req.getParameter("T1"); 
            String query = "select 1 from user_db where col_name = ?"; 
            st = conn.prepareStatement(query);
            st.setString(1, ID);  
            rs = st.executeQuery();
if (rs.next()) {
  out.println(ID); 
  out.println("<html><body><h> login Pass.....:(</h></body></html>"); 
}
..

} finally {
if (rs != null) try { rs.close();}catch (Exception e) {}
if (st != null) try { st.close();}catch (Exception e) {}
}

notice prepared statements are cached and better for frequent use

you let the db do what its good at - search the data

select 1 instead of select * does not bring back data you dont really need

jdbc works harder the more columns and data in general you return, so only get what you need

and add a finally block to always close your db connections properly

Upvotes: 1

fgb
fgb

Reputation: 18559

Calling methods on Connection, Statement, or ResultSet depend on which JDBC driver you've loaded. All the values of the ResultSet could be set as soon as the query is made, or they could be retrieved from the database as they're needed, depending on the implementation of the driver.

The JdbcOdbcDriver throws an SQLException after calling getString for a second time. This can be worked around be storing the values in Strings instead of making multiple calls, or by switching to a different driver.

Upvotes: 0

Related Questions