0

I have a ERROR_MSG table which stores error messages with some ids. I want to insert error message if id is not present in table and if its present update error message. Inserting using below java JDBC code.

ID ERROR_MSG
1  ERR1
2  ERR2
3  ERR3

This is my code:

insertQry = "SQL";
Connection con = null;
PreparedStatement stmt = null;
try {
    con = getDataSource().getConnection();
    stmt = con.prepareStatement(insertQry);
    for(ListingAckNackData errorList: listOfListingERROR) {
        stmt.setLong(1, eqGlobalData.getSrcMsgId());
        stmt.setString(2, errorList.getGliId());
        if (null != errorList.getListingRevisionNo()) {
            stmt.setInt(3, errorList.getListingRevisionNo());
        } else {
            stmt.setNull(3, Types.NULL);
        }
        if (null != errorList.getErrorMessage()) {
            stmt.setString(4, errorList.getErrorMessage());
        } else {
            stmt.setNull(4, Types.NULL);
        }
        stmt.addBatch();
    }
    stmt.executeBatch();
}
6
  • 1
    Possible duplicate of Oracle: how to UPSERT (update or insert into a table?), because I don't believe this is possible in a simple INSERT OR UPDATE query with Oracle. Commented Dec 11, 2017 at 7:47
  • Note that this answer provide a simpler solution in Java but will cost some processing... Commented Dec 11, 2017 at 7:49
  • @AxelH how i can use the if with JAVA ? Commented Dec 11, 2017 at 8:32
  • What do you mean by "the if" ? Commented Dec 11, 2017 at 8:36
  • UPDATE tablename SET val1 = in_val1, val2 = in_val2 WHERE val3 = in_val3; IF ( sql%rowcount = 0 ) THEN INSERT INTO tablename VALUES (in_val1, in_val2, in_val3); END IF; how to write this in string insertQry for java code above. Commented Dec 11, 2017 at 8:38

2 Answers 2

1

The simplest solution in JAVA is to check if the row exist.

You start by getting a row count for the specific id you want to insert/update

select count('a') as rowExist from table where id = ?

Then, based on the result, you can easily create your query

if(rowExist > 0){
    query = "update ..";
else
    query = "insert ...";

Note that the parameters are probably not in the same order as you expect, you need to create the insert in the correct order to have the id at the end (since update need a where clause)

insert into Table (name, birthday, id) values (?, ?, ?)
update Table set name = ?, birthday = ? where id = ?
Sign up to request clarification or add additional context in comments.

6 Comments

For that two sql are needed, i want it in single sql, it is not possible in ORACLE ?
@ArpanPaliwal It is possible if you use a stored procedure that you called from Java using a Callable. For that, check the duplicate. There is no need to rewrite the solution here, since everything will be done in a store proc on the DB. You just need to create a Callable to call that procedure.
can't use store proc for a small change thanks for above solution.
With this solution you have to think about transaction handling. Otherwise multiple threads may insert duplicates or run into exceptions if a matching constraint exists.
Isn't having a single MERGE statement that handles both better than having two hits on database?
|
0

It is possible to run a database statement as questioned. Simply use SQL command MERGE INTO... IF NOT MATCHED INSERT... IF MATCHED UPDATE ... You will find an full example and documentation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.