• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Insert data to database using servlet

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we insert data through java servlet?

I can insert data to table by using JSP Scriptlet easily but I have a hard time with servlet.



I am positive that the above code function fine. The problem is whenever I call UserToDB() class inside the servlet, and I got this error: java.lang.ClassNotFoundException: org.postgresql.Driver which mean that my conn (connection) is not connected to the database but I have ensure to do so by calling openConn() before start the transaction.

This is my servlet code:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

java.lang.ClassNotFoundException: org.postgresql.Driver which mean that my conn (connection) is not connected to the database


That means that the JDBC driver is not in the classpath of your web app. You should put the jar file into the WEB-INF/lib directory.
 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did include the postgresql driver in my lib that why I can test my insertion code as well as my JSP scriptlet code. But when I switch from scriptlet to servlet, it gives that error which makes me so confused to debug it.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the openConn method.
 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
openConn (because it is under UserToDB so I set it private):



MyDatabase - getConnection():

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is a bit odd, as it would print "Opened database successfully" even if there are previous errors :-)

OK, so line 3 loads the DB driver, but that fails because the class isn't there somehow. It's odd that it would work within a JSP, but not in a servlet - are both part of the same web app? The one that has the driver jar file in WEB-INF/lib?
 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, that message is odd ; but except that, there is nothing wrong with the code, isnt there? Any way, they are both resided same directory. That why I am so confused now.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Nguyen wrote:Oh, that message is odd ; but except that, there is nothing wrong with the code, isnt there? Any way, they are both resided same directory. That why I am so confused now.



where is the dbName value ?
 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is global variable of MyDatabase
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic