Stored Procedure in H2 Database

后端 未结 4 2061
旧时难觅i
旧时难觅i 2020-12-06 10:44

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:

ht

4条回答
  •  失恋的感觉
    2020-12-06 11:26

    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));
    

    Console: Version: 1.4.191

    Addendum: The feature is not limited to functions; aliased methods can execute arbitrary Java code. For example, the query() method defined in Function.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));
    }
    

    Console: User: SA

    Note that cli.Function.query is a copy of org.h2.samples.Function.query.

提交回复
热议问题