16

我是数据库新手,最近开始为 H2 数据库编写测试用例。我想知道如何在 Eclipse 中测试存储过程。我看过以下内容:

http://www.h2database.com/html/features.html#user_defined_functions

如何在 H2 中创建程序

h2database链接中给出的示例代码,

"CREATE ALIAS NEXT_PRIME AS $$
String nextPrime(String value) {
    return new BigInteger(value).nextProbablePrime().toString();
}
$$;
" 
  • 这应该在哪里声明?如何运行它?

PS - 我有 H2 JAR 文件并正在测试它。

如果有人能告诉我如何用 Java 为 H2 编写一个简单的存储过程,那将有很大帮助。

H2中是否还有以下等效项?

“开始 dbms_output”?

谢谢。

4

5 回答 5

12

H2数据库中没有存储过程和sql用户定义函数,我们使用java方法并创建一个别名来引用它。我们可以使用别名调用该方法。

下面是一个简单的例子:**

DROP ALIAS IF EXISTS MYFUNCTION;
CREATE ALIAS MYFUNCTION AS $$
String getTableContent(java.sql.Connection con) throws Exception {
    String resultValue=null;
    java.sql.ResultSet rs = con.createStatement().executeQuery(
    " SELECT * FROM TABLE_NAME");
       while(rs.next())
       {
        resultValue=rs.getString(1);
       }
    return resultValue;
}
$$;
于 2014-09-23T10:13:39.757 回答
12

您可能忽略了 中的示例src/test/org/h2/samples/Function.java。这是一个相关的例子:

Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
Statement st = conn.createStatement();
st.execute("CREATE ALIAS getVersion FOR \"org.h2.engine.Constants.getVersion\"");
ResultSet rs;
rs = st.executeQuery("CALL getVersion()");
if (rs.next()) System.out.println("Version: " + rs.getString(1));

安慰:Version: 1.4.191

附录:特性不限于功能;别名方法可以执行任意Java 代码。例如,中query()定义的方法Function.java可能会被别名和调用,如下所示:

Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
Statement st = conn.createStatement();
st.execute("CREATE ALIAS query FOR \"cli.Function.query\"");
rs = st.executeQuery("CALL query('SELECT NAME FROM INFORMATION_SCHEMA.USERS')");
while (rs.next()) {
    System.out.println("User: " + rs.getString(1));
}

安慰:User: SA

请注意,这cli.Function.queryorg.h2.samples.Function.query.

于 2012-08-07T06:21:05.433 回答
2

以下是我们过去在项目中实施的方式。这可能会有所帮助:)

package com.procedures;

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

public class CRITICAL_ACTIONS {

    public static final int SAVE_ACTION(Connection connection) throws SQLException {
        try {
            Statement statement = connection.createStatement();
            return statement.executeUpdate("INSERT INTO SCHEMA1.CRITICAL_ACTIONS(COLLEAGUE_ID,JOURNEY_ID,TYPE,PRODUCT,DESCRIPTION,META_DATA,STATUS) values('12345',11111111,'ABC','Lloyds','hellow','hello','START')");
        } finally {
            //connection.close();
        }
    }

    public static final ResultSet FETCH_ACTION(Connection connection) throws SQLException {
        try {
            Statement statement = connection.createStatement();
            return statement.executeQuery("SELECT * FROM SCHEMA1.CRITICAL_ACTIONS");
        }finally {
            connection.close();
        }
    }


}

在 Java 中调用 H2 Java 存储过程:-

    jdbcTemplate.update("CREATE ALIAS SAVE_ACTION FOR \"com.procedures.CRITICAL_ACTIONS.SAVE_ACTION\"");
    jdbcTemplate.update("CREATE ALIAS FETCH_ACTION FOR \"com.procedures.CRITICAL_ACTIONS.FETCH_ACTION\"");

    jdbcTemplate.getDataSource().getConnection().createStatement().execute("call SAVE_ACTION()");
于 2018-08-06T14:02:35.160 回答
1

H2数据库中的存储过程和java方法一样,所以写java方法,可以使用别名调用。

于 2015-01-12T09:23:48.650 回答
0
H2 DB is not supporting for storedprocs. In place of stored proc we have to create function which returns some output jst like storedproc as we did using registerInOut params. For example,

if ur QueryConst is like this:
public static final String INSERT_EMPLOYEE = "{call INSERT_EMPLOYEE(?,?,?)}";

then,

In our schema.sql(which executes @Test before)
DROP ALIAS IF EXISTS INSERT_EMPLOYEE;
CREATE ALIAS INSERT_EMPLOYEE FOR "com.test.EmployeeDaoImplTest.updateEmpStoredproc";


package com.test;
@ContextConfiguration(locations = { "classpath:configxmltest.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@Sql(scripts = { "classpath:schema.sql" }, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public class EmployeeDaoImplTest {
    
    public static final String INSERT_EMPLOYEE = "{call INSERT_EMPLOYEE(?,?,?)}";


    @Autowired
    EmployeeDaoImpl employeeDaoTest;
    
    and other dependencies....(if any)

    @Test
    public void testUpdateEmployee() {
        ..ur logic if any input data settings
        assertEquals("Inserted Successfully", employeeDaoTest.updateEmployee(input, INSERT_EMPLOYEE));
    }

    public static ResultSet updateEmpStoredproc(String name, String w, Integer i) throws SQLException {
        SimpleResultSet rs = new SimpleResultSet();
        rs.addColumn("input", Types.VARCHAR, 255, 0);
        rs.addColumn("error", Types.VARCHAR, 255, 0);
        rs.addColumn("count", Types.INTEGER, 10, 0);
        rs.addRow(0, "Inserted Successfully");
        rs.addRow(1, 10);
        return rs;
    }
  }
于 2021-04-16T11:06:38.557 回答