Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

sqlite java

作者:潮起潮落   发布日期:2025-05-18   浏览:85

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class SQLiteExample {
    // JDBC URL, username, and password of SQLite database
    static final String JDBC_URL = "jdbc:sqlite:sample.db";  // SQLite数据库的JDBC URL
    static final String USER = "";  // SQLite不需要用户名
    static final String PASS = "";  // SQLite不需要密码

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            // Establish the connection to the database
            connection = DriverManager.getConnection(JDBC_URL);  // 获取数据库连接
            System.out.println("Connected to SQLite database.");

            // Create a statement object to execute SQL queries
            statement = connection.createStatement();  // 创建Statement对象来执行SQL语句

            // Execute a query to create a table
            String sqlCreateTable = "CREATE TABLE IF NOT EXISTS persons (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "name TEXT NOT NULL," +
                    "age INTEGER)";
            statement.execute(sqlCreateTable);  // 执行创建表的SQL语句

            // Insert data into the table
            String sqlInsert = "INSERT INTO persons (name, age) VALUES ('John Doe', 30)";
            statement.executeUpdate(sqlInsert);  // 插入数据到表中

            // Query data from the table
            String sqlSelect = "SELECT id, name, age FROM persons";
            ResultSet resultSet = statement.executeQuery(sqlSelect);  // 执行查询并获取结果集

            // Process the result set
            while (resultSet.next()) {  // 遍历结果集
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close resources
            try {
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

解释说明:

  1. JDBC URL: jdbc:sqlite:sample.db 是SQLite数据库的连接字符串,表示连接到名为 sample.db 的SQLite数据库文件。
  2. Connection: 使用 DriverManager.getConnection() 方法建立与数据库的连接。
  3. Statement: 创建 Statement 对象用于执行SQL语句。
  4. 创建表: 使用 CREATE TABLE IF NOT EXISTS 语句创建一个名为 persons 的表。
  5. 插入数据: 使用 INSERT INTO 语句向表中插入一条记录。
  6. 查询数据: 使用 SELECT 语句从表中查询数据,并通过 ResultSet 处理查询结果。
  7. 关闭资源: 在 finally 块中确保关闭 StatementConnection 资源,以避免资源泄漏。

上一篇:java class文件

下一篇:class反编译成java文件

大家都在看

java连接数据库的代码

ubuntu 卸载java

java sort用法

java collections.sort

java file类的方法

java 判断

java时间数据类型

java 时间加一天

java demo

java 截取

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站