Simple tutorial on using SQLite in Java

Simple tutorial on using SQLite in Java

http://tech.ddvip.com February 19, 2008 Community Exchange

 

Keyword: SQLite 

Summary: I've been wanting to write an example of using SQLite in Java, but I couldn't find a really suitable one for a long time, and now I finally found it, I hope it can help those who are new to Java like me out of confusion. 

  I've been wanting to write an example of using SQLite in Java, but for a long time I couldn't find a really suitable one, and now I finally found it, I hope it can help those who are new to Java like me out of the confusion.

 

  test environment

 

  Intel x86 Laptop

Windows XP SP2

Java2 JDK 1.5 Update 8

Netbeans IDE 5.0  import java.sql.*;

  import org.sqlite.JDBC;

  /**

  * Very Basic SQLite Database Example

  * @author Brandon Tanner

  */

  public class SQLiteTest {

  public static void main(String[] args) {

  try {

  // The SQLite (3.3.8) Database File

  // This database has one table (pmp_countries) with 3 columns (country_id, country_code, country_name)

  // It has like 237 records of all the countries I could think of.

  String fileName = "c:/pmp.db";

  // Driver to Use

  // http://www.zentus.com/sqlitejdbc/index.html  Class.forName("org.sqlite.JDBC");

  // Create Connection Object to SQLite Database

  // If you want to only create a database in memory, exclude the +fileName

  Connection conn = DriverManager.getConnection("jdbc:sqlite:"+fileName);

  // Create a Statement object for the database connection, dunno what this stuff does though.

  Statement stmt = conn.createStatement();

  // Create a result set object for the statement

  ResultSet rs = stmt.executeQuery("SELECT * FROM pmp_countries ORDER BY country_name ASC");

  // Iterate the result set, printing each column

  // if the column was an int, we could do rs.getInt(column name here) as well, etc.

  while (rs.next()) {

  String id  = rs.getString("country_id");  // Column 1

  String code = rs.getString("country_code"); // Column 2

  String name = rs.getString("country_name"); // Column 3

  System.out.println("ID: "+id+" Code: "+code+" Name: "+name);

  }

  // Close the connection

  conn.close();

  }

  catch (Exception e) {

  // Print some generic debug info

  System.out.println(e.getMessage());

  System.out.println(e.toString());

  }

  }

  }

 

  download

 

  pmp.db – the database file used in the examples

 

  SQLiteTest.java - Java source file

 

  pmp_sqlite.sql - SQL statement used to create the database (pmp.db).

 

  sqlite3.exe - SQLite 3.3.8 command line program to create and access databases (not required).

 

  sqlitejdbc.zip v027 (based on SQLite 3.3.8) - SQLiteJDBC driver.

 

  SQLite Administrator - (Optional) I think the best free graphical database management tool, supports SQLite 2/3.

 

  W3 Schools SQL Tutorial - A highly recommended set of online SQL syntax tutorials.

 

  Steps for usage

 

  Download the file above.

 

  The most difficult thing for me is where to put this driver so that Netbeans can find it. I put the downloaded two files (sqlitejdbc.dll and sqlitejdbc.jar) into the lib/ ext directory (E:ProgramsJavajdk1.5.0_08jrelibext on my machine, you may be c:Program FilesJavajdk1.5.0_08jrelibext), so that Netbeans can find it.

 

  Put pmp.db in the root directory of the C drive.

 

  Take a look at the code and comments in SQLiteTest.java, sorry for the lack of documentation.

 

  Start Netbeans, create a new project, add my example files and compile and run. The program will output all countries in the database on the standard output stream.

 

  Additional Notes How did I create this database file? I used the pmp_sqlite.sql file provided above. You can see that each line of SQL statement ends with a semicolon. Using the command line tool, enter sqlite3 pmp.db and you can Create a database file, then enter .read pmp_sqlite.sql to import the SQL table creation statement. Finally, enter .exit to save the database and exit. There are more information on how to use command line tools to create and access databases on the SQLite website.

 

Guess you like

Origin blog.csdn.net/s_ongfei/article/details/5971223