Validate whether INSERT was successfully executed or not (JTDS)

Asked

Viewed 883 times

2

I would like to know a form of validation to know if the Insert was successfully executed, I have no idea how to do this because the execute() method does not return anything!!!

Connection connInsert = DriverManager.getConnection(ConnectionURL);
PreparedStatement inserir = connInsert.prepareStatement("INSERT INTO PRODUTO (ID, NOME, QTDE) VALUES (varId, varNome, varQtde)");
inserir.execute();

I just wanted to know how to validate because if I made a mistake when entering I would like to give the option to try again!!!

3 answers

3


The correct to use the INSERT, UPDATE and DELETE would be the method executeUpdate(), in which it returns a value of the integer type. The method execute() works for CREATE TABLE or ALTER TABLE and returns false or true. See below how your code should actually look:

PreparedStatement inserir = connInsert.prepareStatement("INSERT INTO PRODUTO (ID, NOME, QTDE) VALUES (varId, varNome, varQtde)");
int result = inserir.executeUpdate();

if(result > 0){
   // produto inserido com sucess
} else {
   // erro ao inserir produto.
}
  • i need to execute "insert.execute()" before the if loop?

  • @Marciovieira sorry, I got in the way. See now if it clears up for you.

  • 1

    Good, solved the problem.... Thanks =D

1

You can change the execute() for executeUpdate() and test the return value. According to official documentation:

int executeUpdate() throws Sqlexception

Executes the SQL statement in this Preparedstatement Object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that Returns Nothing, such as a DDL statement.

Returns:

either (1) the Row Count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that Return Nothing

Throws:

Sqlexception - if a database access error occurs; this method is called on a closed Preparedstatement or the SQL statement Returns a Resultset Object

Sqltimeoutexception - when the driver has determined that the timeout value that was specified by the setQueryTimeout method has been exceeded and has at least attempted to Cancel the Currently running Statement

https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate()

1

Do the following:

Connection connInsert = DriverManager.getConnection(ConnectionURL);
PreparedStatement inserir = connInsert.prepareStatement("INSERT INTO PRODUTO (ID, NOME, QTDE) VALUES (varId, varNome, varQtde)");

if(inserir.executeUpdate() > 0) {
    // inserido com sucesso
} else {
    // erro ao inserir
}

According to the documentation of executeUpdate() will return 1 if the insertion is successful and 0 if it is not possible. Further remembering that you should treat the exceptions SQLException and SQLTimeoutException which will be launched by the method.

Use only execute() is not feasible in this case as it is necessary to check your return.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.