Skip to content
Snippets Groups Projects
Commit 3a93fee8 authored by Дмитрий Кольцов's avatar Дмитрий Кольцов
Browse files

add example for PG driver usage

parent fe064432
No related branches found
No related tags found
1 merge request!1add example for PG driver usage
**/target/
\ No newline at end of file
File added
## How to run
1. Install picodata
2. Run picodata with the following command:
```bash
picodata run --config picodata_config.yaml
```
3. Prepare data schema for example app with:
```bash
cat before.sql | picodata admin ./admin.sock
```
4. Run example app with
```bash
mvn compile && mvn exec:java -Dexec.mainClass="com.example.PostgresExample"
```
\ No newline at end of file
CREATE TABLE "warehouse" (
id INTEGER NOT NULL,
item TEXT NOT NULL,
PRIMARY KEY (id))
USING memtx DISTRIBUTED BY (id)
OPTION (TIMEOUT = 3.0);
CREATE USER "randy" WITH PASSWORD 'P@ssw0rd' USING md5 OPTION (TIMEOUT = 3.0);
GRANT WRITE ON TABLE "warehouse" to "randy";
GRANT READ ON TABLE "warehouse" to "randy";
\ No newline at end of file
cluster:
cluster_id: demo
tier:
default:
replication_factor: 1
can_vote: true
default_replication_factor: 1
instance:
data_dir: .
tier: default
peer:
- 127.0.0.1:3301
listen: 127.0.0.1:3301
advertise_address: 127.0.0.1:3301
http_listen: 127.0.0.1:8081
admin_socket: ./admin.sock
plugin_dir: null
audit: null
shredding: false
log:
level: info
destination: null
format: plain
pg:
listen: 127.0.0.1:5432
ssl: false
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>postgres-java-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>postgres-java-example</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>21</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class PostgresExample {
public static void main(String[] args) {
var props = new Properties();
props.setProperty("user", "randy");
props.setProperty("password", "P@ssw0rd");
// props.setProperty("dbname", "postgres");
var connstr = "jdbc:postgresql://localhost:5432/";
try (Connection conn = DriverManager.getConnection(connstr, props)) {
System.out.println("Connected to the PostgreSQL server successfully.");
var DELETE_QUERY = """
DELETE FROM "warehouse";
""";
var stmt = conn.prepareStatement(DELETE_QUERY);
var deleteRows = stmt.executeUpdate();
System.out.println(String.format("%d rows was deleted", deleteRows));
var INSERT_QUERY = """
INSERT INTO \"warehouse\" VALUES (?, ?)
""";
stmt = conn.prepareStatement(INSERT_QUERY);
stmt.setInt(1, 1);
stmt.setString(2, "Dima");
var insertedRows = stmt.executeUpdate();
System.out.println(String.format("%d rows was inserted", insertedRows));
var SELECT_QUERY = """
SELECT * FROM "warehouse" WHERE id = ?;
""";
stmt = conn.prepareStatement(SELECT_QUERY);
stmt.setInt(1, 1);
var res = stmt.executeQuery();
while (res.next()) {
System.out.println(
String.format(
"Id is %d, name is %s",
res.getInt(1),
res.getString(2)
)
);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.example;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment