📜  sqlite java gradle - Java (1)

📅  最后修改于: 2023-12-03 15:05:19.831000             🧑  作者: Mango

SQLite Java Gradle

Introduction

If you are a Java programmer and need a way to manage databases within your application, SQLite is a good choice. It is a lightweight relational database management system that is easy to use and integrates well with Java.

To use SQLite within a Java project, you can add the required dependencies to your Gradle build file. This will allow you to easily create and manage a database within your application.

Setting up Your Project

To use SQLite in your Java project, you will need to add the SQLite JDBC driver and the JDBC API to your Gradle build file.

Here is a sample Gradle build file that adds these dependencies:

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.xerial:sqlite-jdbc:3.34.0'
    implementation 'com.sun.jna:jna:5.8.0'
    implementation 'com.sun.jna:jna-platform:5.8.0'
}

This file specifies the version of the SQLite JDBC driver and the JNA library you will use in your application.

Creating a Database

To create a new SQLite database file, you will need to specify the file location and name. Here is an example of how you can create a new database file:

import java.sql.*;

public class CreateDatabase {

    public static void main(String[] args) {
        String url = "jdbc:sqlite:sample.db";

        try (Connection conn = DriverManager.getConnection(url)) {
            System.out.println("Connection to SQLite has been established.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

In this example, we create a new database file named "sample.db" in the current directory.

Creating Tables

Once you have created a database file, you can create one or more tables within it. Here is an example of how you can create a new table:

import java.sql.*;

public class CreateTable {

    public static void main(String[] args) {
        String url = "jdbc:sqlite:sample.db";

        String sql = "CREATE TABLE IF NOT EXISTS employees (\n"
                + " id integer PRIMARY KEY,\n"
                + " name text NOT NULL,\n"
                + " age integer\n"
                + ");";

        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement()) {
            // create a new table
            stmt.execute(sql);
            System.out.println("Table created successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

In this example, we create a new table named "employees" with three columns: "id" (integer and primary key), "name" (text and not null), and "age" (integer and nullable).

Inserting Data

Once you have created a table, you can insert data into it. Here is an example of how you can insert a new record into the "employees" table:

import java.sql.*;

public class InsertData {

    public static void main(String[] args) {
        String url = "jdbc:sqlite:sample.db";

        String sql = "INSERT INTO employees(id,name,age) VALUES(1, 'John Doe', 29)";

        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement()) {
            // insert a new record
            stmt.execute(sql);
            System.out.println("Record inserted successfully.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
Querying Data

You can retrieve data from a table using a SELECT statement. Here is an example of how you can retrieve all records from the "employees" table:

import java.sql.*;

public class QueryData {

    public static void main(String[] args) {
        String url = "jdbc:sqlite:sample.db";

        String sql = "SELECT * FROM employees";

        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            // retrieve data
            while (rs.next()) {
                System.out.println(rs.getInt("id") + "\t"
                        + rs.getString("name") + "\t"
                        + rs.getInt("age"));
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
Conclusion

By following these steps, you can easily integrate SQLite within your Java project and perform database operations using SQL queries. There are many advanced features you can use with SQLite, such as transactions, triggers, and views, which can help you to manage your data more efficiently.