gpt4 book ai didi

java - H2:如何创建不带参数的存储过程

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:24 26 4
gpt4 key购买 nike

我正在尝试在使用 Spring(非引导)配置的 JUnit 测试中创建不带参数的存储过程。我从文档和示例中了解到,我必须为静态方法设置别名才能响应存储过程调用。但是,我找不到关于它如何与 out parameters 一起工作的提及。 .静态方法的方法签名中包含什么?

package com.example;

import static java.sql.Types.INTEGER;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.h2.tools.SimpleResultSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { H2tests.TestConfiguration.class })
public class H2tests {

@Autowired
DataSource dataSource;

public static ResultSet executeMyProc(int a) {

return new SimpleResultSet();
}

// what should the type of the second parameter be?
public static ResultSet executeMyProc(int a, int b) {

return new SimpleResultSet();
}

@Test // passes
public void worksWithInParam() throws SQLException {

final CallableStatement stmt = dataSource.getConnection().prepareCall("CALL my_proc(?)");
stmt.setInt(1, 44);
stmt.executeQuery();
}

@Test // fails
public void worksWithOutParam() throws SQLException {

final CallableStatement stmt = dataSource.getConnection().prepareCall("CALL my_proc(?, ?)");
stmt.setInt(1, 999);
stmt.registerOutParameter(2, INTEGER);
stmt.executeQuery();
}

public static class TestConfiguration {

@Bean
public DataSource dataSource() throws SQLException {

final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
final EmbeddedDatabase db = builder //@formatter:off
.setType(EmbeddedDatabaseType.H2)
.setName("MODE=MySQL")
.build(); //@formatter:on
db.getConnection().prepareStatement("CREATE ALIAS my_proc FOR \"com.example.H2tests.executeMyProc\"")
.execute();
return db;
}
}
}

最佳答案

我认为 H2 不支持 out 参数。您将必须通过结果集返回值:

public static ResultSet hello(String name)
{
SimpleResultSet result = new SimpleResultSet();
result.addColumn("GREETING", Types.VARCHAR, 255, 0);
result.addRow("Hello, " + name + "!");
return result;
}

@Test
public void test() throws Exception
{
Connection connection = DriverManager.getConnection("jdbc:h2:mem:");
Statement statement = connection.createStatement();
statement.execute("CREATE ALIAS hello FOR \"DatabaseTest.hello\"");
ResultSet resultSet = statement.executeQuery("CALL hello('Bart')");
assertThat(resultSet.next(), is(true));
assertThat(resultSet.getString("GREETING"), is("Hello, Bart!"));
}

关于java - H2:如何创建不带参数的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com