Insert data into a lot of columns in java JDBC?

Get the key set for the HashMap. Iterate that key set to build a String containing your insert statement. Use the resulting String to create a PreparedStatement.

Then iterate that key set again to set parameters by name using the Objects you retrieve from the HashMap.

Get the key set for the HashMap. Iterate that key set to build a String containing your insert statement. Use the resulting String to create a PreparedStatement.

Then iterate that key set again to set parameters by name using the Objects you retrieve from the HashMap. You might have to write a few extra lines of special-case code if any of your values are of a Class that the JDBC driver isn't sure how to map.

2 Few things that seem a bit wrong to me: 1. Keys aren't ordered in any way in a HashMap, so you won't know which one is the first you'll get; and PreparedStatement only allows you to set its parameters based on index values; 2. Column names have no additional meta information about its types, so I'm unsure if calling setObject for every parameter will work.

– darioo Nov 6 '11 at 19:14 Does iterating in KeySet of a HashMap always in one order? I mean if I get create column portion of SQL insert code, has the values the same order? – Snigger Nov 6 '11 at 19:14 +1 to darioo .. you are absolutely correct on both counts.

Changes I would make would be to use the key set to instantiate an ArrayList and then use that to both build the PreparedStatement's sql string, and then set parameters by index. – RichW Nov 6 '11 at 19:21 1 @darioo: 1) the unordered nature of a HashMap can be suppressed by constructing a LinkedHashMap based on it. 2) calling setObject() will definitely work if you use the appropriate value type, e.g. String, Integer, Date, etc. The JDBC driver wil do the type checking.

– BalusC Nov 6 '11 at 19:22 @BalusC: thanks for clearing that up. Although I wouldn't feel too safe using that approach since your intentions aren't clearly expressed code wise and this might lead to some hard to find bugs in the future if one isn't careful enough. – darioo Nov 6 '11 at 19:25.

I'd suggest you bite the dust and simply write a method that will do the dirty work for you containing 50 lines of parameter setting code. This isn't so bad, and you only have to write it once. I hope you aren't that lazy ;-) And by the way, isn't 50 columns in a table a bit much?

Perhaps a normalization process could help and lower complexity of your database and the code that will manipulate it. Another way to go is to use an ORM like or a more lightweight approach like Spring JDBC template.

Call map.keySet() to get the name of all columns. Create an INSERT statement by iterating the key set. The column is from an item (a key) in the key set.

The data is from map. Get(key).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions