Java with SQLite
To use SQLite with java programs, you must have SQLite JDBC Driver and Java set up on the system. Follow the steps given below:
- Download latest version of sqlite-jdbc-(VERSION).jar from sqlite-jdbc repository.
- Add the downloaded jar file to your class path.
- You can now connect to the SQLite database using java.
Connect to SQLite Database
Use the following code to connect to SQLite database using Java programming language:
It is connected with your already created database JTP.db.
Create Database using java
You can also create a new database in SQLite using java programming language. Let’s create a database named “SSSIT.db”. Create a public class “Create” and use the following code:
A new database named “SSSIT.db” is created now. You can see it where you have installed sqlite.
Create a table using java
Let’s create a table named “employees” having columns “name” and “capacity”. Create a class name “CreateTable”, having the following code:
It will create a table “employees” within the SSSIT.db database.
Insert Record in the table
After the creation of the table, use the following code to insert some records in the table. Create a new class “InsertRecords”, having the following code:
Now record is inserted in the table. You can check it out by using the SELECT command:
Select Records
To select records from the table, use the following code. Create a new class “SelectRecords”, having the following data.
Output:
You can see that it displays all the records we inserted once.
Note: By using the same procedure, you can update and delete the table and database.