@@ -27,79 +27,75 @@ public void setUp()
2727 Connection c = Utils .createConnection ();
2828 System .out .println ("-----------------" );
2929 c .createStatement ().execute ("create database if not exists test_prepare_statement" );
30- System .out .println ("drop all existing test table" );
3130
3231 c .createStatement ().execute ("drop table if exists test_prepare_statement" );
33- c .createStatement ().execute ("drop table if exists test_prepare_time" );
34- c .createStatement ().execute ("drop table if exists objects_test1" );
35- c .createStatement ().execute ("drop table if exists binary1" );
36- c .createStatement ().execute ("drop table if exists test_prepare_statement_null" );
3732 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)" );
39- c .createStatement ().execute ("create table test_prepare_time(a DATE, b TIMESTAMP)" );
40- // json data
41- c .createStatement ().execute (
42- "CREATE TABLE IF NOT EXISTS objects_test1(id TINYINT, obj VARIANT, d TIMESTAMP, s String, arr ARRAY(INT64)) Engine = Fuse" );
43- // Binary data
44- c .createStatement ().execute ("create table IF NOT EXISTS binary1 (a binary);" );
4533 }
4634
4735 @ Test (groups = "IT" )
4836 public void TestBatchInsert () throws SQLException {
4937 Connection c = Utils .createConnection ();
5038 c .setAutoCommit (false );
39+ Statement s = c .createStatement ();
40+ s .execute ("use test_prepare_statement" );
41+ s .execute ("create or replace table batch_insert (a int, b string)" );
5142
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" );
43+ int [] c1 = {1 , 2 };
44+ String [] c2 = {"a" , "b" };
45+
46+ PreparedStatement ps = c .prepareStatement ("insert into batch_insert values" );
47+ for (int i = 0 ; i < c1 .length ; i ++) {
48+ ps .setInt (1 , c1 [i ]);
49+ ps .setString (2 , c2 [i ]);
50+ ps .addBatch ();
51+ }
6052 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 ();
53+ Assert .assertEquals (ans , new int [] {1 , 1 });
6554
66- System .out .println ("execute select" );
67- statement .execute ("SELECT * from test_prepare_statement" );
68- ResultSet r = statement .getResultSet ();
55+ s .execute ("SELECT * from batch_insert" );
56+ ResultSet r = s .getResultSet ();
6957
70- while (r .next ()) {
71- System .out .println (r .getInt (1 ));
72- System .out .println (r .getString (2 ));
58+ for (int i = 0 ; i < c1 .length ; i ++) {
59+ Assert .assertTrue (r .next ());
60+ Assert .assertEquals (r .getInt (1 ), c1 [i ]);
61+ Assert .assertEquals (r .getString (2 ), c2 [i ]);
7362 }
63+ Assert .assertFalse (r .next ());
7464 }
7565
7666 @ Test (groups = "IT" )
7767 public void TestBatchInsertWithNULL () throws SQLException {
7868 Connection c = Utils .createConnection ();
7969 c .setAutoCommit (false );
70+ Statement s = c .createStatement ();
71+ s .execute ("use test_prepare_statement" );
72+ s .execute ("create or replace table batch_insert_null (a int, b string)" );
73+
74+
75+ PreparedStatement ps = c .prepareStatement ("insert into batch_insert_null values" );
8076
81- PreparedStatement ps = c .prepareStatement ("insert into test_prepare_statement_null values" );
8277 ps .setInt (1 , 1 );
8378 ps .setNull (2 , Types .NULL );
8479 ps .addBatch ();
80+
8581 ps .setInt (1 , 2 );
8682 ps .setObject (2 , null , Types .NULL );
8783 ps .addBatch ();
88- System . out . println ( "execute batch insert" );
84+
8985 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 ();
86+ Assert .assertEquals (ans , new int [] {1 , 1 });
9487
95- System . out . println ( "execute select" );
96- statement .execute ("SELECT * from test_prepare_statement_null " );
88+ Statement statement = c . createStatement ( );
89+ statement .execute ("SELECT * from batch_insert_null " );
9790 ResultSet r = statement .getResultSet ();
9891
99- while (r .next ()) {
100- System .out .println (r .getInt (1 ));
101- Assert .assertEquals (r .getObject (2 ), null );
92+ int [] c1 = {1 , 2 };
93+ for (int j : c1 ) {
94+ Assert .assertTrue (r .next ());
95+ Assert .assertEquals (r .getInt (1 ), j );
96+ Assert .assertNull (r .getString (2 ));
10297 }
98+ Assert .assertFalse (r .next ());
10399 }
104100
105101 @ Test (groups = "IT" )
@@ -131,30 +127,29 @@ public void TestConvertSQLWithBatchValues() throws SQLException {
131127 @ Test (groups = "IT" )
132128 public void TestBatchDelete () throws SQLException {
133129 try ( Connection c = Utils .createConnection ();
134- Statement statement = c .createStatement ();
130+ Statement statement = c .createStatement ()
135131 ) {
136132 c .setAutoCommit (false );
137133 c .createStatement ().execute ("create or replace table test_batch_delete(a int, b string)" );
134+
135+ int [] c1 = {1 , 3 };
136+ String [] c2 = {"b" , "b" };
137+
138138 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 );
139+ for (int i = 0 ; i < c1 .length ; i ++) {
140+ ps .setInt (1 , c1 [i ]);
141+ ps .setString (2 , c2 [i ]);
142+ ps .addBatch ();
143+ }
144+ Assert .assertEquals (ps .executeBatch (), new int [] {1 , 1 });
150145
151- System .out .println ("execute select" );
152146 statement .execute ("SELECT * from test_batch_delete" );
153147 ResultSet r = statement .getResultSet ();
154148
155- while (r .next ()) {
156- System .out .println (r .getInt (1 ));
157- System .out .println (r .getString (2 ));
149+ for (int i = 0 ; i < c1 .length ; i ++) {
150+ Assert .assertTrue (r .next ());
151+ Assert .assertEquals (r .getInt (1 ), c1 [i ]);
152+ Assert .assertEquals (r .getString (2 ), c2 [i ]);
158153 }
159154
160155 PreparedStatement deletePs = c .prepareStatement ("delete from test_batch_delete where a = ?" );
@@ -180,27 +175,30 @@ public void TestBatchDelete() throws SQLException {
180175 @ Test (groups = "IT" )
181176 public void TestBatchInsertWithTime () throws SQLException {
182177 Connection c = Utils .createConnection ();
178+ Statement s = c .createStatement ();
179+ s .execute ("create or replace table test_prepare_time(a DATE, b TIMESTAMP)" );
183180 c .setAutoCommit (false );
181+
182+ java .sql .Date [] c1 = {Date .valueOf ("2020-01-10" ), Date .valueOf ("1970-01-01" ), Date .valueOf ("2021-01-01" )};
183+ 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" )};
184+
184185 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 ();
186+ for (int i = 0 ; i < c1 .length ; i ++) {
187+ ps .setDate (1 , c1 [i ]);
188+ ps .setTimestamp (2 , c2 [i ]);
189+ ps .addBatch ();
190+ }
191+ Assert .assertEquals (ps .executeBatch (), new int [] {1 , 1 , 1 });
195192
196- System .out .println ("execute select on time" );
197- statement .execute ("SELECT * from test_prepare_time" );
198- ResultSet r = statement .getResultSet ();
193+ s .execute ("SELECT * from test_prepare_time" );
194+ ResultSet r = s .getResultSet ();
199195
200- while (r .next ()) {
201- System .out .println (r .getDate (1 ).toString ());
202- System .out .println (r .getTimestamp (2 ).toString ());
196+ for (int i = 0 ; i < c1 .length ; i ++) {
197+ Assert .assertTrue (r .next ());
198+ Assert .assertEquals (r .getDate (1 ), c1 [i ]);
199+ Assert .assertEquals (r .getTimestamp (2 ), c2 [i ]);
203200 }
201+ Assert .assertFalse (r .next ());
204202 }
205203
206204 @ DataProvider (name = "complexDataType" )
@@ -655,5 +653,4 @@ public void testInsertWithSelect() throws SQLException {
655653 Assert .assertEquals (3 , count , "should have 3 rows in the table after insert with select" );
656654 conn .close ();
657655 }
658-
659656}
0 commit comments