• Post Reply Bookmark Topic Watch Topic
  • New Topic

Insert data to database using servlet  RSS feed

 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the openConn method.
 
Tom Nguyen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
openConn (because it is under UserToDB so I set it private):



MyDatabase - getConnection():

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ivan Krasic
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
It is global variable of MyDatabase
 
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!