MyTetra Share
Делитесь знаниями!
Настройка драйвера SQLite Java
Время создания: 15.09.2017 20:06
Раздел: Java - SQLite - Драйвер
Запись: xintrea/mytetra_db_mcold/master/base/15054951551opjj3quos/text.html на raw.githubusercontent.com

см. приложение


SQLite Java: 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 on bitbucket. You should download the latest version of the driver. As of this writing, the latest version is 3.8.11.2.

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

Add SQLite JDBC Driver JAR file to a Java project

We will use NetBean IDE for developing Java SQLite applications.

First, create a new project from NetBean by clicking the New Project… button on the toolbar.

Next, choose Java Application and click the Next button.

Then, Enter the project name, its location, and the main class. Click Finish button to create the new project.

After that, to add the SQLite JDBC Driver Jar file to the project, right mouse click on the project name and choose Properties.

Finally, select Libraries (1) in the Categories of the Project Properties windows. Then click on the Add JAR/Folder button (2), choose the SQLite JDBC JAR file (3), and click the Open button (4).

SQLite connection string

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


1

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:


1

jdbc:sqlite:sample.db

Or


1

jdbc:sqlite:C:/sqlite/db/chinook.db

To connect to an in-memory database, you use the following connection string:


1

jdbc:sqlite::memory

Connect to a SQLite database via JDBC

The following program connects to the SQLite sample database chinook.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

package net.sqlitetutorial;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

 

/**

*

* @author sqlitetutorial.net

*/

public class SQLiteJDBCDriverConnection {

     /**

     * Connect to a sample database

     */

    public static void connect() {

        Connection conn = null;

        try {

            // db parameters

            String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";

            // create a connection to the database

            conn = DriverManager.getConnection(url);

            

            System.out.println("Connection to SQLite has been established.");

            

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            try {

                if (conn != null) {

                    conn.close();

                }

            } catch (SQLException ex) {

                System.out.println(ex.getMessage());

            }

        }

    }

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        connect();

    }

}

Let’s run it.


1

Connection to SQLite has been established.

It works as expected.

In this tutorial, we have shown you how to use the SQLite JDBC driver to connect to an SQLite database.

Прикрепленные файлы:
Так же в этом разделе:
 
MyTetra Share v.0.60
Яндекс индекс цитирования