Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 将Spring Boot Postgres转换为H2_Spring Boot_H2 - Fatal编程技术网

Spring boot 将Spring Boot Postgres转换为H2

Spring boot 将Spring Boot Postgres转换为H2,spring-boot,h2,Spring Boot,H2,我想开始运行H2作为一个嵌入式数据库在我的API层,这是非常轻的重量。我遇到了一个似乎找不到太多的错误,除了使用H2而不是postgres的SQL语法之外,还有什么主要的变化: 16 9:27:41 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw except

我想开始运行H2作为一个嵌入式数据库在我的API层,这是非常轻的重量。我遇到了一个似乎找不到太多的错误,除了使用H2而不是postgres的SQL语法之外,还有什么主要的变化:

16 9:27:41 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Invalid value "1000" for parameter "resultSetHoldability" [90008-193]] with root cause
org.h2.jdbc.JdbcSQLException: Invalid value "1000" for parameter "resultSetHoldability" [90008-193]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.getInvalidValueException(DbException.java:228)
    at org.h2.jdbc.JdbcConnection.checkHoldability(JdbcConnection.java:1445)
    at org.h2.jdbc.JdbcConnection.createStatement(JdbcConnection.java:261)
    at com.project.x.api.utils.PsqlUtils.getStatement(PsqlUtils.java:146)
    at com.project.x.api.utils.PsqlUtils.sqltoList(PsqlUtils.java:51)
    at com.project.x.api.utils.PsqlUtils.sqltoJson(PsqlUtils.java:79)
    at com.project.x.api.dao.UserDao.getUserCnt(UserDao.java:192)
以下是我的类PsqlUtils:

@Component
public class PsqlUtils {

    final static Logger log = LoggerFactory.getLogger(PsqlUtils.class);

    //Config
    private final String db_url;
    private final String db_username;
    private final String db_password;
    @Autowired
    public PsqlUtils(@Value("${db.url}") String db_url, @Value("${db.username}") String db_username, @Value("${db.password}") String db_password) {
        this.db_url = db_url;
        this.db_username = db_username;
        this.db_password = db_password;
    }



    public List<Map<String, Object>> sqltoList(final String sql) {
        log.debug("SqltoJson sql : {}", sql);
        String jsonData;

        Connection connection;
        Statement statement;
        ResultSet resultSet;
        final List<Map<String, Object>> objList = new ArrayList<>();

        try  {
            connection = getConnection();
            connection.setAutoCommit(false);
            statement = getStatement(connection);
            resultSet = executeQuery(statement, sql);

            final int columnCount = resultSet.getMetaData().getColumnCount();

            while (resultSet.next()) {
                final Map<String, Object> rowData = new HashMap<>();
                for (int column = 1; column <= columnCount; ++column) {
                    rowData.put(resultSet.getMetaData().getColumnName(column), resultSet.getObject(column));
                }
                objList.add(rowData);
            }

            resultSet.close();
            statement.close();
            connection.close();


        }catch (SQLException | IOException e){
            throw new RuntimeException(e.getMessage(), e);
        }
        return objList;
    }

    public String sqltoJson(final String sql) throws SQLException {
        log.debug("SqltoJson sql : {}", sql);
        String jsonData;

        List<Map<String, Object>> objList = sqltoList(sql);

        if (!objList.isEmpty()) {
            final ObjectMapper mapper = new ObjectMapper();
            try {
                jsonData = mapper.writeValueAsString(objList);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        } else {
            jsonData = "[]";
        }



        return jsonData;
    }

    public String sqltoJsonObject(final String sql) throws SQLException {
        log.debug("SqltoJson sql : {}", sql);

        String jsonData;

        Connection connection;
        Statement statement;
        ResultSet resultSet;

        try  {
            connection = getConnection();
            connection.setAutoCommit(false);
            statement = getStatement(connection);
            resultSet = executeQuery(statement, sql);

            final int columnCount = resultSet.getMetaData().getColumnCount();
            final List<Map<String, Object>> objList = new ArrayList<>();

            resultSet.next();
            final Map<String, Object> rowData = new HashMap<>();
            for (int column = 1; column <= columnCount; ++column) {
                rowData.put(resultSet.getMetaData().getColumnName(column), resultSet.getObject(column));
            }
            objList.add(rowData);

            if (!objList.isEmpty()) {
                final ObjectMapper mapper = new ObjectMapper();
                jsonData = mapper.writeValueAsString(rowData);
            } else {
                jsonData = "[]";
            }


        } catch (SQLException | IOException e){
            throw new RuntimeException(e.getMessage(), e);
        }
        return jsonData;
    }

    public Connection getConnection() throws IOException, SQLException {

        //final Properties props = readProperties(propertiesPath);
        final Connection connection = DriverManager.getConnection(db_url, db_username, db_password);
        connection.setAutoCommit(false);
        return connection;
    }


    public Statement getStatement(Connection connection) throws SQLException {
        final Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.FETCH_FORWARD);
        statement.setFetchSize(0);
        return statement;
    }

    public ResultSet executeQuery(Statement statement, String query) throws SQLException {
        log.debug("sql : {}", query);
        return statement.executeQuery(query);
    }

    public Statement getStatementExecute(Connection connection) throws SQLException {
        return connection.createStatement();
    }
}
在H2控制台上,SQL工作正常:

select count("user_id") cnt from "user";
尝试使用:

要使用PostgreSQL模式,请使用数据库URL
jdbc:h2:~/test;MODE=PostgreSQL
或SQL语句
设置模式PostgreSQL


同样的问题仍然存在:db.driver=org.h2.driver db.username=sa db.password=db.url=jdbc:h2:~/test;模式=PostgreSQL
select count("user_id") cnt from "user";