title: Stored Procedure for UPSERT functionality
body: <p>Below is my table in which <code>ID column is Primary Key</code>. And other two columns are string.</p>
  
  <p>I am using a new Database named <code>XpressMP</code>.</p>
  
  <pre><code>Column Name                
  -------
  ID             PrimaryKey                 
  SEARCHES       String
  ACCOUNT        String
  </code></pre>
  
  <p>I am trying to achieve functionality of <code>UPSERT</code> here-</p>
  
  <pre><code>If ID doesn't exists here then insert a new record.
  And ID exists then update the record.
  </code></pre>
  
  <p>I know If I am working with Oracle then I can use <code>MERGE</code> sql command but <code>MERGE</code> is not supported in that database and there is no other command for that as of now. But I believe I can do the same thing with <code>Stored Procedure</code>.</p>
  
  <p>Can anyone provide some suggestions how can I do the same thing with Stored Procedure? As stored procedure will work there.</p>
  
  <p><strong>UPDATED:-</strong></p>
  
  <pre><code>public final static String INSERT = "BEGIN"
  +" INSERT INTO TABLE (ID, SEARCHES, ACCOUNT) VALUES (?, ?, ?)"
  +" EXCEPTION"
  +" WHEN DUP_VAL_ON_INDEX THEN"
  +" UPDATE TABLE"
  +" SET SEARCHES = ?, ACCOUNT = ?"
  +" WHERE ID = ?"
  +" END";
  </code></pre>
  
  <p>Whenever I try to execute the above stored procedure like this</p>
  
  <pre><code>preparedStatement = dbConnection.prepareStatement(INSERT);
  
  preparedStatement.setString(1, String.valueOf(userId));
  preparedStatement.setString(2, Constants.getaAccount(userId));
  preparedStatement.setString(3, Constants.getaAdvertising(userId));
  preparedStatement.executeUpdate();
  </code></pre>
  
  <p>I get exception? Is this the right way to execute it?</p>