SQLite Java 编程接口

sqlite java 编程接口

 

1. 安装

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

最新 sqlite-jdbc-(version).jar 版本可以访问 https://github.com/xerial/sqlite-jdbc/releases 下载。

在您的 class 路径中添加下载的 jar 文件 sqlite-jdbc-(version).jar,或者在 -classpath 选项中使用它,这将在后面的范例中进行讲解。

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

 

2. 连接数据库

下面的 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

 

3. 创建表

下面的 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

 

4. 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

 

5. 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

 

6. 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

 

7. 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

下一节:sqlite php 编程接口

sqlite教程

相关文章
学习SQLite