@@ -641,4 +641,47 @@ public void testExecuteUpdate() throws SQLException {
641
641
conn .createStatement ().execute ("delete from test_prepare_statement" );
642
642
}
643
643
644
+ @ Test
645
+
646
+ public void testInsertWithSelect () throws SQLException {
647
+ Connection conn = Utils .createConnection ();
648
+ conn .createStatement ().execute ("delete from test_prepare_statement" );
649
+
650
+ String insertSql = "insert into test_prepare_statement select a, b from test_prepare_statement where b = ?" ;
651
+ try (PreparedStatement statement = conn .prepareStatement (insertSql )) {
652
+ statement .setString (1 , "a" );
653
+ int insertedRows = statement .executeUpdate ();
654
+ Assertions .assertEquals (0 , insertedRows , "should not insert any rows as the table is empty" );
655
+ }
656
+
657
+ // Insert some data
658
+ String insertDataSql = "insert into test_prepare_statement values (?,?)" ;
659
+ try (PreparedStatement statement = conn .prepareStatement (insertDataSql )) {
660
+ statement .setInt (1 , 1 );
661
+ statement .setString (2 , "a" );
662
+ statement .executeUpdate ();
663
+
664
+ statement .setInt (1 , 2 );
665
+ statement .setString (2 , "b" );
666
+ statement .executeUpdate ();
667
+ }
668
+
669
+ // Now try to insert again with select
670
+ try (PreparedStatement statement = conn .prepareStatement (insertSql )) {
671
+ statement .setString (1 , "a" );
672
+ int insertedRows = statement .executeUpdate ();
673
+ Assertions .assertEquals (1 , insertedRows , "should insert two rows from the select" );
674
+ }
675
+
676
+ ResultSet rs = conn .createStatement ().executeQuery ("select * from test_prepare_statement order by a" );
677
+ int count = 0 ;
678
+ while (rs .next ()) {
679
+ count ++;
680
+ }
681
+ Assertions .assertEquals (3 , count , "should have four rows in the table after insert with select" );
682
+
683
+ // Clean up
684
+ conn .createStatement ().execute ("delete from test_prepare_statement" );
685
+ }
686
+
644
687
}
0 commit comments