Sample project using the Java Database Connectivity (JDBC) API to connect to a Dolt database.
Check out the associated blog post for an in-depth walkthrough on how to connect to a Dolt database via JDBC using this sample code.
Before you can run this sample code, you need to start up a Dolt SQL server for this code to connect to. If you don't already have Dolt installed, head over to the Dolt installation docs and follow the instructions to install Dolt.
Once Dolt is installed, create a directory for your new Dolt database and initialize it as a Dolt database directory. The directory name you use here will be the name of the Dolt database.
mkdir doltdb && cd doltdb
dolt init
After you run dolt init
, you'll have a working Dolt database directory. To launch a Dolt SQL server that serves the data in this directory, run:
dolt sql-server --port 11229
Note that by default, Dolt's SQL server will start up on port 3306, the same port used by MySQL. In case you already have a MySQL server running on your machine, we've added the --port 11229
flag to start the server on a different port. Keep this terminal window open so you can see any log statements emitted from the server.
Next, we'll open up a SQL shell to our Dolt database and create some sample data. In a new terminal window, run:
mysql --port 11229 --protocol TCP -u root
This will open up a SQL shell connected to your Dolt database. There isn't any data in the database yet, so let's create some sample data to query:
-- the database name comes from the directory where we ran dolt init
use doltdb;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
-- Insert some sample data
INSERT INTO users (name, email) VALUES ("Mickey", "themick@dolthub.com"), ("Minnie", "minnie@dolthub.com"), ("Goofy", "goofy@dolthub.com");
-- Create a Dolt commit
CALL dolt_commit('-Am', 'Adding sample data for users table');
Once you've got some sample data loaded into your Dolt database, you're ready to run the sample code! You can execute the code directly from your IDE's tooling, or you can run it from the command line using Maven:
mvn clean compile exec:java -D exec.mainClass=Main
If you run into any problems using this sample code, or just want some help integrating your application with Dolt, come join the DoltHub Discord server and connect with our development team as well as other Dolt customers. We'll be happy to learn more about what you're building and help you get Dolt working!