Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not using Class.forName(), so why is this working?

 
K.C. Brawley
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all. Wonderful forum you all got here. Seems like a good group

So here's an off-the-wall thread for you guys. I'm not wondering how to fix a problem. I'm wondering why something isn't breaking, or more aptly, isn't still broken. Here's the run-down:

I'm learning JSP and Servlets on my own. I thought what better way than to create a simple login page. I ran into the "No suitable driver found" error message and after much Googling did not find a solution that worked for me. I had the JDBC driver for Postgres in my build path and darnit I thought that's all I should have to do. Well, then I found a suggestion that I thought got me a step further and that was using Class.forName("org.postgresql.Driver"). Well, then that resulted in a Class Not Found exception. Fooey, I thought. Back to the Google.

So buried in the search results was a simple suggestion: add the JAR file to Tomcat's /lib folder. I removed the forName() line and JAR from the class path (per the suggestion the latter was useless). Ran the JSP and woila, everything worked. You'd think I'd be happy but now I'm burdened by something else: why is this working without forName()? There are dozens of search results that indicate this is required. I shut down and re-opened Eclipse, tried putting forName() back in the Servlet and it still throws an exception. Below are screen shots of my project:





Any ideas? Thanks in advance.

Edit: the pictures in the post don't show much so here's the direct links:

http://oi41.tinypic.com/fwsied.jpg

http://oi40.tinypic.com/6ei2au.jpg
 
Wei Dai
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>I ran into the "No suitable driver found" error message
You should use
try{
Class.forName("org.postgresql.Driver").newInstance();
}catch(Exception e){
System.err.println(e);
}
in public Login()

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loading the driver manually isn't required if you're using a JDBC 4.0 compatible driver, because the service provider mechanism is used. Have a look at the DriverManager API documentation for more details, but it basically boils down to the JDBC driver's JAR needing a file called java.sql.Driver in META-INF/services that contains the fully-qualified name of the driver class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic