H2数据库中的存储过程 [英] Stored Procedure in H2 Database
问题描述
我是数据库新手,最近开始为H2数据库编写测试用例。
我想知道如何在Eclipse中测试存储过程。我见过以下内容:
I am new to database and recently started writing test cases for H2 database. I want to know how to test a stored procedure in Eclipse. I have seen the following:
http://www.h2database.com/html/features.html#user_defined_functions
h2database链接中给出的示例代码,
The sample code given in the h2database link,
"CREATE ALIAS NEXT_PRIME AS $$
String nextPrime(String value) {
return new BigInteger(value).nextProbablePrime().toString();
}
$$;
"
-
这应该在哪里宣布?以及如何运作?
Where should this be declared?and how to run it?
PS - 我有H2 JAR文件并正在测试它。
PS - I have the H2 JAR file and am testing it.
如果有人可以告诉我如何用Java编写一个简单的存储过程H2,它会有很大的帮助。
If someone can tell me how to write a simple stored procedure in Java for H2, it would be of great help.
H2中还有以下任何等价物吗?
Also is there any equivalent of the following in H2?
开始dbms_output?
"begin dbms_output" ?
谢谢。
推荐答案
你可以忽略了
src / test / org / h2 / samples / Function.java
中的示例。这是一个相关的例子:You may have overlooked the examples in
src/test/org/h2/samples/Function.java
. Here's a related example: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));
控制台:
版本:1.4.191
附录:该功能不仅限于功能;别名方法可以执行任意 Java代码。例如, query()方法org / h2 / samples / Function.javarel =nofollow noreferrer>
Function.java
可能有别名并被调用,如下所示:Addendum: The feature is not limited to functions; aliased methods can execute arbitrary Java code. For example, the
query()
method defined inFunction.java
may be aliased and called as shown below: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)); }
控制台:
用户:SA
请注意
cli.Function.query
是org.h2的副本.samples.Function.query
。这篇关于H2数据库中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!