Java SQLite Tutorial

In this tutorial you will learn about Java SQLite.

SQLite is lightweight, zero configuration, serverless SQL database library. In this tutorial I will teach you how to use SQLite database with Java.

Note: I have made this tutorial using Eclipse. Because it is easier to import library in an IDE.

First off all you need to download the SQLite JDBC library or jar. Download it from below link.

Download: http://www.java2s.com/Code/Jar/s/Downloadsqlitejdbc372jar.htm

Now just import it into your project. If you don’t know how to import library in Eclipse then follow below link.

http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse

Java SQLite Tutorial

Java SQLite Tutorial

Connection with Database

Below example shows how you can connect Java with SQLite database. Here demo.db is the database name, you can change it according to you. If the database doesn’t exists then it will be created.

package com;

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

public class JavaSQLiteExample {

	public static void main(String args[]){
		try{
			
			//establish connection with database
			Class.forName("org.sqlite.JDBC");
			Connection con=DriverManager.getConnection("jdbc:sqlite:demo.db");

			if(con!=null){
				System.out.println("Connection established");
			}
			con.close();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Some error occured");
		}
	}	
}

 

On successful connection the program show output “Connection established” otherwise “Some error occurred”.

 

Java SQLite Example

The below program first establish connection with SQLite database, create a table, insert some record in it and then finally display the records. This shows that how you can work with SQLite using Java.

package com;

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

public class JavaSQLiteExample {

	public static void main(String args[]){
		try{
			
			//establish connection with database
			Class.forName("org.sqlite.JDBC");
			Connection con=DriverManager.getConnection("jdbc:sqlite:demo.db");
			
			Statement st=con.createStatement();
			
			//create table
			System.out.println("Create table:");
			st.executeUpdate("drop table record");
			st.executeUpdate("create table record (name text,age int)");
			
			//insert some records 
			System.out.println("Insert some records:");
			st.executeUpdate("insert into record values('neeraj',21)");
			st.executeUpdate("insert into record values('mayank',22)");
			st.executeUpdate("insert into record values('sumit',22)");

			//reading records
			System.out.println("Reading records:");
			ResultSet rs=st.executeQuery("select * from record");
			
			while(rs.next()){
				System.out.println(rs.getString("name")+" "+rs.getString("age"));
			}
			
			rs.close();
			st.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

 

Output

Java SQLite Tutorial Output

So this was the simple Java SQLite tutorial. You can comment below if you are facing any problem.

2 thoughts on “Java SQLite Tutorial”

  1. If one creates and runs the programs on this page in sequence, the second program will fail because it tries to create “test.db” but finds it already there. It would be better to change the name of the first one so that the sample programs will work as intended.

    It would be useful for people like me who have never quite figured out the Unix way of handling pathnames to give an example of how output could be directed to, e.g., the same directory that contains the Java program.

    Thanks for the good tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *