This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Kevin Mukhar

Ranch Hand
+ Follow
since Nov 28, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin Mukhar

No, those are not the problem. Neither System.out.println or e.printStackTrace write to the output stream, so they will not cause the exception you see. Something else is writing to or committing the response before the call to forward().
13 years ago

Originally posted by Sen Aff:

....
String URL = "jdbc:mysql:localhost:8084/Practice/School?user=root&password=password";
.....
try {
con = DriverManager.getConnection(URL);
......

and this is the error i had:org.apache.jasper.JasperException: Exception in JSP: /ShowStu.jsp:51

48: Connection con = null;
49:
50: try {
51: con = DriverManager.getConnection(URL);


root cause
javax.servlet.ServletException: No suitable driver found for jdbc:mysql:localhost:8084/Practice/School?user=root&password=password



The URL you are using is incorrect. Check the MySQL JDBC driver docs for the correct format for the url.

[ June 01, 2007: Message edited by: Kevin Mukhar ]
[ June 01, 2007: Message edited by: Kevin Mukhar ]
According to http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html:


...the jar file for your JDBC driver should be installed in $CATALINA_HOME/common/lib



This requirement will be the same for other versions of Tomcat.
It probably depends on which Cloudscape version you are running. Here is one possible tool: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Tools4.html#64964


The Cloudscape product includes a text-based, interactive tool called ij. (This tool is not supported by Sun Microsystems, Inc.) You can run the ij tool by typing this command:

Generally, you want to use 'select count(*)' rather than rs.last(). Think about what would happen if your query could result in 1,000,000 rows and you called rs.last(): the database would have to step through all the rows to get to the one you ask for.
Your first set of code should work, assuming the query returns a row. It normally should, but you can test it by using this code:

If the query returns no rows, then it is possible that the database or driver does not support count(*), or that there is something else wrong with some other code of yours that you did not post.
Your second example will never work. You are asking the result set for a column named 'id,' however your SQL command does not retrieve this column. It retrieves 'count(id).' Use rs.getInt(1) or rs.getString(1).

Originally posted by ransika desilva:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
The error is this ;
stockIT.java [315:1] No method found matching forName(String)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
^
stockIT.java [352:1] No method found matching forName(String)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
^
Thanks


All questions dealing with the API can most easily be answered by consulting the API documentation. Not sure why you would be getting that error message, because the API docs (http://java.sun.com/j2se/1.4.2/docs/api/index.html) for the class Class do indicate that the forName(String) method still exists in JDK 1.4.2.
Since this is a compilation error, I would suspect that the compiler cannot find the library that contains the java.lang classes. To check that, try using some of the other java.lang classes and see if the compiler can find them.
Another possibility is that at sometime you created a class named Class without the forName(String) method, and that new Class overrides the java.lang.Class. To check that, use the fully qualified name in your code.

Originally posted by raghav mathur:
Is is possible to access the database(Mysql) which resides at www.xyz.com through a remote client. That is, can an application which DOES NOT reside on the server ( where the database is )can acess the database by specifying the following statements :
String url = "jdbc:mysql://ip of the site:3306/xxx?user=root&password=";
connection = DriverManager.getConnection(url);
Thanks in advance


Yes, as long as the owner of the database has given access privileges to the indicated user, coming from the given host. Since MySql allows one to use wild card hostnames when setting up privileges, it is indeed possible to allow root access from any client machine on the network. That is why the MySql documentation says that one of the first things you should do is give root a password and restrict access to the root account.

Originally posted by Reid M. Pinchback:
If you literally want simultaneous independent statement execution then you want multiple connection objects, typically managed via a connection pool.


Not true, at least according to the specification:
JDBC 3.0 Specification, Final Release
13.1.1 Creating Statements
Statement objects are created by Connection objects..... Each Connection object can create multiple Statement objects that may be used
concurrently by the program.
Based on your description of the symptoms, my first suggestion would be to ensure that the order of elements in the deployment descriptor is correct. The container is sensitive to the order of elements in the deployment descriptor. My experience has been that if any element is out of place, the container throws an exception, which is the symptom you describe. As soon as you remove the elements, the deployment descriptor is again in the correct order, so then it works.
The order of elements you show in your message is correct, however, they must also be correct in relation to the other elements in the deployment descriptor. Check here to see the order of elements shown in tree form.
Another technique to narrow down the problem is to add each element one by one, restarting each time, until you find the specific element that is causing the problem. Then you need to check that element carefully for the correct syntax, required subelements, etc.
JSPs do not normally concern themselves with the difference between POST and GET. If you REALLY need to do this, you can check the HTTP method with a method call to the implicit request object. The method is getMethod(). You can read about the details of this method in the JavaDoc for HttpServletRequest.
17 years ago
JSP

Originally posted by jack nick:
Hi there,
This seem a very curly problem. Have got some servlet specific parameters in the web.xml. For some reason when the servlet is loaded on startup I am able to get the parameters using the getInitParameter("xyz") method. But for some reason cannot seem to get this when the servlet is initialised on first visit.


It is most likely that this is just a symptom of correct behavior. I have the J2SDKEE reference implementation. I copied your code and deployed it, then ran the server in verbose mode. Regardless of whether I set the servlet to be loaded at startup, or loaded at request time, I never saw the debug statements printed to the console. Obviously, that can't be right. J2SDKEE RI is the latest and greatest, it MUST be calling init() correctly.
So, then I changed all the System.out.println() calls to log(), and I edited the server.xml file to turn logging on for Tomcat. Then I redeployed the servlet and checked the output in the Tomcat log. This time, I could see the output from the init() method. When the servlet was loaded at startup, init was called near the end of the startup process. When I set the servlet to load at request time, init() was called right before service() was called.
So, for Tomcat and J2SDKEE1.4 at least, System.out.println() calls in the init() method do not seem to print to the console. However, init() is still called.

Originally posted by Jan Michael Soan:

try {
Statement statement = connect.createStatement();
String query1 = "SELECT * FROM Records " +
"WHERE IdNumber = '" + t1.getText() + "'";
String query2 = "SELECT * FROM Deductions " +
"WHERE IdNumber = '" + t1.getText() + "'";
ResultSet rs1 = statement.executeQuery( query1 );
ResultSet rs2 = statement.executeQuery( query2 );


The proble is your use of a single Statement object to create two ResultSet objects. As the Javadoc for Statement says:
"By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists."
So, when you execute query2, the ResultSet from query1 is automatically closed.
You should first attempt doing a join as someone else has suggested.
If you still think a join won't work for you, another option is to use two statement objects. You can have multiple open Statement objects created by the same connection without any problem.

Originally posted by sheril she:
could u pls explain when exactly the servlet constructor is called and when the init method is called.since in a servlet's lifecycle there is only the init ,service and destroy.and it is said that the init method is called when the servlet is loaded.so what is the difference between creating instances and loading the servlet?where are these instances stored?


All your questions are more or less answered in the Servlet Spec, para 2.3, which you can find at http://java.sun.com/servlets.
In short, there is no answer to when exactly a method is called. The methods are called relative to events in the container life and request process.
- class files are loaded anytime between when the container is initiated and when a request for the servlet is received
- a servlet instance (or instances) is created by calling the no-argument constructor any time after the class is loaded but before the first request
- the init() method is called any time after the serlvet is instantiated but before the first request
- service() is called for a request
- destroy() can be (but is usually not) called anytime after a request is serviced. If the container is shutting down, it can call destroy() even if all request threads are not complete, if it waits for a timeout period.
You can see when some of these events occur by placing log statements in
- a static initializer (called when class is loaded)
- a no-argument constructor
- the init() method
- the destroy() method
17 years ago

Originally posted by thomas davis:
1)How is this method overriden in HttpServlet Or user-defined servlet?
Since service method is abstract so is GenericServlet class .


Yes, service() in GenericServlet is abstract, and thus so is GenericServlet. The HttpServlet class defines the service() method required by Servlet interface. You can verify this simply by looking at the Javadoc for HttpServlet. There you can see that HttpServlet is declared as an abstract class and not an interface, but that the service() method (and every other method) in HttpServlet is not abstract. You can also verify this by obtaining the servlet API source code. You can find the API source code at http://jakarta.apache.org/site/cvsindex.html

2) If service method implemented in HttpServlet class by CONTAINER, Why HttpServlet class is a abstract class?
3) Moreover there are no abstract methods in HttpServlet class,why is it called abstract class?
4) If container override the method service() in HttpServlet,why is it's access specifier abstract?
As per specification,if we implement the abstract method in its subclass ,no need of mentioning it as a abstract class. Why is it mentioned as a abstract class?


