Hsqldb 简明教程

HSQLDB - Insert Query

可以使用 INSERT INTO 命令在 HSQLDB 中执行插入查询语句。你必须按照表中列字段的顺序提供用户定义的数据。

You can achieve Insert query statement in HSQLDB by using the INSERT INTO command. You have to provide the user-defined data following the column field order from the table.

Syntax

以下是 INSERT 查询的一般语法。

Following is the generic syntax to INSERT a query.

INSERT INTO table_name (field1, field2,...fieldN)
VALUES (value1, value2,...valueN );

若要将字符串类型数据插入表中,你必须使用双引号或单引号在插入查询语句中提供字符串值。

To insert a string type data into a table, you will have to use double or single quotes to provide string value into the insert query statement.

Example

我们考虑一个示例,其中将记录插入一个名为 tutorials_tbl 的表中,其中值 id = 100、title = Learn PHP、Author = John Poul,并且提交日期是当前日期。

Let us consider an example that inserts a record into a table named tutorials_tbl with the values id = 100, title = Learn PHP, Author = John Poul, and the submission date is current date.

以下是给定示例的查询。

Following is the query for the given example.

INSERT INTO tutorials_tbl VALUES (100,'Learn PHP', 'John Poul', NOW());

执行以上查询后,您将收到以下输出 −

After execution of the above query, you will receive the following output −

1 row effected

HSQLDB – JDBC Program

以下是使用给定值(id =100、title = Learn PHP、Author = John Poul 且提交日期为当前日期)将记录插入表中的 JDBC 程序。查看给定程序。将代码另存为 InserQuery.java 文件。

Here is the JDBC program to insert the record into the table with the given values, id =100, title = Learn PHP, Author = John Poul, and the submission date is current date. Take a look at the given program. Save the code into the InserQuery.java file.

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

public class InsertQuery {
   public static void main(String[] args) {
      Connection con = null;
      Statement stmt = null;
      int result = 0;
      try {
         Class.forName("org.hsqldb.jdbc.JDBCDriver");
         con = DriverManager.getConnection( "jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
         stmt = con.createStatement();
         result = stmt.executeUpdate("INSERT INTO tutorials_tbl
            VALUES (100,'Learn PHP', 'John Poul', NOW())");
         con.commit();
      }catch (Exception e) {
         e.printStackTrace(System.out);
      }
      System.out.println(result+" rows effected");
      System.out.println("Rows inserted successfully");
   }
}

您可以使用以下命令启动数据库。

You can start the database using the following command.

\>cd C:\hsqldb-2.3.4\hsqldb
hsqldb>java -classpath lib/hsqldb.jar org.hsqldb.server.Server --database.0
file:hsqldb/demodb --dbname.0 testdb

使用以下命令编译并执行以上程序。

Compile and execute the above program using the following command.

\>javac InsertQuery.java
\>java InsertQuery

在执行上述命令之后,您将收到以下输出−

After execution of the above command, you will receive the following output −

1 rows effected
Rows inserted successfully

尝试使用 INSERT INTO 命令将以下记录插入到 tutorials_tbl 表中。

Try to insert the following records into the tutorials_tbl table by using the INSERT INTO command.

Id

Title

Author

Submission Date

101

Learn C

Yaswanth

Now()

102

Learn MySQL

Abdul S

Now()

103

Learn Excell

Bavya kanna

Now()

104

Learn JDB

Ajith kumar

Now()

105

Learn Junit

Sathya Murthi

Now()