黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

SQLite Java 編程接口

sqlite java 編程接口

 

1. 安裝

在 java 程序中使用 sqlite 之前,我們需要確保機(jī)器上已經(jīng)有 sqlite jdbc driver 驅(qū)動(dòng)程序和 java。可以查看 java 教程了解如何在計(jì)算機(jī)上安裝 java?,F(xiàn)在,我們來(lái)看看如何在機(jī)器上安裝 sqlite jdbc 驅(qū)動(dòng)程序。

最新 sqlite-jdbc-(version).jar 版本可以訪問(wèn) https://github.com/xerial/sqlite-jdbc/releases 下載。

在您的 class 路徑中添加下載的 jar 文件 sqlite-jdbc-(version).jar,或者在 -classpath 選項(xiàng)中使用它,這將在后面的范例中進(jìn)行講解。

在學(xué)習(xí)下面部分的知識(shí)之前,您必須對(duì) java jdbc 概念有初步了解。如果您還未了解相關(guān)知識(shí),那么建議您可以先花半個(gè)小時(shí)學(xué)習(xí)下 jdbc 教程相關(guān)知識(shí),這將有助于您學(xué)習(xí)接下來(lái)講解的知識(shí)。

 

2. 連接數(shù)據(jù)庫(kù)

下面的 java 程序顯示了如何連接到一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)。如果數(shù)據(jù)庫(kù)不存在,那么它就會(huì)被創(chuàng)建,最后將返回一個(gè)數(shù)據(jù)庫(kù)對(duì)象。

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");
  }
}

現(xiàn)在,讓我們來(lái)編譯和運(yùn)行上面的程序,在當(dāng)前目錄中創(chuàng)建我們的數(shù)據(jù)庫(kù) test.db。您可以根據(jù)需要改變路徑。我們假設(shè)當(dāng)前路徑下可用的 jdbc 驅(qū)動(dòng)程序的版本是 sqlite-jdbc-3.7.2.jar。

$javac sqlitejdbc.java
$java -classpath ".:sqlite-jdbc-3.7.2.jar" sqlitejdbc
open database successfully

如果您想要使用 windows 機(jī)器,可以按照下列所示編譯和運(yùn)行您的代碼:

$javac sqlitejdbc.java
$java -classpath ".;sqlite-jdbc-3.7.2.jar" sqlitejdbc
opened database successfully

 

3. 創(chuàng)建表

下面的 java 程序?qū)⒂糜谠谙惹皠?chuàng)建的數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)表:

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");
  }
}

上述程序編譯和執(zhí)行時(shí),它會(huì)在 test.db 中創(chuàng)建 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 代碼顯示了如何在上面創(chuàng)建的 company 表中創(chuàng)建記錄:

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");
  }
}

上述程序編譯和執(zhí)行時(shí),它會(huì)在 company 表中創(chuàng)建給定記錄,并會(huì)顯示以下兩行:

opened database successfully
records created successfully

 

5. select 操作

下面的 java 程序顯示了如何從前面創(chuàng)建的 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");
  }
}

上述程序編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生以下結(jié)果:

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 語(yǔ)句來(lái)更新任何記錄,然后從 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");
  }
}

上述程序編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生以下結(jié)果:

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 語(yǔ)句刪除任何記錄,然后從 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");
  }
}

上述程序編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生以下結(jié)果:

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

下一節(jié):sqlite php 編程接口

sqlite教程

相關(guān)文章
學(xué)習(xí)SQLite