Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using Java to connect to a MySQL database. I am trying to insert or update data into the database.

Even though I am quite sure the insert was successful, it returns false.

According to the "execute" API, the return value is "true if the first result is a ResultSet object; false if it is an update count or there are no results".

How can I determine whether or not my insert or update was successful?

    public boolean insertSelections(String selection, String name){         String sql ="INSERT INTO WORKREPORT VALUES (?,?,?,?,?)";         boolean action = false;         try {             PreparedStatement stmt = conn.prepareStatement(sql);             SimpleDateFormat dateFormat =  new java.text.SimpleDateFormat("yyyy:MM:dd hh:mm:ss");             String formatDate = dateFormat.format(new java.util.Date(System.currentTimeMillis()));              java.util.Date mDate = dateFormat.parse(formatDate);             java.sql.Timestamp timeStamp = new java.sql.Timestamp(System.currentTimeMillis()); //          Date time= new Date(mDate.getTime());              stmt.setInt(1, Integer.parseInt(getNumberByName(name).trim()));             stmt.setString(2, name); //          stmt.setDate(3, time);             stmt.setTimestamp(3, timeStamp);             stmt.setString(4, selection);             stmt.setString(5, "N/A");             action = stmt.execute();         } catch (SQLException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }   catch (ParseException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }            return action;     } 
like image 349
user3239558 Avatar asked Jun 24 '14 04:06

user3239558


People also ask

How do I know if a SQL insert is successful?

You can check the @@ROWCOUNT right after insert. If it's more than 0, then the insert succeeded. Also, if @@ERROR = 0 after insert, it was successful. No, check it in T-SQL although if the insert will result in error, most likely the error will be propagated into the client.

What is the difference between insert and update in MySQL?

Insert is for adding data to the table, update is for updating data that is already in the table.

Which is faster update or insert MySQL?

Insertion is inserting a new key and update is updating the value of an existing key. If that is the case (a very common case) , update would be faster than insertion because update involves an indexed lookup and changing an existing value without touching the index.


1 Answers

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().

like image 114
Elliott Frisch Avatar answered Sep 18 '22 12:09

Elliott Frisch