SQLite是一個輕量級的資料庫系統,不需要安裝就可以使用,也可以十分容易的就內嵌於系統內,FireFox就內嵌SQLite,可以在FireFox上直接使用JavaScript來呼叫操作資料庫。 SQLite是由C語言撰寫而已,可以跨Linux及Windows等平台,在Java存取及操作上則可使用JDBC來連線SQLite。 在JDBC連線SQLite上,大概分成二種方式,一種是由Pure-Java來連結資料,另一種則是直接利用Java呼叫C語言撰寫的函式庫,不過在實測上直接呼叫C的函式庫的方式應該是比較快的,不過在無法找到合適C函式庫的平台則可使用Pure-Java版。 SQLiteJDBC目前查到這個版本是比較有在更新,而且在使用上跟一般的JDBC幾乎是一樣的,上手程式十分簡單。 SQLiteJDBC可以由這裡下載,以下範例是由3.5.7版本製作,提供建立Table、移除Table、查詢、新增、刪除及修改等範例。 - import java.sql.*;
-
- import org.sqlite.SQLiteConfig;
- import org.sqlite.SQLiteDataSource;
- public class hh extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- static String SQL_NAME ;
- private static final String MAIN_DATA_TABLE_NAME = "maindata";
- //表的4個欄位
- private static final String MAIN_DATA_Label = "label";
- private static final String MAIN_DATA_NAME = "package";
- private static final String MAIN_DATA_Move = "move";
- private static final String MAIN_DATA_ICON = "icon";
- public static void main(String[] args) throws SQLException {
-
- hh test = new hh();
- Connection con = test.getConnection();
- //查詢顯示資料
- test.selectAll(con);
- }
- public Connection getConnection() throws SQLException
- {
-
- SQLiteConfig config = new SQLiteConfig();
- // config.setReadOnly(true);
- config.setSharedCache(true);
- config.enableRecursiveTriggers(true);
-
-
- SQLiteDataSource ds = new SQLiteDataSource(config);
- ds.setUrl("jdbc:sqlite:C:\\testDBinSD");
- return ds.getConnection();
- //ds.setServerName("sample.db");
-
-
- }
- //create Table
-
- public void createTable(Connection con )throws SQLException{
- String mainDataSQL = "CREATE TABLE IF NOT EXISTS "
- + MAIN_DATA_TABLE_NAME
- + "( "
- + MAIN_DATA_Move + " TEXT, "
- + MAIN_DATA_Label + " TEXT, "
- +"id INTEGER PRIMARY KEY AUTOINCREMENT, "
- + MAIN_DATA_NAME + " TEXT, "
- + MAIN_DATA_ICON + " TEXT);";
- Statement stat = null;
- stat = con.createStatement();
- stat.executeUpdate(mainDataSQL);
-
- }
- //drop table
- public void dropTable(Connection con)throws SQLException{
- String mainDataSQL = "drop table maindata ";
- Statement stat = null;
- stat = con.createStatement();
- stat.executeUpdate(mainDataSQL);
- }
-
- //新增
- public void insert(Connection con,int id,String move,String packages,String label,String icon)throws SQLException{
- String mainDataSQL = "insert into mainData (id,move,package,label,icon) values(?,?,?,?,?)";
- PreparedStatement pst = null;
- pst = con.prepareStatement(mainDataSQL);
- int idx = 1 ;
- pst.setInt(idx++, id);
- pst.setString(idx++, packages);
- pst.executeUpdate();
-
- }
- //修改
- public void update(Connection con,int id,String move,String packages,String label,String icon)throws SQLException{
- String mainDataSQL = "update maindata set move = "+move+",package = "+packages+",label = "+label+",icon = "+icon+" where id = "+id;
- PreparedStatement pst = null;
- pst = con.prepareStatement(mainDataSQL);
- /* int idx = 1 ;
- pst.setString(idx++, packages );
- pst.setInt(idx++, id);*/
- pst.executeUpdate(mainDataSQL);
- }
- //刪除
- public void delete(Connection con,int id)throws SQLException{
- String mainDataSQL = "delete from maindata where id = ?";
- PreparedStatement pst = null;
- pst = con.prepareStatement(mainDataSQL);
- int idx = 1 ;
- pst.setInt(idx++, id);
- pst.executeUpdate();
- }
-
- public void selectAll(Connection con)throws SQLException{
- String mainDataSQL = "select * from maindata";
- Statement stat = null;
- ResultSet rs = null;
- stat = con.createStatement();
- rs = stat.executeQuery(mainDataSQL);
- while(rs.next())
- {
- System.out.println(rs.getInt("id")+"\t"+rs.getString("move")+"\t"+rs.getString("label")+"\t"+rs.getString("package")+"\t"+rs.getString("icon"));
- }
- }
- /**
- * Create the frame.
- */
- public hh() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
- }
- }
複製代碼SQLite並沒有使用者登入的的機制,所以只需要告知要存取的資料庫檔案位置就可以使用了。jdbc:sqlite:C:\\testDBinSD其中testDBinSD就是檔案名稱,也可指定其路徑位置c:\testDBinSD。
|