SQLite - Java

SQLite - Java

参考:

SQLite – Java | 菜鸟教程
http://www.runoob.com/sqlite/sqlite-java.html

安装

在 Java 程序中使用 SQLite 之前,我们需要确保机器上已经有 SQLite JDBC Driver 驱动程序和 Java。可以查看 Java 教程了解如何在计算机上安装 Java。现在,我们来看看如何在机器上安装 SQLite JDBC 驱动程序。

  • 从 sqlite-jdbc 库下载 sqlite-jdbc-(VERSION).jar 的最新版本。
  • 在您的 class 路径中添加下载的 jar 文件 sqlite-jdbc-(VERSION).jar,或者在 -classpath 选项中使用它,这将在后面的实例中进行讲解。

在学习下面部分的知识之前,您必须对 Java JDBC 概念有初步了解。如果您还未了解相关知识,那么建议您可以先花半个小时学习下 JDBC 教程相关知识,这将有助于您学习接下来讲解的知识。

连接数据库

下面的 Java 程序显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Opened database successfully");
  }
}

现在,让我们来编译和运行上面的程序,在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。我们假设当前路径下可用的 JDBC 驱动程序的版本是 sqlite-jdbc-3.7.2.jar

$javac SQLiteJDBC.java
$java -classpath ".:sqlite-jdbc-3.7.2.jar" SQLiteJDBC
Open database successfully

如果您想要使用 Windows 机器,可以按照下列所示编译和运行您的代码:

$javac SQLiteJDBC.java
$java -classpath ".;sqlite-jdbc-3.7.2.jar" SQLiteJDBC
Opened database successfully

创建表

下面的 Java 程序将用于在先前创建的数据库中创建一个表:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      String sql = "CREATE TABLE COMPANY " +
                   "(ID INT PRIMARY KEY     NOT NULL," +
                   " NAME           TEXT    NOT NULL, " +
                   " AGE            INT     NOT NULL, " +
                   " ADDRESS        CHAR(50), " +
                   " SALARY         REAL)";
      stmt.executeUpdate(sql);
      stmt.close();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Table created successfully");
  }
}

上述程序编译和执行时,它会在 test.db 中创建 COMPANY 表,最终文件列表如下所示:

-rw-r--r--. 1 root root 3201128 Jan 22 19:04 sqlite-jdbc-3.7.2.jar
-rw-r--r--. 1 root root    1506 May  8 05:43 SQLiteJDBC.class
-rw-r--r--. 1 root root     832 May  8 05:42 SQLiteJDBC.java
-rw-r--r--. 1 root root    3072 May  8 05:43 test.db

INSERT 操作

下面的 Java 代码显示了如何在上面创建的 COMPANY 表中创建记录:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
                   "VALUES (1, ‘Paul‘, 32, ‘California‘, 20000.00 );";
      stmt.executeUpdate(sql);

      sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
            "VALUES (2, ‘Allen‘, 25, ‘Texas‘, 15000.00 );";
      stmt.executeUpdate(sql);

      sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
            "VALUES (3, ‘Teddy‘, 23, ‘Norway‘, 20000.00 );";
      stmt.executeUpdate(sql);

      sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
            "VALUES (4, ‘Mark‘, 25, ‘Rich-Mond ‘, 65000.00 );";
      stmt.executeUpdate(sql);

      stmt.close();
      c.commit();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Records created successfully");
  }
}

上述程序编译和执行时,它会在 COMPANY 表中创建给定记录,并会显示以下两行:

Opened database successfully
Records created successfully

SELECT 操作

下面的 Java 程序显示了如何从前面创建的 COMPANY 表中获取并显示记录:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
      while ( rs.next() ) {
         int id = rs.getInt("id");
         String  name = rs.getString("name");
         int age  = rs.getInt("age");
         String  address = rs.getString("address");
         float salary = rs.getFloat("salary");
         System.out.println( "ID = " + id );
         System.out.println( "NAME = " + name );
         System.out.println( "AGE = " + age );
         System.out.println( "ADDRESS = " + address );
         System.out.println( "SALARY = " + salary );
         System.out.println();
      }
      rs.close();
      stmt.close();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Operation done successfully");
  }
}

上述程序编译和执行时,它会产生以下结果:

Opened database successfully
ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 20000.0

ID = 2
NAME = Allen
AGE = 25
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

UPDATE 操作

下面的 Java 代码显示了如何使用 UPDATE 语句来更新任何记录,然后从 COMPANY 表中获取并显示更新的记录:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1;";
      stmt.executeUpdate(sql);
      c.commit();

      ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
      while ( rs.next() ) {
         int id = rs.getInt("id");
         String  name = rs.getString("name");
         int age  = rs.getInt("age");
         String  address = rs.getString("address");
         float salary = rs.getFloat("salary");
         System.out.println( "ID = " + id );
         System.out.println( "NAME = " + name );
         System.out.println( "AGE = " + age );
         System.out.println( "ADDRESS = " + address );
         System.out.println( "SALARY = " + salary );
         System.out.println();
      }
      rs.close();
      stmt.close();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Operation done successfully");
  }
}

