如何使用反射API将数据插入H2数据库

问题描述 投票:0回答:1

我有一个用@PrimaryKey和@Column修饰的Person类

    public class Person {

    @PrimaryKey(name = "p_id")
    private Integer id;

    @Column(name = "c_name")
    private String name;

    @Column(name = "c_age")
    private Integer age;
    }

以下是持久方法:

   public void persist(T t) throws IllegalArgumentException, IllegalAccessException, SQLException {
    // TODO Auto-generated method stub
    MetaModel metaModel = MetaModel.of(t.getClass());
    String sql = metaModel.buildInsertRequest();

    try(PreparedStatement statement = prepareStatementWith(sql).andParameters(t)){
        statement.executeUpdate();  
    }

}

    private PreparedStatementWrapper prepareStatementWith(String sql) throws SQLException {
    // TODO Auto-generated method stub

    Connection con = buildConnection();
    PreparedStatement statement = con.prepareStatement(sql);

    return new PreparedStatementWrapper(statement);
}

    private class PreparedStatementWrapper{

    private PreparedStatement statement;
    public PreparedStatementWrapper(PreparedStatement statement) {
        // TODO Auto-generated constructor stub
         this.statement = statement;
    }

这里。返回sql查询

   public String buildInsertRequest() {
    // insert into Person(id, name, age) values(?, ?, ?)
    String pkColumnName = getPrimaryKey().getName();
    List<String> columNames = getColumn().stream().map(c -> c.getName()).collect(Collectors.toList());
    columNames.add(0, pkColumnName);
    String columnElement = String.join(", ", columNames);

    int numberOfColumns = getColumn().size() + 1;
    String questionMarkElement = IntStream.range(0, numberOfColumns)
                                            .mapToObj(index -> "?")
                                            .collect(Collectors.joining(", "));
    return "INSERT into " + this.clss.getSimpleName() + "(" + columnElement + ") values (" + questionMarkElement + ")";
}

   Here adding the parameter

   public PreparedStatement andParameters(T t) throws SQLException, IllegalArgumentException, IllegalAccessException {
        // TODO Auto-generated method stub

        MetaModel metaModel = MetaModel.of(t.getClass());
        Class<?> pkType = metaModel.getPrimaryKey().getType();
        if(pkType == Integer.class) {
            Integer id = idGenerator.incrementAndGet();
            statement.setInt(1, id);    
            Field field = metaModel.getPrimaryKey().getField();
            field.setAccessible(true);
            field.set(t, id);
        }
        for(int columnIndex = 0; columnIndex < metaModel.getColumn().size(); columnIndex++) {
            ColumnField columnField = (ColumnField) metaModel.getColumn().get(columnIndex);
            Class<?> fieldType = columnField.getType();
            Field field = columnField.getField();
            field.setAccessible(true);
            Object value = field.get(t);
            if(fieldType == Integer.class) {
                statement.setInt(columnIndex + 2,(Integer) value);
            } else if(fieldType == String.class) {
                statement.setString(columnIndex + 1, (String)value);
            }
        }
        return statement;
    }

我在prepareStatementWith(sql)方法中遇到错误,它没有重新调用新的PreparedStatementWrapper,而是引发了异常线程“ main” org.h2.jdbc.JdbcSQLException中的异常:未找到表“ PERSON”; SQL语句:插入Person(p_id,c_name,c_age)值(?,?,?)

以下是连接详细信息:

    public Connection buildConnection() throws SQLException {
    Connection con = 
            DriverManager.getConnection("jdbc:h2:C:\\Users\\biplab.musib\\eclipse-workspace\\ReflectionProject\\db-files\\db-files", "sa", "");
    return con;

H2提示中的JDBC网址:jdbc:h2:C:\ Users \ biplab.musib \ eclipse-workspace \ ReflectionProject \ db-files \ db-reflection

有人可以在这里帮我吗

java hibernate jdbc reflection h2
1个回答
0
投票

对我来说,将连接URL更改为与H2数据库连接URL相同:

Connection connection = DriverManager.getConnection(“ jdbc:h2:C:\ Users \ PUSHPENDRA KUMAR \ IdeaProjects \ PlayWithReflection \ dbfiles \ db-pluralsight”,“ sa”,“”);

© www.soinside.com 2019 - 2024. All rights reserved.