Help me with my code plz
I work on a servlet that connects to Access using JDBC -ODBC bridge
but it doesn't retrieve anything.So cud u plz help me?
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
publicclass first_countextends HttpServlet{
private Statement statement =null;
private Connection connection =null;
private String URL ="jdbc:odbc:rjdemo";
publicvoid init( ServletConfig config )
throws ServletException
{
super.init( config );
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );
connection =DriverManager.getConnection( URL,"","" );
}//try
catch ( Exception e )
{
e.printStackTrace();
connection =null;
}//catch
}
publicvoid doPost( HttpServletRequest req,
HttpServletResponse res )
throws ServletException, IOException
{
String cnt;
cnt = req.getParameter("big" );
//takes the parameter of a radio button in a form
PrintWriter out = res.getWriter();
res.setContentType("text/html" );
if ( cnt.equals("big1"))
//if the value of the radio button equals big1
{
int i=0;
out.println("The number retrieved = " + big_counter(i));
}//if
out.close();
}
int big_counter(int i)
{
try{
i=0;
statement = connection.createStatement();
ResultSet r= statement.executeQuery("select beg_count from User");
r.first();
i=r.getInt("beg_count");
//the name of column is beg_count it's a number
i++;
statement.executeUpdate("UPDATE User SET beg_count="+i);
statement.close();
}//try
catch ( Exception e )
{
}//catch
return i;
}
publicvoid destroy()
{
try{
connection.close();
}
catch( Exception e ){
}
}
}
JSWDK WebServer Version 1.0.1
Loaded configuration from: file:C:\Program Files\java\jswdk-1.0.1\webserver.xml
endpoint created: localhost/127.0.0.1:8080
com.sun.web.core.InvokerServlet: init
first_count: init
java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY
at sun.jdbc.odbc.JdbcOdbcResultSet.first(Unknown Source)
at first_count.big_counter(first_count.java:98)
at first_count.doPost(first_count.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155
)
at com.sun.web.core.Context.handleRequest(Context.java:414)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)
HANDLER THREAD PROBLEM: java.net.SocketException: Socket is closed
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Unknown Source)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:161)
> Thanx a lot,it worked now. But how can i write a
> delete statement that deletes the records after i
> close the internet explorer window?
First, you need to detect that the IE window is closed which is very obvious. You will have to rely on JavaScript for that. You will have to send a request with some JavaScript trick to a servlet. The servlet will process the request and perform the delete operation.
Use Google to search how Javascript detects the window closing event.