Quantcast
Channel: The Crazy Programmer
Viewing all articles
Browse latest Browse all 761

Java SQLite Tutorial

$
0
0

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.

The post Java SQLite Tutorial appeared first on The Crazy Programmer.


Viewing all articles
Browse latest Browse all 761

Trending Articles