|
1、這裡https://bitbucket.org/xerial/sqlite-jdbc/downloads下載驅動。
2、配置CLASSPATH路徑。這裡配置的/etc/profile檔。也可以不配置,在運行時不import,直接Class.forName("org.sqlite.JDBC"),用 javac -cp .:路徑 類名,這樣來運行。
3、用eclipse的話,就會你配置了CLASSPATH也找不到,要去工程的Propertries --> Java Buid Path --> Libraries 下添加External JARs。這樣eclipse才能夠找到。
OK,連上了,就好說了。
--------------------------------------------------------------------------------
測試代碼見下:
創建資料庫,表,插入資料,然後查詢。這是從驅動的來源程式的常式裡學的,真是高人啊!- import java.io.File;
- import java.sql.*;
- import org.sqlite.JDBC;
- public class hh extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- hh frame = new hh();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- System.out.println("This is a SqliteTest program!");
-
- //載入資料庫驅動
- try{
- Class.forName("org.sqlite.JDBC");
- System.out.println("Load sqlite Driver sucess!");
- }catch(java.lang.ClassNotFoundException e){
- System.out.println("Fail to Load sqlite Driver!");
- System.out.println(e.getMessage());
- }
- try{
- //連接資料庫
- String connectionString = "jdbc:sqlite:C:\\dbTest.sqlite";
- Connection cn = DriverManager.getConnection(connectionString);
-
- //SQL語句類
- System.out.println("Connect sucessfully!");
- Statement stmt = cn.createStatement();
-
- //創建資料庫
- File testdb = new File("test.db");
- if(testdb.exists()) testdb.delete(); //若有舊的則刪除之
-
- //創建表
- stmt.execute("CREATE TABLE test(id integer primary key, name char(10))");
- //插入資料
- stmt.execute("INSERT INTO test(id, name) VALUES(1, '張三')");
- stmt.execute("INSERT INTO test(id, name) VALUES(2, '李四')");
- //查詢
- ResultSet rs = stmt.executeQuery("SELECT * FROM test");
- while(rs.next()){
- String id = rs.getString("id");
- String name = rs.getString("name");
- System.out.println("id is " + id + " name is " + name);
- }
- //關閉
- stmt.close();
- cn.close();
- }catch(SQLException e){
- System.out.println("Fail!");
- System.out.println(e.getMessage());
- }
- }
- /**
- * 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);
- }
- }
複製代碼 String connectionString = "jdbc:sqlite:C:\\dbTest.sqlite";
這句就是說明SQLITE檔案存放路徑在
C:\dbTest.sqlite下
|
|