Accessing MariaDB on NetBeans 8.2 using JDBC, Part 2: Create a connection

This entry is part 2 of 3 in the series Accessing MariaDB on NetBeans 8.2 using JDBC

From part 1, I already installed MariaDB using XAMPP and import employees sample database. Now I’m ready to write JAVA code to establish a connection to the database from NetBeans.


  1. NetBeans 8.2
  2. MySQL Connector/J

Part 2: Create a connection

  1. Download and install NetBeans 8.2.
  2. Download MySQL Connector/J
    1. Open web browser to
    2. Select Platform Independent in Operating System drop down option.
    3. Click Download for Platform Independent (Architecture Independent), ZIP Archive
      Download MySQL Connector/J
  3. Extract the zip file to a folder, you’ll see mysql-connector-java-8.0.12.jar which is the library file that we want. Just copy the file to a library folder, for example to “C:\Program Files (x86)\Java\jdk1.8.0_144\lib” directory.
    Note: You should place mysql-connector-java-8.0.12.jar in the directory that you won’t delete it later (ex. not in temp folder). It should be in the same directory that keep JAVA’s common library files. If you delete the file without delete a link from the project, the project will display error about missing library.
  4. Open NetBeans and create a new project TestMariaDB.
    Create a new project
  5. Add the JAR file to the project’s library
    1. Right-click the project name and select Properties.
      Open project's properties
    2. Select Libraries on left window and click Add JAR/Folder.
      Add JAR/Folder
    3. Select the JAR file in 3 and click Open.
      Add the JAR file to project
    4. Click OK.
      Close project's properties
  6. Now I’m ready to create a connect to MariaDB.
    1. Import some library.
      import java.sql.*;

      Import java.sql libraries

    2. Add these code in main method.
      try {
          Connection conn = null;
          conn =  DriverManager.getConnection("jdbc:mysql://" +
      } catch (SQLException e) {
          System.out.println("SQL Exception: "+ e.toString());
      } catch (ClassNotFoundException cE) {
          System.out.println("Class Not Found Exception: "+ cE.toString());
      } catch (Exception ex) {
          System.out.println("Exception: "+ ex.toString());

      Code explanation:

      • Line 9: this for registering the driver with DriverManager interface.
      • Line 10-12: Declare connection string for connecting to the database. In this example, my database server is If your database server is on the same computer, you can use locahost or

        My database name is employees. If you want to connect to other database, simply change text ’employees’ after ‘’ to your database name.

        The user/password are user_employees/mypassword1234 which is created in Part 1. You have to change these values to match your database information.

      JAVA code to connect to MariaDB database

    3. Compile and run the project. If no error occurs, it means that the connection has established successfully.
      Compile result in output window
Series Navigation<< Accessing MariaDB on NetBeans 8.2 using JDBC, Part 1: Install Employees sample database and grant permissionAccessing MariaDB on NetBeans 8.2 using JDBC, Part 3: Perform SQL Operations >>

Leave a Reply