Summary: in this tutorial, you will learn how to create a new SQLite database using the Java SQLite JDBC.
To create a new SQLite database from the Java application, you follow these steps:
- First, specify the SQLite database file and its location.
- Second, connect to the database via SQLite JDBC driver.
When you connect to an SQLite database file that does not exist, it automatically creates a new database file.
The following program creates a new SQLite database file called my.db
within the same project directory:
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
String url = "jdbc:sqlite:my.db";
try (var conn = DriverManager.getConnection(url)) {
if (conn != null) {
var meta = conn.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
System.out.println("A new database has been created.");
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
Code language: Java (java)
Output:
The driver name is SQLite JDBC
A new database has been created.
Code language: Shell Session (shell)
Summary
- Connecting to an SQLite database file that does not exist will automatically create that database file.
Was this tutorial helpful ?