Home > Java > javaTutorial > How Can I Connect to an SQLite Database from Java Using the JDBC Driver?

How Can I Connect to an SQLite Database from Java Using the JDBC Driver?

DDD
Release: 2024-12-12 22:11:10
Original
742 people have browsed it

How Can I Connect to an SQLite Database from Java Using the JDBC Driver?

Connecting to SQLite from Java

As you mentioned, SQLite's single file database format offers a convenient approach to data storage. One commonly used library for connecting to SQLite from Java is Javasqlite. However, there are indeed other prominent projects available.

SQLite JDBC Driver

One such option is the SQLite JDBC driver. By adding this JAR file to your project's classpath and importing the necessary packages, you can establish a connection to your SQLite database via JDBC.

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

// ...

try {
    Connection connection = DriverManager.getConnection("jdbc:sqlite:my-database.db");
    // ...
} catch (SQLException e) {
    e.printStackTrace();
}
Copy after login

Example Code

Here's a sample Java program that utilizes the SQLite JDBC driver to create, insert, and query data from a database:

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

public class SQLiteExample {

    public static void main(String[] args) {
        Connection connection = null;

        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:test.db");

            Statement statement = connection.createStatement();
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS people (name TEXT, occupation TEXT)");

            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO people (name, occupation) VALUES (?, ?)");
            preparedStatement.setString(1, "John");
            preparedStatement.setString(2, "Developer");
            preparedStatement.executeUpdate();

            ResultSet resultSet = statement.executeQuery("SELECT * FROM people");
            while (resultSet.next()) {
                System.out.println("Name: " + resultSet.getString("name") + ", Occupation: " + resultSet.getString("occupation"));
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy after login

This example creates a database connection, executes SQL commands to create a table and insert data, and finally queries the data from the table. By utilizing the SQLite JDBC driver, you can seamlessly interact with your SQLite database from Java applications.

The above is the detailed content of How Can I Connect to an SQLite Database from Java Using the JDBC Driver?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template