Why is not deleting the register on my database?

publicvoid deleteRegister(){

try{

String driver ="org.hsqldb.jdbcDriver";

String url ="jdbc:hsqldb:file:./db/database";

String userId ="sa";

String password ="";

String sqlDelete ="DELETE FROM CLIENTS WHERE ID_CLIENT = ?";

Class.forName(driver);

Connection connection = DriverManager.getConnection(url, userId, password);

String id = JOptionPane.showInputDialog(null,"Enter ID Client to be deleted:","ID", 3);

int idInt = Integer.parseInt(id);

PreparedStatement pstmt = connection.prepareStatement(sqlDelete);

pstmt.setInt(1, idInt);

int deleted = pstmt.executeUpdate();

if(deleted != 1){

JOptionPane.showMessageDialog(null,"Could not delete the register. Please verify the ID you've entered., "Result.", 3);

pstmt.close();

connection.close();

}

else{

connection.commit();

JOptionPane.showMessageDialog(null,"Register Deleted!","Result, 1);

pstmt.close();

connection.close();

}

}

catch(ClassNotFoundException cnfe){

cnfe.printStackTrace();

}

catch(SQLException sqle){

sqle.printStackTrace();

}

}

[2131 byte] By [coffee95a] at [2007-10-3 4:34:42]
# 1

PreparedStatement

"Returns:

either (1)

the row count for INSERT, UPDATE, or DELETE statements or

(2) 0 for SQL statements that return nothing"

According to your code, you may get report of failed to delete even if the

record was deleted.

if(deleted != 1) {

what exactly is happening when you run the code?

r035198xa at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> int deleted = pstmt.executeUpdate();

> if(deleted != 1) {

Please do not rely on code like that. It often doesn't work. It is database and driver dependent.

There are three possible scenarios.

1. The where clause matches nothing.

2. The where clause match something and the delete succeeds.

3. The where clause matches something and the delete fails. An exception will occur.

Case 2 and 3 take care of themselves. It is only case 1 that you are dealing with. And what you should actually being doing there is letting the user pick from a list of known (because you query and display them) entities. But since you are not you can do a select count first to see if the the entered value matches something.

jschella at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

In fact, there's no error message after running this code.

It just doesn't delete anything.

I go and check the database files and all registers still there. But the program shows the message that the register has been deleted and if I try to delete that register again I've a message error, like my program previews. Wonderful, but I restart the program or check the database files and the registers deleted are still there.

I need help.

Thanks!

coffee95a at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
do a connection.commit(); after executing the delete statement.If that does not work first execute a select statement and see if your id match with one of the records.
LRMKa at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Is the code from the first post what is actually happening?I also thought transactions could be the problem here but I don't see how if every action creates a new connection.
cotton.ma at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
> In fact, there's no error message after running this> code. > It just doesn't delete anything.Which would be possibility 1 in my list.
jschella at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

Ahh... I had that problem on the Hypersonic database... it's a tricky little thing.

You have to either

a) execute the "shutdown compact" SQL statement to "commit" the data from memory to disk or

b) I added ";shutdown=true" to the URL, which has the same effect, but with zero code changes.

So try using the url "jdbc:hsqldb:file:./db/database;shutdown=true"

regards,

Owen

omcgoverna at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
brilliant tidbit, owen. i don't use HSQL, so I never would have guessed.%
duffymoa at 2007-7-14 22:38:22 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...