Reputation: 3462
I'm developing a webservice using JSON that inserts the email of g+ user(once the user chooses to login to my app through google sign-in). I don't want my webservice to enter multiple entries for the same user into my table. I want to overwrite the old entry of email with the newer one. what should I do?
Edit 1: //setGplusEmail
public ArrayList<Status> setEmail(Connection con,String gplusMail) throws SQLException
{
ArrayList<Status> gplusUserList = new ArrayList<Status>();
//con.setAutoCommit(false);
String sql = "INSERT INTO gpluslogin (gmail) VALUES ('"+gplusMail+"')";
String sql1 = "INSERT INTO gpluslogin (gmail) VALUES ('"+gplusMail+"') ON DUPLICATE KEY UPDATE (gmail = '"+gplusMail+"')";
PreparedStatement stmt = con.prepareStatement(sql);
PreparedStatement stmt1 = con.prepareStatement(sql1);
try{
stmt.executeUpdate();
stmt1.executeUpdate();
}catch(Exception e){
}
Status s = new Status();
s.setStatus("Successfully Inserted...");
gplusUserList.add(s);
return gplusUserList;
}
Is this the right way to execute?
Upvotes: 0
Views: 1277
Reputation: 6854
After creating unique key on gmail column you can use any one statement out of below 2-
REPLACE INTO gpluslogin (gmail) VALUES('[email protected]');
OR
INSERT INTO gpluslogin (gmail) VALUES('[email protected]') ON DUPLICATE KEY UPDATE gmail='[email protected]';
Upvotes: 1