Java SQLite Connectivity
Java provides support for connecting to SQLite databases through the use of JDBC (Java Database Connectivity) drivers. SQLite is a self-contained, file-based database management system that is widely used, especially in embedded systems and mobile devices.
Syntax
To connect to an SQLite database in Java, you'll need to:
- Load the JDBC driver using the
Class.forName()
method. - Create a connection object using the
DriverManager.getConnection()
method. - Execute SQL statements on the database using the
Statement
orPreparedStatement
objects.
Example
Suppose we have an SQLite database file called example.db
with a table called employees
that contains columns for id
, name
, and salary
. We want to connect to this database and retrieve the names and salaries of all the employees.
import java.sql.*;
public class SQLiteExample {
public static void main(String[] args) {
try {
// Load the JDBC driver
Class.forName("org.sqlite.JDBC");
// Connect to the database
Connection conn = DriverManager.getConnection("jdbc:sqlite:example.db");
// Execute a SELECT statement
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name, salary FROM employees");
// Iterate over the result set and print the data
while (rs.next()) {
String name = rs.getString("name");
double salary = rs.getDouble("salary");
System.out.println(name + " earns " + salary);
}
// Close the connection and resources
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("JDBC driver not found");
} catch (SQLException e) {
System.out.println("Database connection failed");
System.out.println(e.getMessage());
}
}
}
Output
The output of the above example would be a list of employee names and their salaries, retrieved from the SQLite database.
John Doe earns 50000.0
Jane Smith earns 60000.0
Bob Johnson earns 70000.0
Explanation
In the example above, we first load the JDBC driver for SQLite using the Class.forName()
method. We then establish a connection to the SQLite database using the DriverManager.getConnection()
method and a JDBC URL that specifies the path to the database file.
We execute a SELECT statement on the employees
table using a Statement
object and retrieve the names and salaries of all the employees using a result set. We iterate over the result set using the next()
method and extract the values of the name
and salary
columns using the getString()
and getDouble()
methods.
Finally, we print the names and salaries to the console and close the connection and other resources.
Use
Java SQLite connectivity is useful for creating Java applications that interact with SQLite databases. This can be particularly useful for embedded systems or mobile applications that need to store and retrieve data in a lightweight and self-contained manner.
Important Points
- SQLite is a self-contained, file-based database management system that doesn't require a separate server process.
- JDBC drivers for SQLite are available for download and can be used to connect to SQLite databases from Java applications.
- JDBC provides a standard API for connecting to and interacting with databases from Java applications.
- Always make sure to close the connection and other resources after using them to avoid resource leaks.
- SQLite is not recommended for large-scale or complex database scenarios.
Summary
In this tutorial, we learned about Java SQLite connectivity and how to connect to an SQLite database using JDBC drivers. We saw examples of executing SQL statements on an SQLite database and retrieving data using result sets. Java SQLite connectivity can be useful for creating lightweight Java applications that need to store and retrieve data in a self-contained manner.