English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this chapter, you will learn how to use SQLite in Java programs.
Before we start using SQLite in our Java program, you need to make sure that the SQLite JDBC driver and Java are set up on your computer. You can check the Java tutorial to install Java on your computer. Now, let's check how to set up the SQLite JDBC driver.
Fromsqlite-jdbcDownload the latest version fromsqlite-jdbc-(VERSION).jar.
Store the downloaded jar filesqlite-jdbc-(VERSION).jar addedto your classpath, or with-Use the classpath option as described in the following example.
The following Java program shows how to connect to an existing database. If the database does not exist, it will create the database, and finally, it will return a database object.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Database opened successfully"); } }
Now, let's compile and run the above program totest.dbCreate a database in the current directory. You can change the path as needed. We assume that the latest version of the JDBC driver is provided in the current path.sqlite-jdbc-3.7.2.jar.
$javac SQLiteJDBC.java $java -classpath ".:sqlite"-jdbc-3.7.2.jar" SQLiteJDBC Open database successfully
If you are using a Windows computer, you can compile and run the code as follows-
$javac SQLiteJDBC.java $java -classpath ".;sqlite"-jdbc-3.7.2.jar" SQLiteJDBC Database Successfully Opened
The following Java program will be used to create a table in the previously created database.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; Statement stmt = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); System.out.println("Database opened successfully"); stmt = c.createStatement(); String sql = "CREATE TABLE COMPANY" + "(ID INT PRIMARY KEY" + "NAME" + "AGE" + "ADDRESS"5"0), " + "SALARY REAL)"; stmt.executeUpdate(sql); stmt.close(); c.close(); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Table creation successful"); } }
After compiling and executing the above program, it will create the COMPANY table in your companytest.dbThe final list of files is as follows-
-rw-r--r--. 1 root root 3201128 Jan 22 19:04 sqlite-jdbc-3.7.2.jar -rw-r--r--. 1 root root 1506 May 8 05:43 SQLiteJDBC.class -rw-r--r--. 1 root root 832 May 8 05:42 SQLiteJDBC.java -rw-r--r--. 1 root root 3072 May 8 05:43 test.db
The following Java program demonstrates how to create a record in the COMPANY table created in the above example.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; Statement stmt = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); c.setAutoCommit(false); System.out.println("Database opened successfully"); stmt = c.createStatement(); String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES ("1, 'Paul', 32, 'California', 20000.00 );" stmt.executeUpdate(sql); sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES ("2, 'Allen', 25, 'Texas', 15000.00 );" stmt.executeUpdate(sql); sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES ("3, 'Teddy', 23, 'Norway', 20000.00 );" stmt.executeUpdate(sql); sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES ("4, 'Mark', 25, 'Rich-Mond ', 65000.00 );" stmt.executeUpdate(sql); stmt.close(); c.commit(); c.close(); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Record creation successful"); } }
When compiling and executing the above program, it will create the specified record in the COMPANY table and display the following two lines-
Database Successfully Opened Record creation successful
The following Java program demonstrates how to retrieve and display records from the COMPANY table created in the above example.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; Statement stmt = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); c.setAutoCommit(false); System.out.println("Database opened successfully"); stmt = c.createStatement(); ResultSet rs = stmt.executeQuery("SELECT "); * FROM COMPANY; "); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String address = rs.getString("address"); float salary = rs.getFloat("salary"); System.out.println("ID = "); + id); System.out.println("NAME = "); + name); System.out.println("AGE = "); + age); System.out.println("ADDRESS = "); + address); System.out.println("SALARY = "); + salary); System.out.println(); } rs.close(); stmt.close(); c.close(); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Operation successful"); } }
After compiling and executing the above program, the following results will be produced.
Database Successfully Opened ID = 1 NAME = Paul AGE = 32 ADDRESS = California SALARY = 20000.0 ID = 2 NAME = Allen AGE = 25 ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy AGE = 23 ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark AGE = 25 ADDRESS = Rich-Mond SALARY = 65000.0 Operation Successful
The following Java code demonstrates how to use the UPDATE statement to update any record and then retrieve and display the updated records from the COMPANY table.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; Statement stmt = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); c.setAutoCommit(false); System.out.println("Database opened successfully"); stmt = c.createStatement(); String sql = "UPDATE COMPANY set SALARY= 25000.00 where ID=1"; "; stmt.executeUpdate(sql); c.commit(); ResultSet rs = stmt.executeQuery("SELECT "); * FROM COMPANY; "); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String address = rs.getString("address"); float salary = rs.getFloat("salary"); System.out.println("ID = "); + id); System.out.println("NAME = "); + name); System.out.println("AGE = "); + age); System.out.println("ADDRESS = "); + address); System.out.println("SALARY = "); + salary); System.out.println(); } rs.close(); stmt.close(); c.close(); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Operation completed successfully"); } }
After compiling and executing the above program, the following results will be produced.
Database Successfully Opened ID = 1 NAME = Paul AGE = 32 ADDRESS = California SALARY = 25000.0 ID = 2 NAME = Allen AGE = 25 ADDRESS = Texas SALARY = 15000.0 ID = 3 NAME = Teddy AGE = 23 ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark AGE = 25 ADDRESS = Rich-Mond SALARY = 65000.0 Operation completed successfully
The following Java code demonstrates how to use the DELETE statement to delete any record and then retrieve and display the remaining records from the COMPANY table.
import java.sql.*; public class SQLiteJDBC { public static void main(String args[]) { Connection c = null; Statement stmt = null; try { Class.forName("org.sqlite.JDBC"); c = DriverManager.getConnection("jdbc:sqlite:test.db"); c.setAutoCommit(false); System.out.println("Database opened successfully"); stmt = c.createStatement(); String sql = "DELETE from COMPANY where ID=";2"; "; stmt.executeUpdate(sql); c.commit(); ResultSet rs = stmt.executeQuery("SELECT "); * FROM COMPANY; "); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); String address = rs.getString("address"); float salary = rs.getFloat("salary"); System.out.println("ID = "); + id); System.out.println("NAME = "); + name); System.out.println("AGE = "); + age); System.out.println("ADDRESS = "); + address); System.out.println("SALARY = "); + salary); System.out.println(); } rs.close(); stmt.close(); c.close(); } catch (Exception e) { System.err.println(e.getClass().getName()); + ": " + e.getMessage()); System.exit(0); } System.out.println("Operation successful"); } }
After compiling and executing the above program, the following results will be produced.
Database Successfully Opened ID = 1 NAME = Paul AGE = 32 ADDRESS = California SALARY = 25000.0 ID = 3 NAME = Teddy AGE = 23 ADDRESS = Norway SALARY = 20000.0 ID = 4 NAME = Mark AGE = 25 ADDRESS = Rich-Mond SALARY = 65000.0 Operation Successful