• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to insert data to database using connection pooling?  RSS feed

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a website using jsp and servlets with connection pooling. I understand the theory about connection pooling I'm just not sure about the code to insert data to a MySQL database. I have the jsp file and servlet created for this but I am not sure what exactly needs to go into the class that does the sql statement.

here is the code for the class that creates the connection and runs the sql statement:


I know I am probably missing some code but I don't know what. Could someone please help with this?
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's talk about the JDBC issues first. It really doesn't matter where you get your connection from, whether it's a connection pool or whether you create a new connection. The database-updating code would be the same in any case.

you should set up a PreparedStatement for the SQL. You're halfway there, in that your SQL code is written with "?" place-holders. Then you should call the various setXXX() methods on the PreparedStatement to assign values to those place-holders before executing it. And you shouldn't call executeQuery() because an INSERT isn't a query and won't return a ResultSet. You should just call execute().

I'd suggest you have a look at Oracle's JDBC tutorial for more examples; I get the feeling that you are starting with guess-work rather than a sound basis of understanding, so working through the tutorial would be a good idea. When you look at that page I linked to it's going to seem a bit daunting with all of those topics, but really just the first two bullet points are what you need for that sound basis. Don't bother with the parts about RowSet and beyond for now.

As for the rest of the code, I don't think the JDBC code should be in the constructor of a class. Putting the code to insert a customer into a database in its own method is definitely a sound decision, but a class constructor is wrong for that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!