Java SQLite Tutorial

In this instructional exercise, you will find out about Java SQLite.

SQLite is a lightweight, zero design, serverless SQL database library. In this instructional

exercise, I will show you how to utilize SQLite database with Java.

Note: I have made this instructional exercise utilizing Eclipse. Since it is simpler to import library in an IDE.

Leading all you have to download the SQLite JDBC library or container. Download it from underneath interface.

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

Presently simply import it into your undertaking. On the off chance that you don’t have a

clue how to import library in Eclipse, at that point pursue underneath interface.

https://stackoverflow.com/questions/3280353/how-to-import-a-container in-obscure

Java SQLite Tutorial

Association with Database

Beneath example shows how you can interface Java with SQLite database. Here demo.db is the database

name, you can transform it as indicated by you. In the event that the database doesn’t exist, at that point, it will be made.

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 the effective association, the program show yield “Association built up” something else “Some mistake happened”.

Java SQLite Example

The underneath program initially builds up an association with SQLite database, make a table, embed some record in it and afterwards at last show the records. This shows how you can function with SQLite utilizing 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

So this was the straightforward Java SQLite instructional exercise. You can remark beneath on the off chance that you are confronting any issue.

Leave a Comment