import java.sql.*; public class MySQLTest { private static Statement statement; public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mysqltest"); statement = conn.createStatement(); runSql("DROP TABLE IF EXISTS test"); runSql("CREATE TABLE test (id INT PRIMARY KEY, data VARCHAR(100))"); runSql("INSERT INTO test (id, data) VALUES(1, 'a')"); int rowsAffected = runSql("INSERT INTO test (id, data) VALUES(2, 'bb') " + "ON DUPLICATE KEY " + "UPDATE data=values(data)"); System.out.println("Inserting a row -- should return 1: " + rowsAffected); rowsAffected = runSql("INSERT INTO test (id, data) VALUES(2, 'bbb') " + "ON DUPLICATE KEY " + "UPDATE data=values(data)"); System.out.println("Modifying a row -- should return 2: " + rowsAffected); rowsAffected = runSql("INSERT INTO test (id, data) VALUES(2, 'bbb') " + "ON DUPLICATE KEY " + "UPDATE data=values(data)"); System.out.println("Not modifying a row -- should return 0: " + rowsAffected); } private static int runSql(String sql) throws SQLException { return statement.executeUpdate(sql); } }