简体   繁体   English

Java JDBC通用SQL插入功能

[英]Java JDBC General SQL insert function

I am writing a generic function to insert data into SQLtables and I am wondering how to improve my current implementation. 我正在编写一个通用函数以将数据插入SQLtables,并且想知道如何改进当前的实现。

My current function looks like: 我当前的功能如下:

public void insertIntoDatabase(String table, ArrayList<String> insertRow) {
..
}

table - Name of the SQL table insertRow - ArrayList of values to insert table-SQL表的名称insertRow-要插入的值的ArrayList

Some tables also contain other attributes than VARCHARS so I am considering ArrayList<xtable> insertRow But is it necessary to write individual java classes for each database table ? 有些表还包含除VARCHARS以外的其他属性,因此我正在考虑ArrayList<xtable> insertRow但是是否有必要为每个数据库表编写单独的Java类?

Thanks for your help! 谢谢你的帮助! M 中号

You can't bind table or column names in PreparedStatement . 您不能在PreparedStatement绑定表或列的名称。

But it is possible to write a generic DAO: 但是可以编写一个通用的DAO:

package persistence;

public interface GenericDao<K, V> {
    List<V> find();
    V find(K id);
    K save(V value);
    void update(V value);
    void delete(V value);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM