0

I have made a registration table reg1 and stored the values for every registered users from a HTML file. Now I have made a Log-In page in HTML where users can give their username and see the datas entered by them. I have made an user "bbb" and want to show his username only.So I made the general java code like follows:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class Check extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         // TODO Auto-generated method stub
         String username=request.getParameter("username");
         try{
             Class.forName("oracle.jdbc.driver.OracleDriver");
             String url="jdbc:oracle:thin:@localhost:1521:XE";
             Connection con=DriverManager.getConnection(url,"system","root");
             Statement stmt=con.createStatement();
             ResultSet rs=stmt.executeQuery("select uname from reg1");


            while(rs.next())    
                {

                String name=rs.getString("uname");
                if(name==username)
                {


                response.setContentType("text/html");
                PrintWriter pw=response.getWriter();
                pw.println("Your User name is:"+username);

            //  System.out.println(""+name);
                }



                    con.commit();

                }
             stmt.close();
    }


                catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


    }

   }

It is giving errors like java.sql.SQLException: Closed Statement: next

The table is like follows:

FNAME LNAME ADDR MAIL OCCU UNAME PASSWD aaa aaa aaaa aaa aaaa bbb cccc bkgkb jjv jhvjmh jjkg jvjv jvjvh bjbmb

Please help me resolving this!!

2 Answers 2

1

First there is no need to use

con.commit();

It you can use only in case, if you will set AutoCommit to false.

con.setAutoCommit(false);

Second, i recommend to you use PreparedStatements which are more faster and safer.
Next, there is no need to close your Statement.

In finally block you have to call con.close() when you close connection, statement will be closed automatic.

Note: You have to call it in finally block, because your Application may crash and then your Connection never be closed.

finally {
   if (con != null) {
      con.close();
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply.But when I try to write the above mentioned code Eclipse is giving error that It cannot identify con.So what should I do?
you need to create Connection variable outside of try block so Connection con = null; try { ... }
0

Try removing the con.commit line, there is no need for that.

You should add finally to your catch and close the statement there.

And you should also use PreparedStatement instead of Statement.

About finally see here.

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.