Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Single Insert Query for Multiple Tables  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code does not compile when I inserted the other tables and Im not really good at SQL statements and such. Is it possible if I can use a single query for inserting into multiple tables?

Code below

 
Rancher
Posts: 3530
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, no.  You can't do mutliple SQL queries in the one Statement.
So your currnet one will need to be split up into three calls, which shouldn't be a major issue?

However.
Since you are using a preparedStatement then you ought to be using it as a PreparedStatament.
At the moment you are simply concatenating the SQL together.

It should take the structure:

and then execute it.
That solves any possible issues with quote characters (for example).  I mean, your current one would fail if Brian O'Neill signed up.

Second point is that this really should not be in a JSP page.
JSP should not have java code in them.
This is something that should be in a servlet or, better, a service class that the servlet calls, which would allow you to test it without all the web server stuff.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, right.

We should use one query per transaction only.

And after each transaction, make practice to close it.
 
Dave Tolls
Rancher
Posts: 3530
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SunilK Chauhan wrote:Yes, right.

We should use one query per transaction only.

And after each transaction, make practice to close it.



Well, not per transaction.
Unless you are using "transaction" to mean something other than a database transaction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!