JDBC Questions

i dont know sql too well and this is why im having a problem at the moment. ive been posting a lot lately with different questions and u guys have really helped me out. but i thought that this time id post a few questions in 1 post :)

1) i want to be able to update a contact from a certain table (e.g. table "A" if the contacts name starts with an A). this is my updateContact method:

publicvoid updateContact(ContactPanel updatePanel){

try{

preparedStatement = connection.prepareStatement("update A set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?");

preparedStatement.setString(1, updatePanel.getName());

preparedStatement.setString(2, updatePanel.getNumber());

preparedStatement.setString(3, updatePanel.getEMail());

preparedStatement.executeUpdate();

}catch(SQLException ex){

ex.printStackTrace();

}

}

first of all this will only work on table A, but i have tables from A-Z. so instead of"update A set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?"

what would i need to put instead of A? i thought i could maybe do this:"update " + updatePanel.getName().toUpperCase().charAt(0) +" set NAME = ?, NUMBER = ?, EMAIL = ? where ID = ?"

also, u probably noticed that i have 4 ?s and only 3 setters. i have made an identity column in each table called "ID". is there a setter for this?

2) almost like the 1st question but this time i want to be able to delete a certain contact. this is my deleteContact method:

publicvoid deleteContact(String contactName, String contactNumber, String contactEMail){

try{

preparedStatement = connection.prepareStatement("delete from A where NAME = ? and NUMBER = ? and EMAIL = ?");

preparedStatement.setString(1, contactName);

preparedStatement.setString(2, contactNumber);

preparedStatement.setString(3, contactEMail);

preparedStatement.executeUpdate();

}catch(SQLException ex){

ex.printStackTrace();

}

}

can i put an * instead of the A to be able to delete contacts from any table?

[2797 byte] By [Alex1989a] at [2007-11-27 6:09:34]
# 1

> what would i need to put instead of A? i

> thought i could maybe do this:

> "update " + updatePanel.getName()...

Yes that is probably what you have to do.

Alternatively you might write a stored procedure. And it does 26 inserts while your java code just calls it once.

> also, u probably noticed that i have 4 ?s and only 3

> setters. i have made an identity column in each table

> called "ID". is there a setter for this?

Usually you don't set 'identity' fields on inserts via a java value.

If you have parameter(?) you must match it however.

>can i put an * instead of the A to be able to delete contacts from any table?

No.

If it was me I would ask why all of those tables exist. If there was a real reason then I would probably create a stored proc. If there was no reason then I would still create a proc and then when the database was refactored only the proc, not java, would have to change.

jschella at 2007-7-12 17:13:42 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...