Connect MatrixOne with JDBC

In Java, we can connect to MatrixOne with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.

Before you start

Prerequisite to understand Java Database Connectivity with MatrixOne, make sure you have installed these items as below:

  1. Make sure you have already installed and launched MatrixOne.
  2. Make sure you have already installed JDK 8+ version.
  3. Make sure you have already installed MySQL client.
  4. Make sure you have already installed JAVA IDE, this document uses IntelliJ IDEA as an example, you can also download other IDE.

Steps

  1. Connect to MatrixOne by MySQL client. Create a new database named test or other names you want in MatrixOne and create a new table named t1:

    1. create database test;
    2. use test;
    3. create table t1
    4. (
    5. code int primary key,
    6. title char(35)
    7. );
  2. Create a new Java project testJDBC in IDEA and select Maven as build system, then click on Create.

    JDBC create project

  3. Click on the File > Project Structure, enter into *Project Setting*, select and click Library, then click + button, add new project library From Maven**.

    JDBC project structure

    JDBC add library

  4. Search library with mysql-connector-java, select mysql:mysql-connector-java:8.0.30, apply it to this project.

    JDBC add driver

  5. Modify the default Java source code at src/main/java/org/example/Main.java. In general, the code below create a connection with the connection address and credentials, after connecting to MatrixOne, you can operate on MatrixOne database and tables by Java language.

    For a full example about how to develop a CRUD(Create, Read, Update, Delete) application in MatrixOne with JDBC, please refer to this JDBC CRUD tutorial.

    ``` package org.example;

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;

  1. public class Main {
  2. private static String jdbcURL = "jdbc:mysql://127.0.0.1:6001/test";
  3. private static String jdbcUsername = "dump";
  4. private static String jdbcPassword = "111";
  5. public static void main(String[] args) {
  6. try {
  7. Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
  8. // Do something with the Connection
  9. } catch (SQLException ex) {
  10. // handle any errors
  11. System.out.println("SQLException: " + ex.getMessage());
  12. System.out.println("SQLState: " + ex.getSQLState());
  13. System.out.println("VendorError: " + ex.getErrorCode());
  14. }
  15. }
  16. }
  17. ```

Reference

For a full list about MatrixOne’s support for JDBC features, see JDBC supported features list in MatrixOne.