Home » Newcomers » Newcomers » jdbc & Servlets question(jdbc & Servlets question)
jdbc & Servlets question [message #536641] |
Sat, 29 May 2010 01:59  |
Eclipse User |
|
|
|
Hi - My apologies if this isn't the right place to post this question. I've gotten good advice here concerning setting up JSP/Servlets on Eclipse, and this problem is at least somewhat related (maybe a configuration issue?), and probably arising out of my inexperience.
I have two programs that attempt to open a Postgresql database called "GPSdata". One is a Java Project called "TestDB" slightly modified from Horstmann's CoreJava2. The second is a Dynamic Web Project I have written called "Nearby" which consists, at this point, of an HTML file and a Servlet. I am using Apache Tomcat as my web server, and everything is happening on my home PC.
"TestDB" works fine. "Nearby" gives the following error:
No suitable driver found for jdbc:postgresql:GPSdata
"Nearby" was working to the extent that the servlet (when called by an html form submit) would execute a "doGet" and echo the input data fields. I've since stripped out everything but the code that makes the database connection, and am using the identical code (cut & paste) as is in the "TestDB" program.
The connection code is pretty straightforward. Horstmann has set it up so that he gets connection parameters from a text file ("database.properties"). A routine called "getConnection" reads the text file and handles the connection via a DriverManager.getConnection(url, username, password). The calling code is simply the following:
Connection conn = getConnection();
I've copied over "getConnection()" and at this point have both programs using the exact same, fully-spelled-out address for the "database.properties" file. Thus, both programs are using the exact same text file of properties.
Also, both programs have the same Postgresql JDBC4 .jar file as a "Java Source Attachment".
Is there something about Apache Tomcat running that changes how Java finds and uses the JDBC driver? I can't tell from the error message if the driver was not found, or if the driver was found but deemed unsuitable. I've done about as much troubleshooting to eliminate as many differences (other than the web context) as I can, and have run out of ideas.
Any help or suggestions would be much appreciated! Thank you!
|
|
| |
Re: jdbc & Servlets question [message #536704 is a reply to message #536641] |
Sun, 30 May 2010 03:06   |
Eclipse User |
|
|
|
Thank you! This seems like it must be a step in the right direction. I updated what was an obsolete reference in "Preferences/Data Management/Connectivity/Driver Definititions". The more current "PostgreSQL JDBC Driver" has been entered, and now appears in the web project as a library icon under "Java Resources: src/Libraries". It previously was in the same spot but with a jar icon.
[in the previous post, when I referred to "Java source attachment," I was reading the header label given when clicking on the properties of the JAR icon in the library area. The properties now calls the library a "Connectivity Driver Definition".]
Unfortunately, I'm still getting the error:
- java.sql.SQLException: No suitable driver found for jdbc:postgresql:GPSdata
on the last line of code of the "getConnection" function:
- return DriverManager.getConnection(url, username, password);
I'm unclear from the error message "no suitable driver found" as to whether the driver wasn't found, or if the found driver is not suitable. I guess a third possible interpretation is that the address specified was problematic.
If the driver or database is "not found," could it have to do with the program running on Tomcat and this changing the way the jdbc.url is interpreted? In both of my programs, the connection information is loaded from a text file "database.properties," which follows:
jdbc.url=jdbc:postgresql:GPSdata
jdbc.password=********
jdbc.username=postgres
jdbc.drivers=org.postgresql.Driver
I've seen other jdbc URL specifications include the local host and more in the third section of the specification. In this case, the physical location of the database ("GPSdata") is not a subfile of localhost, but elsewhere on the c: drive. (Do I need to move it? Perhaps to WEB-INF?)
The other possible interpretation of the error, "not suitable", makes me wonder if the PostgreSQL JDBC driver has some limitations in that it works in the Java Application environment but not the Servlet environment--but this speculation is another "rookie wild guess" as I am unclear as to whether Java works differently in these two contexts. Along those lines, I have posted this question at a PostgreSQL forum, to find out if I am misinterpreting how this JDBC can or cannot be used.
|
|
|
Re: jdbc & Servlets question [message #537896 is a reply to message #536662] |
Fri, 04 June 2010 02:40   |
Eclipse User |
|
|
|
I spent a bit of time on a Postgresql forum site, and now think the problem is not the driver, but something incorrect about how I have indicated its location.
The initial error message was as follows:
No suitable driver found for jdbc:postgresql:GPSdata
One suggestion had me adding the following code:
this.getClass().getClassLoader().loadClass("org.postgresql.Driver ");
But the error persists, with this message being given:
java.lang.ClassNotFoundException: org.postgresql.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(Webap pClassLoader.java:1516)
at org.apache.catalina.loader.WebappClassLoader.loadClass(Webap pClassLoader.java:1361)
at nearbyGPS.NearByHub.<init>(NearByHub.java:31)
In this instance, the line of code was in the constructor, but the same thing happens as the first line of the doGet procedure.
This seems to indicate the JDBC is not being found.
I don't know much about what should be in the .classpath, the .project and .jsdtscope files. They all seem to pertain to build paths or class paths, but only the .classpath has a reference to the jdbc, as follows:
<classpathentry kind="con" path=" org.eclipse.datatools.connectivity.jdt.DRIVERLIBRARY/Postgre SQL JDBC Driver"/>
Should there be a reference in the .project or .jsdtscope files? Is the <classpathentry> tag in the .project correct and sufficient?
In the Project Explorer, under "Java resources" and "Libraries", the library "PostgreSQL JDBC Driver" is shown with a library icon. Under it is it's .jar file, and under that, a long list of org.postgresql packages, the first of which has class Driver. So this seems to indicate to me that the name and address I gave for the jdbc .jar is OK.
I took another look at the "Configure Build Path" for the project, and "PostgreSQL JDBC Driver" is there in the Libraries section, and in the "Order and Export" tab, it is the last of six entries, where all except the first is unchecked, and the first (package icon: Nearby/src) has a small green box in the checkbox.
I wish I understood better how the different configuration files worked, the build sequence, things like this 'Order and Export". If you have a reference work that goes into this level of detail, it could be very helpful.
Meanwhile, I hope there is something here that will help diagnose if something is missing or wrong in how this project is configured.
Many thanks,
Phil Freihofner
|
|
|
Re: jdbc & Servlets question [message #538113 is a reply to message #537896] |
Sat, 05 June 2010 02:37  |
Eclipse User |
|
|
|
This is a last message to describe the solution to the problem, just in case anyone is following behind me.
Many thanks to Russell Bateman!
If the project name is right-clicked and the "properties" are selected, there is a selection: "Java EE Modules Dependencies". In this area is a reference to the PostgreSQL JDBC Driver library. Once I checked the box for this option, the driver was added "as a Web Library dependency." Then, I was able to run the program and successfully connect the to PostgreSQL database.
As a side note: there was a warning in the Markers tab that offered a useful clue.
Classpath Dependency Validator Message:
Classpath entry org.eclipse.datatools.connectivity.jdt.DRIVERLIBRARY/Postgre SQL JDBC Driver will not be exported or published. Runtime ClassNotFoundExceptions may result.
Indeed, the problem was exactly that, not finding a class in the PostgreSQL JDBC. The Marker warning disappeared after "checking" the box as mentioned above.
|
|
|
Goto Forum:
Current Time: Thu Sep 18 23:28:20 EDT 2025
Powered by FUDForum. Page generated in 0.08667 seconds
|