How to use SQLite with Java using NetBeans IDE


  • SQLite is an embedded database which will have all the tables stored in a db file. This saves the trouble of having to setup database servers everytime we develop a new programs. Android uses SQLite as a database.
  • I was browsing through the net on how to use SQLite for a small app I was developing. I came across this page which had very good directions on how to use SQLite with Java.

1. SQLite is serverless. With SQLiteJDBC driver, we can write programs to access SQLite using JAVA.
This tutorial will have step by step description about how to do it.
2. We will use Netbeans, but it will work in the same manner with eclipse too.
3. Download SQLiteJDBC driver jar, ‘sqlitejdbc-v056.jar’ from website: http://www.zentus.com/sqlitejdbc/
4. Create a java project named ‘SQLite’ in Netbeans.(You can give any name
5. Create a class called ‘Test’ in that project.
6. Add ‘sqlitejdbc-v056.jar’ in the class path of ‘SQLite’ project in Netbeans.
7. Paste following code in Test class
—>
import java.sql.*;
public class Test {
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
Connection conn =
DriverManager.getConnection("jdbc:sqlite:Vinit");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists school;");
stat.executeUpdate("create table school (name, state);");
PreparedStatement prep = conn.prepareStatement(
"insert into school values (?, ?);");
prep.setString(1, "UTD");
prep.setString(2, "texas");
prep.addBatch();
prep.setString(1, "USC");
prep.setString(2, "california");
prep.addBatch();
prep.setString(1, "MIT");
prep.setString(2, "massachusetts");
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
ResultSet rs = stat.executeQuery("select * from school;");
while (rs.next()) {
System.out.print("Name of School = " + rs.getString("name") + " ");
System.out.println("state = " + rs.getString("state"));
}
rs.close();
conn.close();
}
}
—>
8. Run the project as java application.
9. It will create database file by name ‘Vinit’ in the Netbeans ‘SQLite’ project folder.
10. Here is in above code, we are creating sqlite database ‘Vinit’.

  • In NetBeans add libraries by going to Tools –> Libraries and Click ADD LIBRARY. An Add Library dialog will appear

  • Add the driver JAR file location into CLASSPATH tab

  • In the Projects window Right Click on Libraries folder and Select Add Library

Click on Add Library and select Sqlite3.

Sources:

http://en.wikibooks.org/wiki/Java_JDBC_using_SQLite/Connecting

http://tech-britney.blogspot.com/2010/07/using-sqlite-with-sqlitejdbc-driver.html

Advertisements

3 Responses

  1. I do not even know the way I stopped up here, however
    I believed this post used to be good. I don’t know who you’re however definitely you’re
    going to a well-known blogger should you aren’t already. Cheers!

  2. Thank You Very Much from my core of heart. This blog helped me a lot understanding. May God help you.

  3. This post is very good. My Question is that How can I set password on Sqlite database (file) to secure the Database?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: