简体   繁体   English

使用JDBC的简单Java插入

[英]Simple Java Insert using JDBC

My Database Table Schema is something like 我的数据库表架构类似于

DocumentID: Name1: Name2: Name3: Name4: Location1: Location2: Location3....:Organization1:..

Now I have 3 Hashset's available having the above values (ie one for name, one for location and one for organization) 现在,我有3个具有上述值的哈希集(即,一个用于名称,一个用于位置,一个用于组织)

In each single iteration of loop these hashset are being populated with above values. 在循环的每个单次迭代中,这些哈希集都填充有以上值。

At the end of each iteration the data from these hashset's is removed and new one's are created. 在每次迭代结束时,将删除这些哈希集的数据,并创建新的哈希集。

Now my problem is at each iteration I have to populate the sql table row (just 1 row each iteration) from these hashset values. 现在我的问题是,在每次迭代中,我必须从这些哈希集值中填充sql表行(每次迭代仅1行)。

What I am not able to understand is if I have hard coded strings than simply I can use something like: 我无法理解的是,如果我有硬编码的字符串,而不是简单地可以使用类似以下内容的字符串:

String sql = "INSERT INTO Table " +
               "VALUES ('100', 'Zara', 'Akli', '18','100', 'Zara', 'Ali', '18')";

However I need to iterate through each hashset and insert (something like above) the data of all 3 hashset's in a single row. 但是,我需要遍历每个哈希集并将所有3个哈希集的数据插入到同一行中。 I am confused of how to write such statement. 我对如何写这样的陈述感到困惑。 Remember my table is initially completely empty so I cant't use the where clause (something like "insert into.....where documentID="23423") 请记住,我的表最初是完全空的,所以我不能使用where子句(诸如“插入..... where documentID =“ 23423”之类的东西)

Assuming you have created these Sets these way: 假设您通过以下方式创建了这些集合:

long DocumentId
names {"A", "B", "C"}
location {"1", "2", "3"}
and so on...

I think the easiest is to build dinamically the SQL to execute: 我认为最简单的方法是动态创建要执行的SQL:

{
...
    StringBuilder sb = new StringBuilder("insert into mytable (");
    List<Object> params = new ArrayList<Object>();

    addColumnAndValue(sb, "DocumentID", docIdYouHaveSomewhere, params);
    int i = 0;
    for (String name: names)
       addColumnAndValue(sb, ",name" + i, name, params);
    i = 0;
    for (String location: locations)
       addColumnAndValue(sb, ",location" + i, location, params);
    // now close the thing
    sb.append(") values (?");
    for (i = 1; i<params.size(); i++)
       sb.append(",?");
    sb.append("=");

    // and now sb.toString() will contain valid SQL and the param values for a PreparedStatement will be in params array-list:

    PreparedStatement ps = conn.prepareStatement(sb.toString());
    for (i=0; i<params.size(); i++)
       ps.setObject(i+1, params.get(i));

    ps.executeUpdate(); // voi là!
    ...

}

private void add addColumnAndValue(StringBuilder sb, String columnName, Object value, List<Object> params) {
   sb.append(columnName);
   params.add(value);
}

i guess you need to first do some work on your 3 "HashSet"s. 我想您需要首先对3个“ HashSet”进行一些工作。

Since you said the data in 3 Sets will finally go to single row in database. 正如您所说的,“ 3组数据”最终将进入数据库的单行。 so I suggest that convert your 3 hashset into a Map, or at least a List, with same order as the fields in your insert statement. 因此,我建议您将3个哈希集转换为Map或至少一个List,其顺序与insert语句中的字段相同。 so that later you could set those values by name or index as parameters to your PS. 这样以后您就可以通过名称或索引将这些值设置为PS的参数。

and I have never seen an insert statement like "Insert into table values (....) where id=123" are u sure it will work? 而且我从未见过像"Insert into table values (....) where id=123"这样的插入语句,您确定它会起作用吗?

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

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