Hsqldb 简明教程

HSQLDB - Select Query

SELECT 命令用于从 HSQLDB 数据库中获取记录数据。在此处,你需要在 Select 语句中提及所需字段列表。

The SELECT command is used to fetch the record data from HSQLDB database. Here, you need to mention the required fields list in the Select statement.

Syntax

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

Here is the generic syntax for Select query.

SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE Clause]
[OFFSET M ][LIMIT N]
  1. You can fetch one or more fields in a single SELECT command.

  2. You can specify star (*) in place of fields. In this case, SELECT will return all the fields.

  3. You can specify any condition using WHERE clause.

  4. You can specify an offset using OFFSET from where SELECT will start returning records. By default, offset is zero.

  5. You can limit the number of returns using LIMIT attribute.

Example

以下是一个示例,其中获取 tutorials_tbl 表中所有记录的 id、title 和 author 字段。我们可以使用 SELECT 语句来实现该操作。以下是示例的查询。

Here is an example that fetches id, title, and author fields of all records from tutorials_tbl table. We can achieve this by using the SELECT statement. Following is the query for the example.

SELECT id, title, author FROM tutorials_tbl

在执行上述查询后,你将收到以下输出。

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

+------+----------------+-----------------+
|  id  |      title     |    author       |
+------+----------------+-----------------+
| 100  |     Learn PHP  |    John Poul    |
| 101  |     Learn C    |    Yaswanth     |
| 102  |   Learn MySQL  |     Abdul S     |
| 103  |   Learn Excell |   Bavya kanna   |
| 104  |   Learn JDB    |    Ajith kumar  |
| 105  |   Learn Junit  |   Sathya Murthi |
+------+----------------+-----------------+

HSQLDB – JDBC Program

以下是一个 JDBC 程序,其中将获取 tutorials_tbl 表中所有记录的 id、title 和 author 字段。将以下代码另存为 SelectQuery.java 文件。

Here is the JDBC program that will fetch id, title, and author fields of all records from tutorials_tbl table. Save the following code into the SelectQuery.java file.

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

public class SelectQuery {

   public static void main(String[] args) {
      Connection con = null;
      Statement stmt = null;
      ResultSet result = null;

      try {
         Class.forName("org.hsqldb.jdbc.JDBCDriver");
         con = DriverManager.getConnection(
            "jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
         stmt = con.createStatement();
         result = stmt.executeQuery(
            "SELECT id, title, author FROM tutorials_tbl");

         while(result.next()){
            System.out.println(result.getInt("id")+" | "+
               result.getString("title")+" | "+
               result.getString("author"));
         }
      } catch (Exception e) {
         e.printStackTrace(System.out);
      }
   }
}

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

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 code using the following command.

\>javac SelectQuery.java
\>java SelectQuery

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

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

100 | Learn PHP | John Poul
101 | Learn C | Yaswanth
102 | Learn MySQL | Abdul S
103 | Learn Excell | Bavya Kanna
104 | Learn JDB | Ajith kumar
105 | Learn Junit | Sathya Murthi