EmailSurfer
asked on
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(qu ery);
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu ery);
Statement statement = connection.createStatement
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(qu
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu
ASKER
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(qu ery);
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu ery);
?
Statement statement = connection.createStatement
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(qu
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes, this looks like you have the right idea.
Statement statement = connection.createStatement ();
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(qu ery);
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu ery);
Statement statement = connection.createStatement
String query = "INSERT INTO author(authid, lastname, firstname)VALUES
statement.executeUpdate(qu
query = "INSERT INTO isbn(authid, isbn)VALUES
statement.executeUpdate(qu
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.prepareStatemen t( "INSERT INTO author( authid, lastname, firstname ) VALUES ( ?, ?, ? )" ) ;
s.setInt( 1, id ) ;
s.setString( 2, lastname ) ;
s.setString( 3, firstname ) ;
s.executeUpdate() ;
int id = 1 ;
String lastname = "Yates" ;
String firstname = "Tim" ;
PreparedStatement s = connection.prepareStatemen
s.setInt( 1, id ) ;
s.setString( 2, lastname ) ;
s.setString( 3, firstname ) ;
s.executeUpdate() ;
You need to have two inserts.
Best Regards
Dave