Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Question about sql in java?

Hello,

Could anyone offer some advise, about adding records to mysql using java.

I managed all the connection, and understand some sql. The database has two tables:

table author:
authid
lastname
firstname

table isbn:
authid
isbn

When adding values to these tables, since they are linked by the authid field. Could I use one insert statement to add values to both tables.

Or would I need to use two insert statements, one for each table?

Thanks
0
EmailSurfer
Asked:
EmailSurfer
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
TimYatesCommented:
> Could I use one insert statement to add values to both tables.

Nope

> Or would I need to use two insert statements, one for each table?

Yeah :-)
0
 
suprapto45Commented:
Well,

You need to have two inserts.

Best Regards
Dave
0
 
EmailSurferAuthor Commented:
Something like this: the values are not added for the example.

Statement statement = connection.createStatement();
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(query);

query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(query);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
EmailSurferAuthor Commented:
Something like this: the values are not added for the example.

?

Statement statement = connection.createStatement();
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(query);

query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(query);
0
 
petmagdyCommented:
query = "INSERT INTO isbn(authid, isbn) VALUES (123, 'john')"
0
 
reginabCommented:
yes, this looks like you have the right idea.

Statement statement = connection.createStatement();
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(query);

query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(query);
0
 
TimYatesCommented:
prepared statement would be better (probably), as it gets round people entering ' into strings...

int id = 1 ;
String lastname = "Yates" ;
String firstname = "Tim" ;
PreparedStatement s = connection.prepareStatement( "INSERT INTO author( authid, lastname, firstname ) VALUES ( ?, ?, ? )" ) ;
s.setInt( 1, id ) ;
s.setString( 2, lastname ) ;
s.setString( 3, firstname ) ;
s.executeUpdate() ;
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now