Hsqldb 简明教程

HSQLDB - Where Clause

通常,我们使用 SELECT 命令从 HSQLDB 表中获取数据。我们可以使用 WHERE 条件子句来过滤结果数据。使用 WHERE,我们可以指定选择条件以从表中选择所需的记录。

Generally, we use SELECT command to fetch data from HSQLDB table. We can use WHERE conditional clause to filter the resultant data. Using WHERE we can specify the selection criteria to select the required records from a table.

Syntax

以下是 SELECT 命令 WHERE 子句的语法,用于从 HSQLDB 表中获取数据。

Following is the syntax of SELECT command WHERE clause to fetch data from HSQLDB table.

SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....
  1. You can use one or more tables separated by comma to include various conditions using a WHERE clause, but WHERE clause is an optional part of SELECT command.

  2. You can specify any condition using WHERE clause.

  3. You can specify more than one conditions using AND or OR operators.

  4. A WHERE clause can also be used along with DELETE or UPDATE SQL command to specify a condition.

我们可以使用条件来过滤记录数据。我们正在条件 WHERE 子句中使用不同的运算符。此处的运算符列表可与 WHERE 子句一起使用。

We can filter the record data by using conditions. We are using different operators in conditional WHERE clause. Here is the list of operators, which can be used with WHERE clause.

Operator

Description

Example

=

Checks if the values of two operands are equal or not, if yes then the condition becomes true.

(A = B) is not true

!=

Checks if the values of two operands are equal or not, if values are not equal then the condition becomes true.

(A != B) is true

>

Checks if the value of the left operand is greater than the value of the right operand, if yes then the condition becomes true.

(A > B) is not true

<

Checks if the value of the left operand is less than the value of the right operand, if yes then the condition becomes true.

(A < B) is true

>=

Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then the condition becomes true.

(A >= B) is not true

Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then the condition becomes true.

(A ⇐ B) is true

Example

这里有一个示例,该示例检索有关 ID、标题以及标题为“Learn C”一书的作者的详细信息。这可以通过在 SELECT 命令中使用 WHERE 子句来实现。以下是查询。

Here is an example that retrieves the details such as id, title, and the author of the book titled "Learn C". It is possible by using WHERE clause in the SELECT command. Following is the query for the same.

SELECT id, title, author FROM tutorials_tbl WHERE title = 'Learn C';

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

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

+------+----------------+-----------------+
| id   |      title     |    author       |
+------+----------------+-----------------+
| 101  |      Learn C   |   Yaswanth      |
+------+----------------+-----------------+

HSQLDB – JDBC Program

以下 JDBC 程序从表 tutorials_tblhaving 标题 Learn C 中检索记录数据。将以下代码保存到 WhereClause.java 中。

Here is the JDBC program that retrieves the record data from the table tutorials_tblhaving the title Learn C. Save the following code into WhereClause.java.

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

public class WhereClause {

   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
            WHERE title = 'Learn C'");

         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 WhereClause.java
\>java WhereClause

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

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

101 | Learn C | Yaswanth