简体   繁体   中英

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)

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.

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. 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")

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:

{
...
    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.

Since you said the data in 3 Sets will finally go to single row in database. 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. so that later you could set those values by name or index as parameters to your PS.

and I have never seen an insert statement like "Insert into table values (....) where id=123" are u sure it will work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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