JDBC question: not being able to correctly bind variables and values (shortened)
I'm working on a project that can be described as follows: The user will pass in a table name and some criteria. The program will then retrieve a table from Oracle and display the results to the user. The user will then have the opportunity to alter cells in a row, save the changes and send it back to Oracle. Now I'm writing a "generalDAO" that does all these things and works for any table passed. (One of the reason开发者_Python百科s we're trying to do this in a general setting is that we have 50+ tables to work through.) Anyways, I'm able to do everything except correctly bind the variables to the updated values. I think I'm screwing up the logic of this. If somebody has the time and patience to work through this, I'd appreciate any help or suggestions. To make this concrete, let's assume that the table I'm working with is called "BOOKS" and has the following columns: book_id (also the PK), title, author_last_name, author_first_name, and rating.
(getPrimaryKey is a simple method that queries metadata table for the PK of a table. row is a linkedhashmap that contains the column names as keys--there's other info in the value, but it's not related)
Here's an example of the update statement that is constructed:
UPDATE BOOKS SET BOOK_ID=?, TITLE=?, AUTHOR_LAST_NAME=?, AUTHOR_FIRST_NAME=?, RATING=? WHERE BOOK_ID=?
So that is all good. My problem is that I cannot actually get the values to pair up correctly with the correct columns. To put it one way, changes are stored in a list in the following manner:
(primarykey1value, primarykey2value,..., column1value, column2value, ...,columnnvalue)
but the query is set up as:
(column 1, column 2, column 3,..., primarykey1, primarykey 2...)
So if I just write a for loop to run through the query then I'll end up binding the values as follows:
UPDATE BOOKS SET BOOK_ID=primarykey1value, TITLE=primarykey2value, AUTHOR_LAST_NAME=?, AUTHOR_FIRST_NAME=?, RATING=? WHERE BOOK_ID=?
And so on. The problem, in short, is that the pairing is not correct.
Here is my code:
public void update(String tableName, List dataList){
LinkedHashMap primaryKeyMap=getPrimaryKey(tableName);
Iterator itr=dataList.iterator();
Connection conn=null;
PreparedStatement ps=null;
try {
conn=ConnectionHelper.getConnection();
conn.setAutoCommit(false);
LinkedHashMap row=null;
while(itr.hasNext()){
row=(LinkedHashMap)itr.next();
SQLVO vo=makeUpdate(tableName, primaryKeyMap, row);
String sql=vo.getSql();
int totalCount=vo.getColCount()+vo.getPkCount();
int upperBd=totalCount-vo.getPkCount()+1;
int colBd=vo.getColCount();
ps=conn.prepareStatement(sql);
Iterator iter=row.entrySet().iterator();
//if("Y".equals(checker)){
for(int i=upperBd; i <=totalCount; i++){
Map.Entry pairs=(Map.Entry)iter.next();
ps.setString(i, (String)pairs.getValue());
System.out.println(" This is the associated value: "+pairs.getValue()); //
}
for(int i=1; i<=colBd; i++){
Map.Entry pairs=(Map.Entry)iter.next();
ps.setString(i, (String)pairs.getValue());
}
ps.addBatch();
//}
}
int updateCounts[]=ps.executeBatch();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
DAOException daoe = new DAOException(e.getMessage());
throw daoe;
} finally {
ConnectionHelper.close(conn);
}
}
Does anybody have any experience with this type of problem? The two for loops are clearly not correct, and I get "NoSuchElementException" errors. But I'm at a loss on how to proceed.
Anyways, any help would be appreciated--as my handle sugges, I'm a newbie to all of this. Sorry for making this post so long, but hopefully a full question length helps.
If the values are to be used in different capacities (aka value vs PK) then you will need to map them uniquely either by index or as a name/value pair.
精彩评论