package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class MySQLTest { final private static String host = ""; final private static String user = ""; final private static String password = ""; final private static boolean useAffectedRows = false; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://" + host + "/test?user=" + user + "&password=" + password + "&useAffectedRows=" + useAffectedRows); PreparedStatement pstmt = connection.prepareStatement("CREATE TABLE test_onduplicate (id integer not null auto_increment primary key, foo varchar(255)) ENGINE=INNODB"); pstmt.execute(); /* 3 new rows */ pstmt = connection.prepareStatement("INSERT INTO test_onduplicate(id, foo) VALUES (?,?), (?,?), (?,?) ON DUPLICATE KEY UPDATE foo=values(foo)"); pstmt.setInt(1, 1); pstmt.setString(2, "a"); pstmt.setInt(3, 2); pstmt.setString(4, "b"); pstmt.setInt(5, 3); pstmt.setString(6, "c"); System.err.println("3 new rows : Impacted rows: " + (pstmt.executeUpdate())); pstmt = connection.prepareStatement("INSERT INTO test_onduplicate(id, foo) VALUES (?,?), (?,?), (?,?) ON DUPLICATE KEY UPDATE foo=values(foo)"); pstmt.setInt(1, 1); pstmt.setString(2, "a"); pstmt.setInt(3, 2); pstmt.setString(4, "d"); pstmt.setInt(5, 4); pstmt.setString(6, "e"); System.err.println("1 new row, 1 updated, 1 untouched : Impacted rows: " + (pstmt.executeUpdate())); pstmt = connection.prepareStatement("INSERT INTO test_onduplicate(id, foo) VALUES (?,?), (?,?), (?,?) ON DUPLICATE KEY UPDATE foo=values(foo)"); pstmt.setInt(1, 1); pstmt.setString(2, "g"); pstmt.setInt(3, 2); pstmt.setString(4, "h"); pstmt.setInt(5, 3); pstmt.setString(6, "i"); System.err.println("3 updated rows : Impacted rows: " + (pstmt.executeUpdate())); pstmt = connection.prepareStatement("DROP TABLE test_onduplicate"); pstmt.execute(); } catch(Exception e) { e.printStackTrace(); } } }