Skip to content

Commit 8afae6b

Browse files
committed
Update code examples README.md
1 parent b6fa7d5 commit 8afae6b

File tree

1 file changed

+95
-50
lines changed

1 file changed

+95
-50
lines changed

README.md

Lines changed: 95 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,16 @@ dependencies {
6060
Default plugin `config.yml` settings.
6161

6262
```yaml
63-
# sqlite directory name for anonymous databases
64-
# set empty, for creating database file in plugin's folder by default
65-
sqlite-directory: "database"
66-
67-
# default mysql connection properties
68-
mysql-properties: "useSSL=false&autoReconnect=true&useUnicode=true&serverTimezone=UTC"
69-
7063
# Hikari connection pool settings (https://github.com/brettwooldridge/HikariCP)
7164
hikari:
72-
auto-commit: true
73-
connection-timeout: 30000
74-
idle-timeout: 600000
75-
keepalive-time: 0
76-
max-lifetime: 1800000
77-
maximum-pool-size: 10
65+
min-idle: 10 # Minimum number of idle connections in the pool
66+
maximum-pool-size: 10 # Maximum number of connections in the pool
67+
max-lifetime: 1800000 # Maximum lifetime of a connection in milliseconds (30 minutes)
68+
connection-timeout: 30000 # Maximum time to wait for a connection in milliseconds (30 seconds)
69+
validation-timeout: 5000 # Timeout for connection validation in milliseconds (5 seconds)
70+
idle-timeout: 600000 # Maximum idle time for a connection in milliseconds (10 minutes)
71+
auto-commit: true # Should connections auto-commit by default
72+
keepalive-time: 0 # Keepalive time in milliseconds (0 = disabled)
7873
```
7974
8075
## How to use
@@ -84,22 +79,62 @@ Firstly we recommend to read:
8479
- [*Jdbi Developer Guide*](http://jdbi.org/)
8580
- [*HikariCP Configuration*](https://github.com/brettwooldridge/HikariCP#gear-configuration-knobs-baby)
8681
87-
Very basic example of MySQL database class:
82+
Basic example of MySQLDatabase class:
8883
8984
```java
9085
import me.hteppl.data.database.MySQLDatabase;
91-
import org.jdbi.v3.core.Handle;
86+
import org.sql2o.Connection;
9287

9388
public class MyDatabase extends MySQLDatabase {
9489

9590
public MyDatabase() {
96-
super("host", "database", "user", "password");
91+
super("localhost", "mydb", "user", "password"); // defaults to port 3306
92+
93+
// Example: create table and insert data safely
94+
try (Connection con = this.openConnection()) {
95+
// Create table
96+
con.createQuery("""
97+
CREATE TABLE IF NOT EXISTS users (
98+
id SERIAL PRIMARY KEY,
99+
name VARCHAR(100) NOT NULL,
100+
email VARCHAR(100) UNIQUE NOT NULL
101+
)
102+
""").executeUpdate();
103+
104+
// Insert data with parameters (safe)
105+
con.createQuery("INSERT INTO users (name, email) VALUES (:name, :email)")
106+
.addParameter("name", "Alice")
107+
.addParameter("email", "alice@example.com")
108+
.executeUpdate();
109+
}
110+
}
111+
}
112+
```
113+
114+
Basic example of PostgreSQLDatabase class:
97115

98-
try (Handle handle = this.getHandle()) {
99-
handle.createUpdate("...")
100-
.bind("var1", "data1")
101-
.bind("var2", "data2")
102-
.execute();
116+
```java
117+
import me.hteppl.data.database.PostgreSQLDatabase;
118+
import org.sql2o.Connection;
119+
120+
public class MyPostgresDatabase extends PostgreSQLDatabase {
121+
122+
public MyPostgresDatabase() {
123+
super("localhost", "mydb", "user", "password"); // default port 5432
124+
125+
try (Connection con = this.openConnection()) {
126+
con.createQuery("""
127+
CREATE TABLE IF NOT EXISTS products (
128+
id SERIAL PRIMARY KEY,
129+
name VARCHAR(100),
130+
price NUMERIC(10,2)
131+
)
132+
""").executeUpdate();
133+
134+
con.createQuery("INSERT INTO products (name, price) VALUES (:name, :price)")
135+
.addParameter("name", "Sword")
136+
.addParameter("price", 29.99)
137+
.executeUpdate();
103138
}
104139
}
105140
}
@@ -109,43 +144,54 @@ Example of SQLite database class:
109144

110145
```java
111146
import me.hteppl.data.database.SQLiteDatabase;
112-
import org.jdbi.v3.core.Handle;
113-
114-
public class MyDatabase extends SQLiteDatabase {
115-
116-
public MyDatabase() {
117-
super("database");
118-
119-
try (Handle handle = this.getHandle()) {
120-
handle.createUpdate("...")
121-
.bind("var1", "data1")
122-
.bind("var2", "data2")
123-
.execute();
147+
import org.sql2o.Connection;
148+
149+
public class MySQLiteDatabase extends SQLiteDatabase {
150+
151+
public MySQLiteDatabase() {
152+
super("data/mydb"); // folder + filename
153+
154+
try (Connection con = this.openConnection()) {
155+
con.createQuery("""
156+
CREATE TABLE IF NOT EXISTS items (
157+
id INTEGER PRIMARY KEY AUTOINCREMENT,
158+
name TEXT NOT NULL,
159+
quantity INTEGER NOT NULL
160+
)
161+
""").executeUpdate();
162+
163+
con.createQuery("INSERT INTO items (name, quantity) VALUES (:name, :quantity)")
164+
.addParameter("name", "Diamond")
165+
.addParameter("quantity", 5)
166+
.executeUpdate();
124167
}
125168
}
126169
}
127170
```
128171

129-
After that, you can easily do what you want with your [*Jdbi*](http://jdbi.org/) handles:
172+
Using transactions:
130173

131174
```java
132-
/* import your database class */
133-
134-
public class Main {
135-
136-
public static void main(String[] args) {
137-
MyDatabase database = new MyDatabase();
138-
139-
try (Handle handle = database.getHandle()) {
140-
handle.createUpdate("...")
141-
.bind("var1", "data1")
142-
.bind("var2", "data2")
143-
.execute();
144-
}
145-
}
175+
try (Connection tx = database.beginTransaction()) {
176+
tx.createQuery("INSERT INTO users (name, email) VALUES (:name, :email)")
177+
.addParameter("name", "Bob")
178+
.addParameter("email", "bob@example.com")
179+
.executeUpdate();
180+
181+
tx.createQuery("UPDATE users SET email = :email WHERE name = :name")
182+
.addParameter("name", "Alice")
183+
.addParameter("email", "alice_new@example.com")
184+
.executeUpdate();
185+
186+
tx.commit(); // commit transaction
146187
}
147188
```
148189

190+
- Always use parameter binding (:param) to avoid SQL injection.
191+
- Prefer try-with-resources so connections are closed automatically.
192+
- Transactions can be used with beginTransaction() to group multiple queries.
193+
- Hikari settings are configured via YAML (config.yml) and applied automatically via your Database classes.
194+
149195
## Libraries
150196

151197
[**PowerNukkitX**](https://github.com/PowerNukkitX/PowerNukkitX) is a branch version based on PowerNukkit,
@@ -154,8 +200,7 @@ developed and maintained by PowerNukkitX.
154200
[**MariaDB Connector**](https://github.com/mariadb-corporation/mariadb-connector-j) MariaDB Connector/J is a Type 4 JDBC
155201
driver. It was developed specifically as a lightweight JDBC connector for use with MariaDB and MySQL database servers.
156202

157-
[**Jdbi**](https://github.com/jdbi/jdbi) The Jdbi library provides convenient, idiomatic access to relational databases
158-
in Java.
203+
[**sql2o**](https://github.com/aaberg/sql2o) A lightweight Java library for working with SQL databases. Provides clean, minimalistic ORM-style mapping between database rows and Java objects.
159204

160205
[**HikariCP**](https://github.com/brettwooldridge/HikariCP) is a "zero-overhead" production ready JDBC connection pool.
161206
At roughly 130Kb, the library is very light.

0 commit comments

Comments
 (0)