設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 449|回復: 0
打印 上一主題 下一主題

Connect To The SQLite Database Using SQLite JDBC Driver

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2020-4-27 15:38:35 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Connect To The SQLite Database Using SQLite JDBC Driver
Summary: in this tutorial, we will show you how to download SQLite JDBC Driver and connect to the SQLite database via JDBC.

Download SQLite JDBC Driver
To download the latest version of SQLite JDBC Driver, you go to the download page. You should download the latest version of the driver. At the time of this writing, the latest version is sqlite-jdbc-3.27.2.1.jar.

The JAR file includes both Java class files and SQLite binaries for Mac OX S, Linux, and Windows, Both 32-bit and 64-bit.

SQLite connection strings
The SQLite JDBC driver allows you to load an SQLite database from the file system using the following connection string:

jdbc:sqlite:sqlite_database_file_path

The sqlite_data_file_path is the path to the SQLite database file, which is either relative or absolute path as follows:

jdbc:sqlite:sample.db
Or

jdbc:sqlite:C:/sqlite/db/chinook.db
To connect to an in-memory database, you use the following connection string:

jdbc:sqlite::memory:
Connect to an SQLite database via JDBC
Step 1
Create a new directory called java under c:\sqlite

Step 2
Inside the java folder create a new folder called connect.

Step 3
Copy the jar file sqlite-jdbc-3.27.2.1.jar to the c:\sqlite\connect folder.


Step 4
Create a new subfolder called net inside c:\sqlite\connect\ and another subfolder called sqlitetutorial inside the net folder.

Step 5
Create a new file named Connect.java in the sqlitetutorial folder with the following contents.
The program will connect to the chinook.db database located in the c:\sqlite\db\ folder.

  1. package net.sqlitetutorial;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;

  5. /**
  6. *
  7. * @author sqlitetutorial.net
  8. */
  9. public class Connect {
  10.      /**
  11.      * Connect to a sample database
  12.      */
  13.     public static void connect() {
  14.         Connection conn = null;
  15.         try {
  16.             // db parameters
  17.             String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";
  18.             // create a connection to the database
  19.             conn = DriverManager.getConnection(url);
  20.             
  21.             System.out.println("Connection to SQLite has been established.");
  22.             
  23.         } catch (SQLException e) {
  24.             System.out.println(e.getMessage());
  25.         } finally {
  26.             try {
  27.                 if (conn != null) {
  28.                     conn.close();
  29.                 }
  30.             } catch (SQLException ex) {
  31.                 System.out.println(ex.getMessage());
  32.             }
  33.         }
  34.     }
  35.     /**
  36.      * @param args the command line arguments
  37.      */
  38.     public static void main(String[] args) {
  39.         connect();
  40.     }
  41. }
複製代碼

Note that you should have the chinook.db file downloaded and copied to the C:/sqlite/db/ folder.

Step 6
Launch the command line window and navigate to the sqlitetutorial subfolder created above using the following command:

cd c:\sqlite\java\connect\net\sqlitetutorial
Step 7
Compile the Connect.java file using the following command:

javac Connect.java
You will see a new class file generated:
Note that your JDK must be on the PATH, otherwise you will get an error.

Step 8
Change the current directory to the connect directory:

c:\sqlite\java\connect\net\sqlitetutorial>cd..
c:\sqlite\java\connect\net>cd..
Step 9
Run the net.sqlitetutorial.Connect class using the following command:

java -classpath ".;sqlite-jdbc-3.27.2.1.jar" net.sqlitetutorial.Connect
Here is the output:

Connection to SQLite has been established.
It works as expected.

Troubleshooting
If you receive the following message, you should have missed step 8:

Error: Could not find or load main class net.sqlitetutorial.Connect

How the program works
In the connect() method:

First, declare a variable that holds a connecting string to the sqlite database c:\sqlite\db\chinook.db

String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";
Next, use the DriverManager class to get a database connection based on the connection string.

conn = DriverManager.getConnection(url);
Then, trap any SQLException  in the try catch block and display the error message.

After that, close the database connection in the finally block.

Finally, call the connect() method in the main() method of the Connect class.

In this tutorial, you have learned step by step how to use the SQLite JDBC driver to connect to an SQLite database from a Java program.

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-6-10 19:45 , Processed in 0.230749 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表