title: java jdbc duplicate entry for key primary on insert foreign key
body: <p>The "duplicate entry for key 'primary'" seems to be a popular problem on the internet, even though I did not manage to find a post related to my problem. I'm trying to insert values into a table using java jdbc, but somehow I get this 'primary' key problem. </p>
  
  <p>The relevant code is this: </p>
  
  <pre><code>preparedStatement = connect
                .prepareStatement("insert into database1.table2 values (default, ?, ?));
            preparedStatement.setString(1, randomString(12)); // Inserts a normal String, tested &amp; working
            insert_plz(plz); // Inserts a given value as a primary key into table1
            preparedStatement.setInt(2, plz); // Inserts that value as a foreign key 
            preparedStatement.executeUpdate(); // Exception is thrown here
  </code></pre>
  
  <p>Calling function <code>insert_plz(plz)</code> works without any problems, I checked the database. </p>
  
  <p>This is how I created table2:</p>
  
  <pre><code>create table table2 (
  standorte_schluessel int unsigned auto_increment,
  name varchar (20) not null,    
  postleitzahl int unsigned,
  primary key (standorte_schluessel),
  foreign key (postleitzahl) references plz (postleitzahl) on delete cascade
  );
  </code></pre>
  
  <p>Now my question is, how come that on trying to insert a foreign key, it gives me a primary key error? </p>
  
  <p>Also, I'm kind of new to mysql (not to java though) so the answer to my question might be obvious (unfortunately not to me). </p>
  
  <p>This is the error log:</p>
  
  <pre><code>Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '74141' for key 'PRIMARY'
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
  at com.mysql.jdbc.Util.getInstance(Util.java:383)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
  at Connect.insert_standort(Connect.java:113)
  at Connect.readDataBase(Connect.java:41)
  at Main.main(Main.java:5)
  </code></pre>