程式扎記: [ Java 代碼範本 ] 使用 Java 操作 SQLiteJDBC

標籤

2011年10月30日 星期日

[ Java 代碼範本 ] 使用 Java 操作 SQLiteJDBC

前言 : 
這裡的範例代碼是基於 Xerial SQLite JDBC Driver 進行實現. 底下是官網對於該 SQLite JDBC Driver 的說明 : 
Our SQLiteJDBC library, developed as a part of Xerial project, requires no configuration since all native libraries for Windows, Mac OS X, Linux and pure-java SQLite, which works in any OS enviroment, are assembled into a single JAR (Java Archive) file. The usage is quite simple; Download our sqlite-jdbc library, then append the library (JAR file) to your class path. See the sample code.

範例代碼 : 
- Sample.java :
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5. import java.sql.Statement;  
  6.   
  7.   
  8. public class Sample  
  9. {  
  10.   public static void main(String[] args) throws ClassNotFoundException  
  11.   {  
  12.     // load the sqlite-JDBC driver using the current class loader  
  13.     Class.forName("org.sqlite.JDBC");  
  14.       
  15.     Connection connection = null;  
  16.     try  
  17.     {  
  18.       // create a database connection  
  19.       connection = DriverManager.getConnection("jdbc:sqlite:sample.db");  
  20.       Statement statement = connection.createStatement();  
  21.       statement.setQueryTimeout(30);  // set timeout to 30 sec.  
  22.         
  23.       statement.executeUpdate("drop table if exists person");  
  24.       statement.executeUpdate("create table person (id integer, name string)");  
  25.       statement.executeUpdate("insert into person values(1, 'leo')");  
  26.       statement.executeUpdate("insert into person values(2, 'yui')");  
  27.       ResultSet rs = statement.executeQuery("select * from person");  
  28.       while(rs.next())  
  29.       {  
  30.         // read the result set  
  31.         System.out.println("name = " + rs.getString("name"));  
  32.         System.out.println("id = " + rs.getInt("id"));  
  33.       }  
  34.     }  
  35.     catch(SQLException e)  
  36.     {  
  37.       // if the error message is "out of memory",   
  38.       // it probably means no database file is found  
  39.       System.err.println(e.getMessage());  
  40.     }  
  41.     finally  
  42.     {  
  43.       try  
  44.       {  
  45.         if(connection != null)  
  46.           connection.close();  
  47.       }  
  48.       catch(SQLException e)  
  49.       {  
  50.         // connection close failed.  
  51.         System.err.println(e);  
  52.       }  
  53.     }  
  54.   }  
  55. }  

至於如何指定 DB 檔案的位置, 是透過 DriverManager.getConnection() 呼叫時同時指定, 幾個簡單範例如下 : 
* Here is an example to select a file C:\work\mydatabase.db (in Windows
  1. Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");  
* A UNIX (Linux, Mac OS X, etc) file /home/leo/work/mydatabase.db 
  1. Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");  

補充說明 : 
Free學習札記 : SQLite with Java 
Java Gossip: Statement、 ResultSet

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!