Connect to Access database with Java [UCan Access] * Set until it can be executed with VS Code

Purpose

Access the MS Access database in Java. At that time, it seems better to use a JDBC driver called UCanAccess, so I will introduce it.

Also, since I am using VS Code in Java development, I will set it until it can be executed here.

Prerequisites

The following environment is assumed.

If you don't develop with VS Code, VS Code is not required. When developing with VS Code, it is necessary to make settings that allow Java development with VS Code. Java development is explained separately in VS Code, so please refer to here. Introduction of Java 15 and VS Code environment settings

Introduction of UCanAccess

UCanAccess is a type of JDBC for connecting to Access.

** What is JDBC ** A library for Java to connect to a database and is included with the JDK. By adding an external library such as UCanAccess this time, you can connect to database products such as Access and MySQL. External libraries exist for each product.

Download UCanAccess

Download from here. https://sourceforge.net/projects/ucanaccess/files/

Select "Download Latest Version" to download UCanAccess-5.0.0-bin.zip. image.png

UCanAccess folder layout

Unzip the downloaded zip. Then move the entire folder to an easy-to-understand location. This time, I placed it in C: \ Program Files \ UCanAccess-5.0.0-bin.

Access settings

Next, enter the Access settings. Open Access and select "Options". image.png Open the option General. image.png Make sure that the order of the new database is "Japanese-Legacy" like this. If not, please change it.

Access settings are now complete.

Create sample database

Create a database to connect from Java. image.png I created it like this.

Also, remember the path to the files in this database. This time, I saved it in the following path. C:\Users\kazus\Documents\sample.accdb

Test with the included console tool

Now, UCanAccess comes with a console tool. This is a tool that allows you to connect to Access via the CLI.

To run it, run console.bat in the UCanAccess folder. Then a command prompt will be launched, and you can throw SQL after that by entering the full path of the Access database here. let's do it. image.png You can operate the database like this!

Used in VS Code Java projects

Now that I know that UCanAccess can be used with console tools, it's time to get into VS Code settings.

Create a Java project

First, create a Java project on VS Code. You can create it with Java: Create Java Project ... from Ctrl + Shift + P. image.png

coding

Let's write the code to connect to the Access database.

App.java


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

public class App {
    public static void main(String[] args) throws Exception {
        try {
            Connection conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/kazus/Documents/sample.accdb");
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from users");
            while (rs.next()) {
                System.out.println(rs.getString("user_name"));
            }
            rs.close();
            st.close();
            conn.close();
        } catch (SQLException e) {
            System.out.println("error");
        }
    }
}

Added UCanAccess library to Java project

Add the UCanAccess .jar to your project's Referenced Libraries. image.png You can add from + like this. The .jar to be added is as follows.

Please restart VS Code just in case you add it.

Execute

Let's execute with ▶. image.png I was able to do it like this!

Recommended Posts

Connect to Access database with Java [UCan Access] * Set until it can be executed with VS Code
How to build Java development environment with VS Code
Connect to DB with Java
Connect to MySQL 8 with Java
A memo to start Java programming with VS Code (2020-04 version)
Java arguments to run with gradle run can be specified with --args (since Gradle 4.9)
Settings to delete unused Java imports when saving with VS Code
Introduction to Java that can be understood even with Krillin (Part 1)
Java build with mac vs code
Easy database access with Java Sql2o
Set the access load that can be changed graphically with JMeter (Part 2)
Set the access load that can be changed graphically with JMeter (Part 1)
Introduction to Java development environment & Spring Boot application created with VS Code
Prepare Java development environment with VS Code
Connect with VS Code from a Windows client to Docker on another server
You can do it right away with Serverless Framework Serverless on AWS (API + Lambda [java] is easy to set up)
Be careful to avoid access blocking with scraping
Using Gradle with VS Code, build Java → run
[Java / PostgreSQL] Connect the WEB application to the database
[Java] char type can be cast to int type
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
Compiled kotlin with cli with docker and created an environment that can be executed with java
Create a Java (Maven) project with VS Code and develop it on a Docker container