我可以使用 Apache Commons DBUtils 将记录插入到将 bean 作为值传递的 SQL 表中吗

Posted

技术标签:

【中文标题】我可以使用 Apache Commons DBUtils 将记录插入到将 bean 作为值传递的 SQL 表中吗【英文标题】:Can I Insert Records into a SQL Table passing a bean as values using Apache Commons DBUtils 【发布时间】:2019-12-10 06:09:03 【问题描述】:

我正在使用 Java 访问 MS SQL 服务器。我正在寻找一种无需编写大型 sql 查询即可将数据导入各种表的简单方法。其中一些表有 30 列。

我找到了 apache commons dbutils 库,它可以很好地从表中读取数据并将行映射到 bean。我似乎找不到任何使用相同原理的代码将数据插入到将 bean 作为数据值传递的表中

这可能吗?

【问题讨论】:

你尝试过 jpa/hibernate 吗? 我正在尝试简单地做到这一点,而无需使用类似的框架或 spring 等。 【参考方案1】:

是的,这会很好,但是,我认为这是不可能的。一种方法是在 POJO 类中创建一个静态函数来获取“插入 SQL 语句”。虽然您必须保持两个函数(插入和参数)之间的顺序。

Employee.java

public static String getEmployeeInsertStatement()

    //if you add more fields, increase this integer
    int numberOfParameters = 5;

    String insertion = "INSERT INTO employee\n"
            + "(name\n"
            + ",address\n"
            + ",phone\n"
            + ",comments\n"
            + ",hire_date\n";            

    insertion += ")"
              + "VALUES\n"
              + getParameterQuestionMarks(numberOfParameters);

    return insertion;


// allow for multiple insertions
public static Object[][] getEmployeeParamters(List<Employee> employees)

    List<List<Object>> paramList = new ArrayList<>();

    for (Employee employee : employees)
    
        List<Object> objectList = new ArrayList<>();

        objectList.add(employee.getName());
        objectList.add(employee.getAddress());
        objectList.add(employee.getPhone());
        objectList.add(employee.getComments());
        objectList.add(employee.getHire_date());

        paramList.add(objectList);


    

    return paramList.stream().map(u -> u.toArray(new Object[0])).toArray(Object[][]::new);



public static String getParameterQuestionMarks(int number)

    String paramterMarks = "(";
    for (int x = 1; x <= number; x++)
    
        paramterMarks += "?";
        if ((x + 1) <= number)
        
            paramterMarks += ",";
        
    
    paramterMarks += ")";

    return paramterMarks;

那么在你的 SQLManager 类中,你的 insert 方法是这样的:

public void insertEmployees(List<Employee> employees)

    Connection connection = createConnection();

    try
    
        new QueryRunner().insertBatch(connection, Employee.getEmployeeInsertStatement(), new ScalarHandler<>(), Employee.getEmployeeParameters(employees));


     catch (SQLException ex)
    
        Logger.getLogger(SQLManager.class.getName()).log(Level.SEVERE, null, ex);

     finally
    
        try
        
            DbUtils.close(connection);
         catch (SQLException ex)
        
            Logger.getLogger(SQLManager.class.getName()).log(Level.SEVERE, null, ex);

        

    


【讨论】:

以上是关于我可以使用 Apache Commons DBUtils 将记录插入到将 bean 作为值传递的 SQL 表中吗的主要内容,如果未能解决你的问题,请参考以下文章

java.sql.SQLException: 无法转换为内部表示 -〉java 查询oracle数据库返回错误信息

apache-commons-dbutils 可以将 bean 转换为 SQL 语句吗?

使用 org.apache.commons.validator 验证 IBAN

如何使用 org.apache.commons 包?

我可以使用 Apache Commons DBUtils 将记录插入到将 bean 作为值传递的 SQL 表中吗

如何在没有 IDE 的情况下使用 Apache Commons Lang 代码? (org.apache.commons.lang3)