How Can I Connect to an SQLite Database from Java Using the JDBC Driver?
Dec 12, 2024 pm 10:11 PMConnecting 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(); }
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(); } } } } }
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

Node.js 20: Key Performance Boosts and New Features

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?
