0

I'm very new to web-application programming with Java. I have two questions.

First: I have a postgresql database. I can access the database fine when doing it outside of a servlet (as a JUnit4 test). However, when I try to access it via a servlet I get:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/comic_store
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at ilya.database.DBService.<clinit>(DBService.java:15)
at ilya.database.UserGateway.getUserByEmail(UserGateway.java:29)
at ilya.controller.Login.<init>(Login.java:12) 

etc. Why would this exception get thrown while accessing from a servlet, but not otherwise.

Second: What is the preferred way of initializing a database connection in servlets? Should I be creating a DataSource with web.xml?

webhound
  • 339
  • 1
  • 4
  • 13

1 Answers1

0

Found my answer here . And Apache's JNDI Datasources HOW-TO also helped. Here's what worked: adding the driver jar to %CATALINA_HOME%/lib instead of WEB-INF/lib and then using Class.forName() to load the driver. I tried excluding Class.forName() and got the same exception. I also tried Class.forName() with the driver only in WEB-INF/lib, and that also didn't work. As I said above, I'm running Tomcat 7 and jdk 1.6.0.

Community
  • 1
  • 1
webhound
  • 339
  • 1
  • 4
  • 13