How do you determine if an insert or update was successful using Java and MySQL? How do you determine if an insert or update was successful using Java and MySQL? mysql mysql

How do you determine if an insert or update was successful using Java and MySQL?


Since you are using PreparedStatement you can call executeUpdate() -

 int count = stmt.executeUpdate(); action = (count > 0); // <-- something like this.

From the Javadoc (Returns) link above, emphasis added,

either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.

If you want to insert a large number of entries, I would prefer addBatch() and executeBatch().


First of all this you should know :

boolean execute()Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

ResultSet executeQuery()Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

int executeUpdate()Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.

        int i = stmt.executeUpdate();        if (i > 0) {            System.out.println("success");        } else {            System.out.println("stuck somewhere");        }

Try this and check it out whether insert is happening or not


If you don't get a exception I think query is went ok. Or, you might be able to use executeUpdate() (http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() )

You can do a select count(*) do validate number of records if you want.