@@ -6,7 +6,7 @@ author: saimicrosoft
6
6
ms.service : postgresql
7
7
ms.subservice : hyperscale-citus
8
8
ms.topic : how-to
9
- ms.date : 06/20 /2022
9
+ ms.date : 07/26 /2022
10
10
---
11
11
12
12
# Java app to connect and query Hyperscale (Citus)
@@ -173,37 +173,35 @@ import javax.sql.DataSource;
173
173
import com.zaxxer.hikari.HikariDataSource ;
174
174
175
175
public class DButil {
176
- private static final String DB_USERNAME = " db.username" ;
177
- private static final String DB_PASSWORD = " db.password" ;
178
- private static final String DB_URL = " db.url" ;
179
- private static final String DB_DRIVER_CLASS = " driver.class.name" ;
180
- private static Properties properties = null ;
181
- private static HikariDataSource datasource;
182
-
183
- static {
184
- try {
185
- properties = new Properties ();
186
- properties .load(new FileInputStream (" src/main/java/application.properties" ));
187
- datasource = new HikariDataSource ();
188
- datasource. setDriverClassName(properties . getProperty( DB_DRIVER_CLASS ) );
189
- datasource. setJdbcUrl (properties. getProperty(DB_URL ));
190
- datasource. setUsername (properties. getProperty(DB_USERNAME ));
191
- datasource. setPassword (properties. getProperty(DB_PASSWORD ));
192
- datasource. setMinimumIdle( 100 );
193
- datasource. setMaximumPoolSize( 1000000000 );
194
- datasource. setAutoCommit( false );
195
- datasource. setLoginTimeout( 3 );
196
-
197
-
198
- } catch ( IOException | SQLException e) {
199
- e . printStackTrace();
176
+ private static final String DB_USERNAME = " db.username" ;
177
+ private static final String DB_PASSWORD = " db.password" ;
178
+ private static final String DB_URL = " db.url" ;
179
+ private static final String DB_DRIVER_CLASS = " driver.class.name" ;
180
+ private static Properties properties = null ;
181
+ private static HikariDataSource datasource;
182
+
183
+ static {
184
+ try {
185
+ properties = new Properties ();
186
+ properties . load(new FileInputStream (" src/main/java/application.properties" ));
187
+
188
+ datasource = new HikariDataSource ( );
189
+ datasource. setDriverClassName (properties. getProperty(DB_DRIVER_CLASS ));
190
+ datasource. setJdbcUrl (properties. getProperty(DB_URL ));
191
+ datasource. setUsername (properties. getProperty(DB_USERNAME ));
192
+ datasource. setPassword(properties . getProperty( DB_PASSWORD ) );
193
+ datasource. setMinimumIdle( 100 );
194
+ datasource. setMaximumPoolSize( 1000000000 );
195
+ datasource. setAutoCommit( false );
196
+ datasource . setLoginTimeout( 3 );
197
+ } catch ( IOException | SQLException e) {
198
+ e . printStackTrace();
199
+ }
200
200
}
201
-
202
- }
203
- public static DataSource getDataSource () {
204
- return datasource;
205
- }
201
+ public static DataSource getDataSource () {
202
+ return datasource;
206
203
}
204
+ }
207
205
```
208
206
209
207
Create a ` src/main/java/DemoApplication.java ` file that contains:
@@ -335,12 +333,12 @@ public class Pharmacy {
335
333
@Override
336
334
public String toString () {
337
335
return " TPharmacy{" +
338
- " pharmacy_id=" + pharmacy_id +
339
- " , pharmacy_name='" + pharmacy_name + ' \' ' +
340
- " , city='" + city + ' \' ' +
341
- " , state='" + state + ' \' ' +
342
- " , zip_code='" + zip_code + ' \' ' +
343
- ' }' ;
336
+ " pharmacy_id=" + pharmacy_id +
337
+ " , pharmacy_name='" + pharmacy_name + ' \' ' +
338
+ " , city='" + city + ' \' ' +
339
+ " , state='" + state + ' \' ' +
340
+ " , zip_code='" + zip_code + ' \' ' +
341
+ ' }' ;
344
342
}
345
343
}
346
344
```
@@ -534,15 +532,16 @@ The following code is an example for copying data from a CSV file to a database
534
532
It requires the file [ pharmacies.csv] ( https://download.microsoft.com/download/d/8/d/d8d5673e-7cbf-4e13-b3e9-047b05fc1d46/pharmacies.csv ) .
535
533
536
534
``` java
537
- public static long copyFromFile(Connection connection, String filePath, String tableName)
538
- throws SQLException , IOException {
535
+ public static long
536
+ copyFromFile(Connection connection, String filePath, String tableName)
537
+ throws SQLException , IOException {
539
538
long count = 0 ;
540
539
FileInputStream fileInputStream = null ;
541
540
542
541
try {
543
542
Connection unwrap = connection. unwrap(Connection . class);
544
543
BaseConnection connSec = (BaseConnection ) unwrap;
545
-
544
+
546
545
CopyManager copyManager = new CopyManager ((BaseConnection ) connSec);
547
546
fileInputStream = new FileInputStream (filePath);
548
547
count = copyManager. copyIn(" COPY " + tableName + " FROM STDIN delimiter ',' csv" , fileInputStream);
@@ -594,24 +593,23 @@ The following code is an example for copying in-memory data to table.
594
593
595
594
``` java
596
595
private static void inMemory(Connection connection) throws SQLException ,IOException {
597
- log. info(" Copying inmemory data into table" );
596
+ log. info(" Copying in-memory data into table" );
598
597
String [] input = {" 0,Target,Sunnyvale,California,94001" };
599
-
598
+
600
599
Connection unwrap = connection. unwrap(Connection . class);
601
600
BaseConnection connSec = (BaseConnection ) unwrap;
602
-
601
+
603
602
CopyManager copyManager = new CopyManager ((BaseConnection ) connSec);
604
- String copyCommand = " COPY pharmacy FROM STDIN with csv " ;
605
-
606
- for (String var : input)
607
- {
603
+ String copyCommand = " COPY pharmacy FROM STDIN with csv" ;
604
+
605
+ for (String var : input)
606
+ {
608
607
Reader reader = new StringReader (var );
609
608
copyManager. copyIn(copyCommand, reader);
610
609
}
611
610
612
611
copyManager. copyIn(copyCommand);
613
-
614
- }
612
+ }
615
613
```
616
614
617
615
You can now add the following line in the main method:
0 commit comments