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!!
Try removing the
con.commitline, there is no need for that.You should add
finallyto yourcatchand close the statement there.And you should also use PreparedStatement instead of Statement.
About
finallysee here.