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 ){

}

}

}

[4244 byte] By [Yasmin_249a] at [2007-10-2 7:44:37]
# 1

catch ( Exception e )

{

}//catch

You swallow exceptions which is a bad practice. Print their stack traces for now, they contain very helpgul debug information.

Also, does the same SQL statement return rows whe you execute it in query builder?

Mike

bellyrippera at 2007-7-16 21:29:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Thanx Mike 4 ur advice,i'll fix the catch statement. But could u plz tell me how to write a correct update statement?
Yasmin_249a at 2007-7-16 21:29:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

That looks like a correct update statement already. Provided the update you wanted to do was setting all the records in the User table to have the same beg_count value. But you don't even know yet whether that is the problem, because you didn't see what the exception was and where it occurred. Or even if there was an exception.

DrClapa at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

The user table should contain only 1 record,it updates it & when the servlets are closed it should make the columns return to their default values,i put the exception like u told me:

catch ( Exception e )

{

e.printStackTrace();

}//catch

but i still get errors & i dnt know what they mean..i made a print screen 4 them bs theres no way 2 send them :)..shud i write them?

Yasmin_249a at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Just copy-and-paste the stack trace and post it here. And stop responding to your other thread, you're just getting duplicate answers.

You have a DOS window and you don't know how to copy from it? Click on the icon at the top left corner. You get a drop-down menu. Select Edit and then Mark. Use your mouse to mark the text you want to copy. Then click on the icon again, select Edit and then Copy.

DrClapa at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

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)

Yasmin_249a at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

ResultSet r= statement.executeQuery("select beg_count from User");

r.first(); //<-- The problem seems to be here (if this is line 98)

i=r.getInt("beg_count");

I don't think you need this 'fitst' call. Calling r.next() will do the job and return a boolean so that you know whether it is on a valid row.

Mike

bellyrippera at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
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?
Yasmin_249a at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

> 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.

aniseeda at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
> You will have to send a request with some JavaScript trick to a servlet.The various tricks for doing that kind of thing are now being called "AJAX"; using Google will find lots of discussion and examples.
StuDerbya at 2007-7-16 21:29:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...