上述程序编译和执行时,它会产生以下结果:

Opened database successfully
ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 25000.0

ID = 2
NAME = Allen
AGE = 25
ADDRESS = Texas
SALARY = 15000.0

ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully

DELETE 操作

下面的 Java 代码显示了如何使用 DELETE 语句删除任何记录,然后从 COMPANY 表中获取并显示剩余的记录:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {
    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      stmt = c.createStatement();
      String sql = "DELETE from COMPANY where ID=2;";
      stmt.executeUpdate(sql);
      c.commit();

      ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
      while ( rs.next() ) {
         int id = rs.getInt("id");
         String  name = rs.getString("name");
         int age  = rs.getInt("age");
         String  address = rs.getString("address");
         float salary = rs.getFloat("salary");
         System.out.println( "ID = " + id );
         System.out.println( "NAME = " + name );
         System.out.println( "AGE = " + age );
         System.out.println( "ADDRESS = " + address );
         System.out.println( "SALARY = " + salary );
         System.out.println();
      }
      rs.close();
      stmt.close();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Operation done successfully");
  }
}

上述程序编译和执行时,它会产生以下结果:

Opened database successfully
ID = 1
NAME = Paul
AGE = 32
ADDRESS = California
SALARY = 25000.0

ID = 3
NAME = Teddy
AGE = 23
ADDRESS = Norway
SALARY = 20000.0

ID = 4
NAME = Mark
AGE = 25
ADDRESS = Rich-Mond
SALARY = 65000.0

Operation done successfully
时间: 2024-10-14 12:13:56

SQLite - Java的相关文章

sqlite - java 初学

进来准备使用一种embedded database,即嵌入式数据库,方便随项目本地存储.目前学习打算是sqlite和H2. document:http://www.runoob.com/sqlite/sqlite-java.html 1.连接数据库 添加依赖jdbc: <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <versio

linux上 java 使用 javasqlite

linux上 java 使用 javasqlite http://www.ch-werner.de/javasqlite/ 1) 下载: http://www.ch-werner.de/javasqlite/javasqlite-20140624.tar.gz 2) 解压后进入目录编译 $ cd javasqlite-20140624 $ ./configure $ make 3) 拷贝libsqlite_jni.so到/usr/lib64目录 $ sudo cp .libs/libsqlite

(私人收藏)SQLite 全面手册以及教程

SQLite 全面手册以及教程 https://pan.baidu.com/s/1VKzCWjF79fAiOLjAlx-3zwnfkp SQLite 全面手册以及教程 SQLite 简介 SQLite 安装 SQLite 命令 SQLite 语法 SQLite 数据类型 SQLite 创建数据库 SQLite 附加数据库 SQLite 分离数据库 SQLite 创建表 SQLite 删除表 SQLite Insert 语句 SQLite Select 语句 SQLite 运算符 SQLite 表

MySql &amp; JDBC

1.什么是数据库? 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以通过SQL对数据库中的数据进行增加.修改.删除.及查询操作. 数据库系统类型(历史发展): 网状型数据库 层次型数据库 关系数据库 ---理论最成熟.应用最广泛 面向对象数据库 常见的数据库(软件): MYSQL Oracle DB2 SQLServer SyBase SQLite Java相关: MYSQL  Oracle 2.数据库和表 数据表示存储数据的逻辑单元,可以把数据表想象成

external 里面文件的介绍

搞了半天android,竟然对external下的库一无所知?不能容忍! 马上解决: android-mock:编译为java静态库.说明:Android Mock is a framework for mocking interfaces and classes on the Dalvik VM.资料http://code.google.com/p/android-mock/ antlr:编译为java静态库,开源语法分析器 apache-harmony:放源码 Java SE apache-

java sqlite配置和自定义函数

资源 jetty Jetty Downloads地址 sqlite sqlite JDBC Driver 地址:bitbucket代码托管 和 Github代码托管 jetty配置sqlite 在jetty里的配置(工程MWeb为例) /MWeb/WebContent/WEB-INF/jetty-web.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE Configure PUBLIC &q

关于SimpleCursorAdapter在sqlite数据库中取数据报错 : java.lang.IllegalArgumentException: column &#39;_id&#39; does not exist

_id列不存在 需要创建一个 id列 因为SQLlite数据库 的onCreate方法是在数据库不存在的时候才调用所以我们需要清除一下这个app 的数据 将数据清空 关于SimpleCursorAdapter在sqlite数据库中取数据报错 : java.lang.IllegalArgumentException: column '_id' does not exist

Java连接SQLite数据库

下载java包:sqlite-jdbc-3.7.2.jar,放到java工程目录lib下 如下代码实例: import java.sql.*; import org.sqlite.JDBC; /** * 这是个非常简单的SQLite的Java程序, * 程序中创建数据库.创建表.然后插入数据, * 最后读出数据显示出来 */ public class SqliteTest { public static void main(String[] args) { try { //连接SQLite的JD

【Java】操作Sqlite数据库

首先在https://github.com/xerial/sqlite-jdbc下载jar包 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class MainProcess { public static void main(String[]