First, the container does not override the service() method. The servlet API implementation provided by the container has the HttpServlet class which contains the service() method. This servlet API can be provided as part of the servlet container. However, nothing prevents you from obtaining a servlet API implementation from some third party and using that with the servlet container. In tomcat 4.1 and earlier, the API binary was in servlet.jar; with Tomcat 5.0, it is now servlet-api.jar.
Second, I'm not sure which spec you are referring to. The Java Lang Spec, P 8.1.2.1 requires that abstract classes to be declared abstract: "If a class that is not abstract contains an abstract method, then a compile-time error occurs." In other words, if the class has an abstract method, the class MUST be declared abstract. However, the spec does not prevent a class with NO abstract methods from being declared abstract. There are other classes in Java like that (for instance, look at the adapter classes like java.awt.event.MouseAdapter).
Which access specifier is abstract? The access specifier for service() is public void for the service() method required by Servlet, and protected void for the overloaded version provided by HttpServlet. The HttpServlet class is declared to be abstract, because you are expected to implement your own servlet class.
The HTTP spec defines various HTTP methods such as GET, POST, etc. HttpServlet provides doGet() and doPost() and various other methods to respond to HTTP Requests. However, doGet and doPost are implemented to return an error message to the user saying GET not supported, or POST not supported. You are expected to extend HttpServlet and implement doPost and/or doGet and/or any of the other methods to provide an implementation that is meaningful to your web application. HttpServlet is declared abstract to force you to implement your own servlet class, where you will presumably implement your own doPost or doGet method.
HttpServlet defines NO abstract methods. However, the designers of Servlets did not want you to simply use HttpServlet as your servlet class. Thus, to force you to implement your own servlet class, the class is marked as abstract.
[ February 07, 2003: Message edited by: Kevin Mukhar ]
17 years ago