Skip to content

Commit dceccba

Browse files
committed
Updated dependencies in JDBC examples
1 parent 843d1d7 commit dceccba

File tree

25 files changed

+99
-94
lines changed

25 files changed

+99
-94
lines changed

jdbc/mariadb-pool/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Prepare the database:
2626
CREATE DATABASE demo;
2727
CREATE USER 'user'@'%';
2828
GRANT ALL ON demo.* TO 'user'@'%' IDENTIFIED BY 'password';
29-
FLUSH PRIVILEGES;
29+
3030

3131
USE demo;
3232
CREATE TABLE programming_language(

jdbc/mariadb-pool/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
<version>1.0-SNAPSHOT</version>
88

99
<properties>
10-
<maven.compiler.source>17</maven.compiler.source>
11-
<maven.compiler.target>17</maven.compiler.target>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
1212
</properties>
1313

1414
<dependencies>
1515
<dependency>
1616
<groupId>org.mariadb.jdbc</groupId>
1717
<artifactId>mariadb-java-client</artifactId>
18-
<version>3.1.2</version>
18+
<version>3.2.0</version>
1919
</dependency>
2020
</dependencies>
2121

jdbc/mariadb-pool/src/main/java/com/example/Application.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
public class Application {
1616

1717
public static void main(String[] args) throws InterruptedException {
18-
ExecutorService executorService = Executors.newFixedThreadPool(200);
19-
20-
IntStream.range(0, 1000)
21-
.mapToObj(i -> (Runnable) () -> requestService())
22-
.forEach(executorService::submit);
23-
24-
executorService.shutdown();
25-
executorService.awaitTermination(1, TimeUnit.MINUTES);
18+
try(ExecutorService executorService = Executors.newFixedThreadPool(200)) {
19+
IntStream.range(0, 1000)
20+
.mapToObj(i -> (Runnable) () -> requestService())
21+
.forEach(executorService::submit);
22+
23+
executorService.shutdown();
24+
executorService.awaitTermination(1, TimeUnit.MINUTES);
25+
}
2626
}
2727

2828
public static void requestService() {

jdbc/mariadb-pool/src/main/java/com/example/Service.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
package com.example;
22

3-
import org.mariadb.jdbc.MariaDbDataSource;
4-
import org.mariadb.jdbc.MariaDbPoolDataSource;
5-
6-
import javax.sql.DataSource;
73
import java.sql.Connection;
84
import java.sql.PreparedStatement;
95
import java.sql.ResultSet;
106
import java.sql.SQLException;
117
import java.util.ArrayList;
128
import java.util.List;
139

10+
import javax.sql.DataSource;
11+
12+
import org.mariadb.jdbc.MariaDbDataSource;
13+
1414
/**
1515
* You can configure the database connection in the constructor.
1616
* You need an SQL database with the following table:
1717
*
1818
* <pre>
1919
* CREATE TABLE programming_language(
20-
* name VARCHAR(50) NOT NULL UNIQUE,
21-
* Rating INT
20+
* pl_name VARCHAR(50) NOT NULL UNIQUE,
21+
* pl_rating INT
2222
* );
2323
* </pre>
2424
*
@@ -43,29 +43,34 @@ private Service() throws SQLException {
4343
*/
4444
dataSource.setUrl("jdbc:mariadb://localhost:3306/demo");
4545
dataSource.setUser("user");
46-
dataSource.setPassword("password");
46+
dataSource.setPassword("Password123!");
4747
this.dataSource = dataSource;
4848
/*
4949
* If you are using MariaDB SkySQL (https://mariadb.com/products/skysql),
5050
* enable SSL and specify the path to the CA chain file that you can download
5151
* from the SkySQL Portal (https://cloud.mariadb.com):
52-
* jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?useSsl=true&
52+
* jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?sslMode=verify-ca&
5353
* serverSslCert=/path/to/your/skysql_chain.pem
5454
*/
5555
}
5656

5757
public static synchronized Service getInstance() throws SQLException {
58-
return instance == null ? instance = new Service() : instance;
58+
if(instance == null) {
59+
instance = new Service();
60+
}
61+
return instance;
5962
}
6063

6164
public List<String> getAllProgrammingLanguages() throws SQLException {
6265
var list = new ArrayList<String>();
66+
// expect a SQLNonTransientConnectionException (too many connections)
67+
// see README.md
6368
try (Connection connection = dataSource.getConnection()) {
6469
try (PreparedStatement statement = connection.prepareStatement(
65-
"SELECT name FROM programming_language")) {
70+
"SELECT pl_name FROM programming_language")) {
6671
ResultSet resultSet = statement.executeQuery();
6772
while (resultSet.next()) {
68-
list.add(resultSet.getString("name"));
73+
list.add(resultSet.getString("pl_name"));
6974
}
7075
}
7176
}

jdbc/part1/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ Open the connection (alternatively use a try-catch block to close the connection
2222
```java
2323
Connection connection = DriverManager.getConnection(
2424
"jdbc:mariadb://localhost:3306/database_name",
25-
"user", "password"
25+
"user", "Password123!"
2626
);
2727
```
2828

2929
> If you are using [MariaDB SkySQL](https://mariadb.com/products/skysql/), enable SSL and specify the path to the CA chain file that you can download from the [SkySQL Portal](https://cloud.mariadb.com):
3030
>
31-
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/database_name?useSsl=true&serverSslCert=/path/to/your/skysql_chain.pem`
31+
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/database_name?sslMode=verify-ca&serverSslCert=/path/to/your/skysql_chain.pem`
3232
3333
Close the connection (if not using a try-catch block):
3434

@@ -53,8 +53,7 @@ Prepare the database:
5353
```sql
5454
CREATE DATABASE demo;
5555
CREATE USER 'user'@'%';
56-
GRANT ALL ON demo.* TO 'user'@'%' IDENTIFIED BY 'password';
57-
FLUSH PRIVILEGES;
56+
GRANT ALL ON demo.* TO 'user'@'%' IDENTIFIED BY 'Password123!';
5857
```
5958

6059
Run the following from the command line:
@@ -63,7 +62,7 @@ Run the following from the command line:
6362
git clone [email protected]:mariadb-developers/java-quickstart.git
6463
cd java-quickstart/jdbc/part1/
6564
mvn package
66-
java -jar java -jar target/jdbc-demo-1.0-SNAPSHOT.jar
65+
java -jar target/jdbc-demo-1.0-SNAPSHOT.jar
6766
```
6867

6968
Screenshot of the output:

jdbc/part1/pom.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<version>1.0-SNAPSHOT</version>
88

99
<properties>
10-
<maven.compiler.source>17</maven.compiler.source>
11-
<maven.compiler.target>17</maven.compiler.target>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
1212
</properties>
1313

1414
<dependencies>
@@ -19,16 +19,17 @@
1919
<dependency>
2020
<groupId>org.mariadb.jdbc</groupId>
2121
<artifactId>mariadb-java-client</artifactId>
22-
<version>3.1.2</version>
22+
<version>3.2.0</version>
2323
</dependency>
2424
</dependencies>
2525

2626
<build>
2727
<plugins>
2828
<!-- Creates an Uber JAR (with the JDBC driver) -->
2929
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
3031
<artifactId>maven-shade-plugin</artifactId>
31-
<version>3.2.4</version>
32+
<version>3.5.1</version>
3233
<executions>
3334
<execution>
3435
<phase>package</phase>

jdbc/part1/src/main/java/com/example/Application.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public static void main(String[] args) throws SQLException {
2828
private static void initDatabaseConnection() throws SQLException {
2929
System.out.println("Connecting to the database...");
3030
connection = DriverManager.getConnection(
31-
"jdbc:mariadb://localhost:3306/demo",
32-
"user", "password");
31+
"jdbc:mariadb://127.0.0.1:3306/demo",
32+
"user", "Password123!");
3333
System.out.println("Connection valid: " + connection.isValid(5));
3434
/*
3535
If you are using MariaDB SkySQL (https://mariadb.com/products/skysql),
3636
enable SSL and specify the path to the CA chain file that you can download
3737
from the SkySQL Portal (https://cloud.mariadb.com):
38-
jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?useSsl=true&serverSslCert=/path/to/your/skysql_chain.pem
38+
jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?sslMode=verify-ca&serverSslCert=/path/to/your/skysql_chain.pem
3939
*/
4040
}
4141

jdbc/part2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Prepare the database:
5252
CREATE DATABASE demo;
5353
CREATE USER 'user'@'%';
5454
GRANT ALL ON demo.* TO 'user'@'%' IDENTIFIED BY 'password';
55-
FLUSH PRIVILEGES;
55+
5656

5757
USE demo;
5858
CREATE TABLE programming_language(

jdbc/part2/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<version>1.0-SNAPSHOT</version>
88

99
<properties>
10-
<maven.compiler.source>17</maven.compiler.source>
11-
<maven.compiler.target>17</maven.compiler.target>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
1212
</properties>
1313

1414
<dependencies>
@@ -19,7 +19,7 @@
1919
<dependency>
2020
<groupId>org.mariadb.jdbc</groupId>
2121
<artifactId>mariadb-java-client</artifactId>
22-
<version>3.1.2</version>
22+
<version>3.2.0</version>
2323
</dependency>
2424
</dependencies>
2525

jdbc/part2/src/main/java/com/example/Application.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class Application {
1313
*
1414
* <pre>
1515
* CREATE TABLE programming_language(
16-
* name VARCHAR(50) NOT NULL UNIQUE,
17-
* Rating INT
16+
* pl_name VARCHAR(50) NOT NULL UNIQUE,
17+
* pl_rating INT
1818
* );
1919
* </pre>
2020
*
@@ -41,7 +41,7 @@ public static void main(String[] args) throws SQLException {
4141

4242
private static void createData(String name, int rating) throws SQLException {
4343
try (PreparedStatement statement = connection.prepareStatement("""
44-
INSERT INTO programming_language(name, rating)
44+
INSERT INTO programming_language(pl_name, pl_rating)
4545
VALUES (?, ?)
4646
""")) {
4747
statement.setString(1, name);
@@ -53,16 +53,16 @@ INSERT INTO programming_language(name, rating)
5353

5454
private static void readData() throws SQLException {
5555
try (PreparedStatement statement = connection.prepareStatement("""
56-
SELECT name, rating
56+
SELECT pl_name, pl_rating
5757
FROM programming_language
58-
ORDER BY rating DESC
58+
ORDER BY pl_rating DESC
5959
""")) {
6060
try (ResultSet resultSet = statement.executeQuery()) {
6161
boolean empty = true;
6262
while (resultSet.next()) {
6363
empty = false;
64-
String name = resultSet.getString("name");
65-
int rating = resultSet.getInt("rating");
64+
String name = resultSet.getString("pl_name");
65+
int rating = resultSet.getInt("pl_rating");
6666
System.out.println("\t> " + name + ": " + rating);
6767
}
6868
if (empty) {
@@ -75,8 +75,8 @@ private static void readData() throws SQLException {
7575
private static void updateData(String name, int newRating) throws SQLException {
7676
try (PreparedStatement statement = connection.prepareStatement("""
7777
UPDATE programming_language
78-
SET rating = ?
79-
WHERE name = ?
78+
SET pl_rating = ?
79+
WHERE pl_name = ?
8080
""")) {
8181
statement.setInt(1, newRating);
8282
statement.setString(2, name);
@@ -88,7 +88,7 @@ private static void updateData(String name, int newRating) throws SQLException {
8888
private static void deleteData(String nameExpression) throws SQLException {
8989
try (PreparedStatement statement = connection.prepareStatement("""
9090
DELETE FROM programming_language
91-
WHERE name LIKE ?
91+
WHERE pl_name LIKE ?
9292
""")) {
9393
statement.setString(1, nameExpression);
9494
int rowsDeleted = statement.executeUpdate();
@@ -100,13 +100,13 @@ private static void initDatabaseConnection() throws SQLException {
100100
System.out.println("Connecting to the database...");
101101
connection = DriverManager.getConnection(
102102
"jdbc:mariadb://localhost:3306/demo",
103-
"user", "password");
103+
"user", "Password123!");
104104
System.out.println("Connection valid: " + connection.isValid(5));
105105
/*
106106
* If you are using MariaDB SkySQL (https://mariadb.com/products/skysql),
107107
* enable SSL and specify the path to the CA chain file that you can download
108108
* from the SkySQL Portal (https://cloud.mariadb.com):
109-
* jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?useSsl=true&
109+
* jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/demo?sslMode=verify-ca&
110110
* serverSslCert=/path/to/your/skysql_chain.pem
111111
*/
112112
}

jdbc/part3/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ Add HikariCP in the **pom.xml** file ([check](https://search.maven.org/artifact/
1818
Configuration file (**src/main/resources/database.properties**):
1919

2020
```properties
21-
jdbcUrl=jdbc:mariadb://localhost:3306/database_name
21+
jdbcUrl=jdbc:mariadb://127.0.0.1:3306/database_name
2222
dataSource.username=user
23-
dataSource.password=password
23+
dataSource.password=Password123!
2424
```
2525

2626
> If you are using [MariaDB SkySQL](https://mariadb.com/products/skysql/), enable SSL and specify the path to the CA chain file that you can download from the [SkySQL Portal](https://cloud.mariadb.com):
2727
>
28-
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/database_name?useSsl=true&serverSslCert=/path/to/your/skysql_chain.pem`
28+
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/database_name?sslMode=verify-ca&serverSslCert=/path/to/your/skysql_chain.pem`
2929
3030
Create a `DataSource` at application start:
3131

@@ -66,7 +66,7 @@ Prepare the database:
6666
CREATE DATABASE demo;
6767
CREATE USER 'user'@'%';
6868
GRANT ALL ON demo.* TO 'user'@'%' IDENTIFIED BY 'password';
69-
FLUSH PRIVILEGES;
69+
7070

7171
USE demo;
7272
CREATE TABLE programming_language(

jdbc/part3/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<version>1.0-SNAPSHOT</version>
88

99
<properties>
10-
<maven.compiler.source>17</maven.compiler.source>
11-
<maven.compiler.target>17</maven.compiler.target>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
1212
</properties>
1313

1414
<dependencies>
@@ -19,14 +19,14 @@
1919
<dependency>
2020
<groupId>org.mariadb.jdbc</groupId>
2121
<artifactId>mariadb-java-client</artifactId>
22-
<version>3.1.2</version>
22+
<version>3.2.0</version>
2323
</dependency>
2424

2525
<!-- Connection pool -->
2626
<dependency>
2727
<groupId>com.zaxxer</groupId>
2828
<artifactId>HikariCP</artifactId>
29-
<version>5.0.0</version>
29+
<version>5.0.1</version>
3030
</dependency>
3131
<!--
3232
Logging framework. Pick a more suitable SLF4J binding. See

0 commit comments

Comments
 (0)