我试图通过以下方式在Java应用程序中使用HyperSQL:

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

public class Main {

    static Connection conn;
    static Statement stat;

    public static void main(String[] args) {

        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver" );
        } catch (Exception ex) {
            System.out.println("An error occurred while loading HSQLDB JDBC driver: " + ex.getMessage());
            return;
        }

        try {

            conn = DriverManager.getConnection(
                    "jdbc:hsqldb:file:helper_db;sql.syntax_mys=true");

            stat = conn.createStatement();

            stat.executeUpdate(
                "CREATE TABLE IF NOT EXISTS some_table " +
                     "(" +
                        "foo TEXT PRIMARY KEY, " +
                        "bar TEXT" +
                    ");"
            );

            stat.executeUpdate(
                "INSERT INTO some_table VALUES" +
                        "('foo', 'bar') " +
                        "ON DUPLICATE KEY UPDATE some_table = VALUES" +
                        "('foo', 'bar');"
            );

        } catch (Exception ex) {

            System.out.println("An error occurred: " + ex.getMessage());
            return;

        }

    }
}


这段代码为我提供了以下输出:

An error occurred: unexpected token: ON


我究竟做错了什么?如何解决这个问题?

最佳答案

HSQLDB不支持ON DUPLICATE语法(手册中明确记录了该语法)。

您需要使用MERGE来代替,假设您的values子句中至少有一个列是唯一键:

MERGE INTO some_table ut
USING (
  VALUES
    ('foo', 'bar')
) AS md (foo_column, bar_column) ON (ut.foo_column = md.foo_column)
WHEN MATCHED THEN UPDATE
     SET ut.bar_column = md.bar_column
WHEN NOT MATCHED THEN
  INSERT (foo_column, bar_column)
  VALUES (md.foo_column, md.bar_column);


请查看手册以获取更多详细信息:http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_merge_statement

关于java - HyperSQL-意外 token 打开,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18760476/

10-09 04:09