HSQL中的Oracle存储过程

时间:2015-10-08 07:20:17

标签: oracle jdbc hsqldb in-memory-database

我们可以在HSQL DB中执行所有Oracle存储过程吗?

这是我们尝试的Oracle存储过程(test.sql)

CREATE OR REPLACE PROCEDURE Calc_TotalSalary_Bonus (
    p_Salary IN NUMBER,
    p_Bonus IN OUT NUMBER,
    p_TotalSalary OUT NUMBER)
IS 
BEGIN 
    p_Bonus := p_Bonus * p_Salary / 100;
    p_TotalSalary := p_Salary + p_Bonus;
END;

这是我们用于使用HSQLDB执行上述过程的代码 (Sample4.java)

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample4 {
    public static void init() throws SQLException, ClassNotFoundException,
            IOException {

        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        initDatabase();
    }

    private static void initDatabase() throws SQLException, IOException {
        try (Connection connection = getConnection();
                Statement statement = connection.createStatement();) {
            for (String sql : getSqlInitStatements().split(";;")) {
                if (sql.length() > 0) {
                    try {
                        statement.execute(sql);
                    } catch (Exception e) {
                        System.err.println("Failed to execute SQL statement "
                                + sql);
                        throw e;
                    }
                }
            }

        }

    }

    private static Connection getConnection() throws SQLException {

        return DriverManager.getConnection(
                "jdbc:hsqldb:mem:mymemdb;sql.syntax_ora=true", "SA", "");

    }

    public static String getSqlInitStatements() throws IOException {
        FileInputStream fis = new FileInputStream("test.sql");

        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String line = null;
        String lineslist = "";
        while ((line = br.readLine()) != null) {
            lineslist = lineslist + "\n" + line;
        }
        br.close();
        return lineslist;
    }

    public static void main(String[] args) throws IOException, SQLException {
        Sample4.initDatabase();
    }

}

这是我们执行

时得到的例外
Exception in thread "main" java.sql.SQLSyntaxErrorException: unexpected token: IN : line: 2
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
    at plsqlSample.Sample4.initDatabase(Sample4.java:40)
    at plsqlSample.Sample4.main(Sample4.java:87)
Caused by: org.hsqldb.HsqlException: unexpected token: IN : line: 2
    at org.hsqldb.error.Error.parseError(Unknown Source)
    at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
    at org.hsqldb.ParserBase.checkIsNonCoreReservedIdentifier(Unknown Source)
    at org.hsqldb.ParserDQL.checkIsSchemaObjectName(Unknown Source)
    at org.hsqldb.ParserDQL.readTypeDefinition(Unknown Source)
    at org.hsqldb.ParserRoutine.readRoutineParameter(Unknown Source)
    at org.hsqldb.ParserRoutine.compileCreateProcedureOrFunction(Unknown Source)
    at org.hsqldb.ParserDDL.compileCreate(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 4 more

如果Hsqldb无法执行Oracle存储过程,那么内存数据库中是否还有其他可以执行此操作的程序。

0 个答案:

没有答案