Reputation: 5416
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!!
Upvotes: 0
Views: 192
Reputation: 33515
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();
}
}
Upvotes: 1
Reputation: 17940
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.
Upvotes: 0