An SQLite Java Tutorial

Written by micah mcdunnigan
  • Share
  • Tweet
  • Share
  • Pin
  • Email
An SQLite Java Tutorial
Databases are one convient way to store data in between application sessions. (Jupiterimages/Photos.com/Getty Images)

Programs store information in variables throughout the life of that program's active session. However, program variables are wiped out the second the program closes. When creating programs in Java, or any other language for that matter, many applications will require the ability to store data then retrieve that data for the next time the program starts up. One way of doing this is to use a database to store that information between program sessions. One lightweight way to do this is with the SQLite database with its Java interface.

Skill level:
Moderate

Other People Are Reading

Instructions

    Creating SQLite Database

  1. 1

    Go to the SQLiteJDBC driver download page (see Resources) for the latest version of the JAR file. Like other Java tools, this library and driver is cross platform and will work with Windows, Mac or Linux.

  2. 2

    Copy the SQLiteJDBC JAR file from your download directory to your Java program's working directory.

  3. 3

    Direct Java to the JAR code with the following syntax: "Class.forName("org.sqlite.JDBC");" at the beginning of your main function.

  4. 4

    Create a connection to the SQLite database with the command "Connection db_connection = DriverManager.getConnection("jdbc:sqlite:test.db");" where "db_connection" is the name you are giving to this database socket connection and "yourDB.db" is your target database file.

  5. 5

    Create the object you will use to query the database with the syntax: "Statement query = db_connection.createStatement();" where "query" is the name you will give to the statement object.

  6. 6

    Create a new table in the SQLite database with the command " query.executeUpdate('create table new_table (myfields);');" where "new_table" is the name for the table you wish to create, and "myfields" are the names of the columns you wish to create in that database, each name separated with a comma.

  7. 7

    Prepare to insert rows into your database table with the code: "PreparedStatement statement = db_connection.prepareStatement('insert into new_table values (?);');" where "statement" is the name you will use for your PreparedStatement object.

  8. 8

    Insert a row of data into your table with the code "statement.setInt(index, int);" for each item you want to insert into the row where "index" is the place in the row you want to write the data, starting with one, and "int" is the number you want to write. If you want to write a different data type replace the "Int" of "setInt" with the appropriate type label.

  9. 9

    Repeat this syntax for each item you want to put into a row. Invoke the "statement.executeUpdate();" command when you have finished writing the insert code to commit those items to the database table.

    Reading SQLite Databases

  1. 1

    Create a connection to the SQLite database with the command "Connection db_connection = DriverManager.getConnection("jdbc:sqlite:yourDB.db");" where "db_connection" is the name you are giving to this database socket connection and "yourDB.db" is your target database file.

  2. 2

    Create the object you will use to query the database with the syntax: "Statement query = db_connection.createStatement();" where "query" is the name you will give to the statement object.

  3. 3

    Retrieve a target row from the database table with the command "ResultSet results = query.executeQuery('select * from target_table where field = n;');" where "target_table" is the name of the table you want to retrieve data from, field is the name of the column, such as an ID column, where the row whose field matches the value you supply in "n" will feed its data into your "results" object.

  4. 4

    Access the value of a given field in the row your retrieved with the command "results.getInt("field");" where "field is the name of the column whose value in that row the function will return. Replace the "Int" of the "getInt" with the appropriate label for the data type you are trying to retrieve.

  5. 5

    Close the connection to the database with "db_connection.close();" when you are finished with the database.

Tips and warnings

  • Remember to include the BSD license for the SQLite driver if you are going to use the program you create with it for other than personal uses.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.