Well, I want to make a modification in my database, so I need to use alter table but java seems to have problems making that.
This is the sentence
ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id);
how do you execute it?
I was doing this:
rawStatement="ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id);";
currentStatement = conn.createStatement();
currentStatement.execute(rawStatement);
is the last line correct?
As far I know, execute must run everything.
Use this:
executeUpdate()
instead of
execute()
Also if the constraint is already present it will throw an exception
Other things you should look out for:
Try with the below code:
rawStatement="ALTER TABLE loans ADD FOREIGN KEY (id_reader) REFERENCES readers (id)";
PreparedStatement ps = conn.prepareStatement(rawStatement);
ps.executeUpdate();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With