@@ -33,9 +33,7 @@ public void setUp()
3333 c .createStatement ().execute ("drop table if exists test_prepare_time" );
3434 c .createStatement ().execute ("drop table if exists objects_test1" );
3535 c .createStatement ().execute ("drop table if exists binary1" );
36- c .createStatement ().execute ("drop table if exists test_prepare_statement_null" );
3736 c .createStatement ().execute ("create table test_prepare_statement (a int, b string)" );
38- c .createStatement ().execute ("create table test_prepare_statement_null (a int, b string)" );
3937 c .createStatement ().execute ("create table test_prepare_time(a DATE, b TIMESTAMP)" );
4038 // json data
4139 c .createStatement ().execute (
@@ -48,58 +46,66 @@ public void setUp()
4846 public void TestBatchInsert () throws SQLException {
4947 Connection c = Utils .createConnection ();
5048 c .setAutoCommit (false );
49+ Statement s = c .createStatement ();
50+ s .execute ("use test_prepare_statement" );
51+ s .execute ("create or replace table batch_insert (a int, b string)" );
5152
52- PreparedStatement ps = c .prepareStatement ("insert into test_prepare_statement values" );
53- ps .setInt (1 , 1 );
54- ps .setString (2 , "a" );
55- ps .addBatch ();
56- ps .setInt (1 , 2 );
57- ps .setString (2 , "b" );
58- ps .addBatch ();
59- System .out .println ("execute batch insert" );
53+ int [] c1 = {1 , 2 };
54+ String [] c2 = {"a" , "b" };
55+
56+ PreparedStatement ps = c .prepareStatement ("insert into batch_insert values" );
57+ for (int i = 0 ; i < c1 .length ; i ++) {
58+ ps .setInt (1 , c1 [i ]);
59+ ps .setString (2 , c2 [i ]);
60+ ps .addBatch ();
61+ }
6062 int [] ans = ps .executeBatch ();
61- Assert .assertEquals (ans .length , 2 );
62- Assert .assertEquals (ans [0 ], 1 );
63- Assert .assertEquals (ans [1 ], 1 );
64- Statement statement = c .createStatement ();
63+ Assert .assertEquals (ans , new int [] {1 , 1 });
6564
66- System .out .println ("execute select" );
67- statement .execute ("SELECT * from test_prepare_statement" );
68- ResultSet r = statement .getResultSet ();
65+ s .execute ("SELECT * from batch_insert" );
66+ ResultSet r = s .getResultSet ();
6967
70- while (r .next ()) {
71- System .out .println (r .getInt (1 ));
72- System .out .println (r .getString (2 ));
68+ for (int i = 0 ; i < c1 .length ; i ++) {
69+ Assert .assertTrue (r .next ());
70+ Assert .assertEquals (r .getInt (1 ), c1 [i ]);
71+ Assert .assertEquals (r .getString (2 ), c2 [i ]);
7372 }
73+ Assert .assertFalse (r .next ());
7474 }
7575
7676 @ Test (groups = "IT" )
7777 public void TestBatchInsertWithNULL () throws SQLException {
7878 Connection c = Utils .createConnection ();
7979 c .setAutoCommit (false );
80+ Statement s = c .createStatement ();
81+ s .execute ("use test_prepare_statement" );
82+ s .execute ("create or replace table batch_insert_null (a int, b string)" );
83+
84+
85+ PreparedStatement ps = c .prepareStatement ("insert into batch_insert_null values" );
8086
81- PreparedStatement ps = c .prepareStatement ("insert into test_prepare_statement_null values" );
8287 ps .setInt (1 , 1 );
8388 ps .setNull (2 , Types .NULL );
8489 ps .addBatch ();
90+
8591 ps .setInt (1 , 2 );
8692 ps .setObject (2 , null , Types .NULL );
8793 ps .addBatch ();
88- System . out . println ( "execute batch insert" );
94+
8995 int [] ans = ps .executeBatch ();
90- Assert .assertEquals (ans .length , 2 );
91- Assert .assertEquals (ans [0 ], 1 );
92- Assert .assertEquals (ans [1 ], 1 );
93- Statement statement = c .createStatement ();
96+ Assert .assertEquals (ans , new int [] {1 , 1 });
9497
95- System . out . println ( "execute select" );
96- statement .execute ("SELECT * from test_prepare_statement_null " );
98+ Statement statement = c . createStatement ( );
99+ statement .execute ("SELECT * from batch_insert_null " );
97100 ResultSet r = statement .getResultSet ();
98101
99- while (r .next ()) {
100- System .out .println (r .getInt (1 ));
101- Assert .assertEquals (r .getObject (2 ), null );
102+ int [] c1 = {1 , 2 };
103+ for (int j : c1 ) {
104+ Assert .assertTrue (r .next ());
105+ Assert .assertEquals (r .getInt (1 ), j );
106+ Assert .assertNull (r .getString (2 ));
102107 }
108+ Assert .assertFalse (r .next ());
103109 }
104110
105111 @ Test (groups = "IT" )
@@ -131,30 +137,29 @@ public void TestConvertSQLWithBatchValues() throws SQLException {
131137 @ Test (groups = "IT" )
132138 public void TestBatchDelete () throws SQLException {
133139 try ( Connection c = Utils .createConnection ();
134- Statement statement = c .createStatement ();
140+ Statement statement = c .createStatement ()
135141 ) {
136142 c .setAutoCommit (false );
137143 c .createStatement ().execute ("create or replace table test_batch_delete(a int, b string)" );
144+
145+ int [] c1 = {1 , 3 };
146+ String [] c2 = {"b" , "b" };
147+
138148 PreparedStatement ps = c .prepareStatement ("insert into test_batch_delete values" );
139- ps .setInt (1 , 1 );
140- ps .setString (2 , "b" );
141- ps .addBatch ();
142- ps .setInt (1 , 3 );
143- ps .setString (2 , "b" );
144- ps .addBatch ();
145- System .out .println ("execute batch insert" );
146- int [] ans = ps .executeBatch ();
147- Assert .assertEquals (ans .length , 2 );
148- Assert .assertEquals (ans [0 ], 1 );
149- Assert .assertEquals (ans [1 ], 1 );
149+ for (int i = 0 ; i < c1 .length ; i ++) {
150+ ps .setInt (1 , c1 [i ]);
151+ ps .setString (2 , c2 [i ]);
152+ ps .addBatch ();
153+ }
154+ Assert .assertEquals (ps .executeBatch (), new int [] {1 , 1 });
150155
151- System .out .println ("execute select" );
152156 statement .execute ("SELECT * from test_batch_delete" );
153157 ResultSet r = statement .getResultSet ();
154158
155- while (r .next ()) {
156- System .out .println (r .getInt (1 ));
157- System .out .println (r .getString (2 ));
159+ for (int i = 0 ; i < c1 .length ; i ++) {
160+ Assert .assertTrue (r .next ());
161+ Assert .assertEquals (r .getInt (1 ), c1 [i ]);
162+ Assert .assertEquals (r .getString (2 ), c2 [i ]);
158163 }
159164
160165 PreparedStatement deletePs = c .prepareStatement ("delete from test_batch_delete where a = ?" );
@@ -180,27 +185,30 @@ public void TestBatchDelete() throws SQLException {
180185 @ Test (groups = "IT" )
181186 public void TestBatchInsertWithTime () throws SQLException {
182187 Connection c = Utils .createConnection ();
188+ Statement s = c .createStatement ();
189+ s .execute ("create or replace table test_prepare_time(a DATE, b TIMESTAMP)" );
183190 c .setAutoCommit (false );
191+
192+ java .sql .Date [] c1 = {Date .valueOf ("2020-01-10" ), Date .valueOf ("1970-01-01" ), Date .valueOf ("2021-01-01" )};
193+ Timestamp [] c2 = {Timestamp .valueOf ("1983-07-12 21:30:55.888" ), Timestamp .valueOf ("1970-01-01 00:00:01" ), Timestamp .valueOf ("1970-01-01 00:00:01.234" )};
194+
184195 PreparedStatement ps = c .prepareStatement ("insert into test_prepare_time values" );
185- ps .setDate (1 , Date .valueOf ("2020-01-10" ));
186- ps .setTimestamp (2 , Timestamp .valueOf ("1983-07-12 21:30:55.888" ));
187- ps .addBatch ();
188- ps .setDate (1 , Date .valueOf ("1970-01-01" ));
189- ps .setTimestamp (2 , Timestamp .valueOf ("1970-01-01 00:00:01" ));
190- ps .addBatch ();
191- ps .setDate (1 , Date .valueOf ("2021-01-01" ));
192- ps .setTimestamp (2 , Timestamp .valueOf ("1970-01-01 00:00:01.234" ));
193- int [] ans = ps .executeBatch ();
194- Statement statement = c .createStatement ();
196+ for (int i = 0 ; i < c1 .length ; i ++) {
197+ ps .setDate (1 , c1 [i ]);
198+ ps .setTimestamp (2 , c2 [i ]);
199+ ps .addBatch ();
200+ }
201+ Assert .assertEquals (ps .executeBatch (), new int [] {1 , 1 , 1 });
195202
196- System .out .println ("execute select on time" );
197- statement .execute ("SELECT * from test_prepare_time" );
198- ResultSet r = statement .getResultSet ();
203+ s .execute ("SELECT * from test_prepare_time" );
204+ ResultSet r = s .getResultSet ();
199205
200- while (r .next ()) {
201- System .out .println (r .getDate (1 ).toString ());
202- System .out .println (r .getTimestamp (2 ).toString ());
206+ for (int i = 0 ; i < c1 .length ; i ++) {
207+ Assert .assertTrue (r .next ());
208+ Assert .assertEquals (r .getDate (1 ), c1 [i ]);
209+ Assert .assertEquals (r .getTimestamp (2 ), c2 [i ]);
203210 }
211+ Assert .assertFalse (r .next ());
204212 }
205213
206214 @ DataProvider (name = "complexDataType" )
@@ -655,5 +663,4 @@ public void testInsertWithSelect() throws SQLException {
655663 Assert .assertEquals (3 , count , "should have 3 rows in the table after insert with select" );
656664 conn .close ();
657665 }
658-
659666}
0 commit comments