29
loading...
This website collects cookies to deliver better user experience
JDBC driver
Connection
Connection pool
mariadb
client tool using the database user that I created beforehand:mariadb -u user -p
mariadb --host example.skysql.net --port 5001 \
--user db_user --password \
--ssl-verify-server-cert \
--ssl-ca /path/to/skysql_chain.pem
jdbc_demo
and a table with the name programming_language
as follows:CREATE DATABASE jdbc_demo;
USE jdbc_demo;
CREATE TABLE programming_language(
name VARCHAR(50) NOT NULL UNIQUE,
Rating INT
);
quit
command to exit the client tool.<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>jdbc-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
mariadb-java-client
. We have also added the Maven Shade Plugin to be able to generate an executable JAR that includes the JDBC driver (Uber JAR).mvn package
to download the MariaDB Connector/J dependency and check that the project builds correctly:main
) and a couple of methods. You can use any IDE to create the class or we can manually add a new subdirectory for the Java package where our code will reside. On Linux-like systems, this will look like the following:package com.example;
public class Application {
public static void main(String[] args) throws SQLException {
openDatabaseConnection();
closeDatabaseConnection();
}
private static void openDatabaseConnection() {
}
private static void closeDatabaseConnection() {
}
}
DataSource
interface or the DriverManager
class. For simplicity, we are going to use the latter in this step of the tutorial, but most serious applications should use a connection pool (we’ll learn this in the third article of this series). jdbc:<subprotocol>:<subname>
<subprotocol>
part identifies the kind of database. The <subname>
part is database-specific and typically contains information on the location of the database instance and configuration parameters. For example, in the case of a MariaDB server running locally, the connection URL looks something like this:jdbc:mariadb://localhost:3306/jdbc_demo
jdbc:mariadb://example.skysql.net:5001/jdbc_demo?useSsl=true&serverSslCert=/path/to/skysql_chain.pem
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Application {
private static Connection connection;
public static void main(String[] args) throws SQLException {
openDatabaseConnection();
closeDatabaseConnection();
}
private static void openDatabaseConnection() throws SQLException{
System.out.println("Opening database connection...");
connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/jdbc_demo",
"user", "password"
);
System.out.println("Connection valid: " + connection.isValid(0));
}
private static void closeDatabaseConnection() {
}
}
Connection
to the class. This interface contains all the methods to interact with the database. Second, we used the DriverManager
class to get a new connection to the database using the connection URL and the database username and password. Third, we showed a message confirming that the connection is valid.throws
clause to the method signatures. Most JDBC operations throw an SQLException
in case of errors. We can handle it in a catch block or in the case we want to show an error message to the user. In this demo, we won’t deal with exception handling.private static void closeDatabaseConnection() throws SQLException {
connection.close();
}
openDatabaseConnection()
and closeDatabaseConnection()
methods, things could go wrong at any point and we might not be able to close the database connection properly. Hopefully, this can be easily solved by enclosing the code in a try
block and closing the connection in a finally
block. The finally
block always gets executed even if a problem occurs and an exception is thrown. Here’s the final result with the problem fixed:package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Application {
private static Connection connection;
public static void main(String[] args) throws SQLException {
try {
openDatabaseConnection();
} finally {
closeDatabaseConnection();
}
}
private static void openDatabaseConnection() throws SQLException{
System.out.println("Opening database connection...");
connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/jdbc_demo",
"user", "password"
);
System.out.println("Connection valid: " + connection.isValid(0));
}
private static void closeDatabaseConnection() throws SQLException {
connection.close();
System.out.println("Connection valid: " + connection.isValid(0));
}
}
mvn clean package
to build the final JAR and run it from the command line as follows:java -jar target/jdbc-demo-1.0-SNAPSHOT.jar
if (true) throw new RuntimeException("Simulated error!");
connection
object to send SQL statements to the database.