簡介:這是在Java中使用SQLite的教程的詳細頁面,介紹了和java,有關的知識、技巧、經驗,和一些java源碼等。
簡單的在Java中使用SQLite的教程
使用SQLiteJDBC封裝 www.zentus.com/sqlitejdbc/
作者序言
我一直想寫一個在Java中使用SQLite的例子,但是很長時間都找不到一個真正合適的,
現在我終於找到了,我希望它可以幫助那些像我一樣的Java新手走出困惑. 另外我的一個朋友也在問我如何在Java里使用 數據庫,
所以這篇文章也是為他寫的. 希望我的文章可以真的幫助一些人. 如果你有更好的想法, 請立即告訴我, 謝謝! 布蘭頓 T.
現在我終於找到了,我希望它可以幫助那些像我一樣的Java新手走出困惑. 另外我的一個朋友也在問我如何在Java里使用 數據庫,
所以這篇文章也是為他寫的. 希望我的文章可以真的幫助一些人. 如果你有更好的想法, 請立即告訴我, 謝謝! 布蘭頓 T.
測試環境
- 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());
}
}
}
下載
- SQLiteExample/pmp.db”>pmp.db � 例子中用到的數據庫文件
- SQLiteExample/SQLiteTest.java”>SQLiteTest.java – Java 源文件
- SQLiteExample/pmp_sqlite.sql”>pmp_sqlite.sql � 用來建立數據庫(pmp.db)的SQL語句.
- SQLiteExample/sqlite3.exe”>sqlite3.exe – SQLite 3.3.8 命令行程序用來創建和訪問數據庫(非必需).
- SQLiteExample/sqlitejdbc.zip”>sqlitejdbc.zip v027 (based on SQLite 3.3.8) – SQLiteJDBC 驅動.
- SQLite Administrator – (可選的) 我認為最好的一個免費的圖形數據庫管理工具,支持SQLite 2/3.
- W3 Schools SQL Tutorial � 強力推薦的一套在線SQL語法教程.
使用步驟
- 下載上面的文件.
- 對
我來說最難的事情就是要把這個驅動放在什么地方Netbeans才能找到它. 我是把下載回來的兩個文件(sqlitejdbc.dll
和sqlitejdbc.jar)放到了JDK中的JRE下的lib/ext
目錄中(我的機器上是E:\Programs\Java\jdk1.5.0_08\jre\lib\ext,你可能是c:\Program
Files\Java\jdk1.5.0_08\jre\lib\ext),這樣就Netbeans就可以找到它了. - 把pmp.db放到C盤的根目錄下.
- 看一下SQLiteTest.java中的代碼和注釋,很抱歉沒有文檔的說明.
- 啟動Netbeans, 創建一個新的工程, 加入我的例子文件然后編譯運行. 程序將把數據庫中的所有國家輸出在標准輸出流上.
附加說明
我是如何創建這個數據庫文件的? 我用上面提供的pmp_sqlite.sql文件. 你可以看到每行SQL語句的后面都以分號結尾. 使用命令行工具, 輸入
sqlite3 pmp.db 這樣就可以創建一個數據庫文件, 然后輸入
.read pmp_sqlite.sql 導入SQL建表語句. 最后輸入
.exit 保存數據庫並退出. 在SQLite的網站上還有更多關於如何使用命令行工具創建和訪問數據庫的資料.
原文轉自:http://www.dnbcw.com/biancheng/java/qrkc272508.html
http://crazyblog.sinaapp.com/15540.html