Pergunta

I have checked around, and I have checked up the issue all over Google, tried all kind of solutions, but I haven't found any solution.
The issue is that the DELETE-statement isn't really working.

java.sql.Connection conn = new Connection().getConnection();

try {
    conn = new Connection().getConnection();
    PreparedStatement statement = (PreparedStatement) conn.prepareStatement("DELETE FROM _users WHERE _id = ?");

    statement.setInt(1, 4);
    statement.executeUpdate();
} catch (Exception e) {
    System.out.println(e);
} finally {
    try {
        conn.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

This is the code I am using, and for all I have understood statement.executeUpdate() is the method to use. I have also tried statement.execute() and all kinds of tries.
There is no exception, so no known error. The new Connection().getConnection() gives away an valid connection ( working with SELECT,INSERT,UPDATE ) and the user has permissions for SELECT,INSERT,UPDATE and DELETE.

I am currently using technology such as:

  • Java 6 (1.6)
  • mysql-connector-java-5.1.28.jar
    and I also had the same experience with
  • mysql-connector-java-5.1.19.jar

So I would highly appreciate if anyone had the same problem as I seem to have, what it might be because of and/or how to fix it.
Thank you everyone at Stackoverflow. :) Hope this been relevant to anyone else.

Foi útil?

Solução

To say that something isn't really working does not give us much to go on.

But I suggest that you read up on Transactions. See http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

Unless autoCommit is turned on, you will need to commit your transaction.

Also in your example, you use the ID with the value of 4 - does this really exist?

Outras dicas

You always use the same id to delete from. So whether id 4 exists or not the statement will always execute.

Does the id 4 exist? You can issue this command over and over in mysql: DELETE FROM _users WHERE _id = 4;

So long as the table _users exists, you won't get any error. It'll just keep running. Verify that id 4 exists.

The problem has been resolved! So this answer is for future needs

My connection is set automaticely to conn.setAutoCommit(false) and this works for INSERT, UPDATE and SELECT, and for the DELETE-statement to work is to set connection to conn.setAutoCommit(true), and then add an conn.commit() to make the statement to go through.

Want to thank user2310289 for s/he's answer which helped alot.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top