• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

DriverManager.getConnection() throws exception when getting PostgreSQL driver only with Tomcat

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Working with netbeans IDE 8.2. This small snippet (index.jsp) shows the problem. The purpose is just getting a java.sql.Connection value. In a succesful case, this would be a HellowWorld aplicacion.

When using Glassfish 4.1.1 it works well (localhost, my PC). Instead, with Apache Tomcat, the exception is thrown. DriverManager.getConnection() is the method that fails, launching the exception.




Any help would be hightly appeciated. Spent more than one month on the net looking for it.

Thankyou.

Carles
 
Bartender
Posts: 20838
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Carles!

1. You really shouldn't put Java code on your JSPs. It's outdated, and besides, it's murder to debug. Put the Java code in a class. A popular paradigm is to handle the HTTP request in a servlet (Controller), have the servlet produce/populate JavaBeans (Model objects) and have the JSP define the View, employing the useBean JSP tag to retrieve the bean property values for display.

2. You really shouldn't use DriverManager to get a Connection for a J2EE webapp. It's OK for learning purposes, but Java is an expensive language to use, so it's best employed on high-performance applications, where you'd use a ConnectionPool supplied by the server instead.

3. If something blows up we need a stack trace. "It throws an exception" doesn't really tell us anything. Literally dozens of things can cause an exception here.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To have that code produce a stack trace, change it to look like this:



However as Tim mentioned, the exception being thrown isn't necessarily an SQLException so that might not help this particular problem. (Everything else Tim mentioned, you should take that into account too.)
 
Tim Holloway
Bartender
Posts: 20838
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, since this is a web application, I'd recommend getting rid of the try/catch/print exception stuff altogether. That way the full stack trace goes to the server log (catalina.out in Tomcat's case).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!