File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Set multiple columns using prepare statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Set multiple columns using prepare statement" Watch "Set multiple columns using prepare statement" New topic

Set multiple columns using prepare statement

Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54

I have JTable, where the user sets the data and clicks update. What i want to do is update multiple columns in my database using prepare statement, i can update 1 column no problem but how do i update more then 1?

This is what i tried to do but it says Optional feature not included..any ideas?
Thank You
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

You are missing a comma between your columns. Also, 'Part Number' looks odd - do you have columns with spaces in the name? I would expect a database to interpret a value in single quotes as a literal string value, not a column identifier.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
right each column is seperated by comma?

yes i have spaces in my column name but if you add ` ` to the column name it works fine with access.

PS: they are sinlge quotes instead they are backtips.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124

Mayur Gosalia wrote:right each column is seperated by comma?


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54

Tried the above gives me a syntax error. Also tried inserting the word set in front of each column name with and without space after comma no joy..i dont know what i am doing wrong.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Don't put a comma before the WHERE clause.

You're using Access? Then it comes with documentation on the version of SQL it supports. In the long run you might find it easier to track down that documentation and find out the correct syntax for yourself, rather than waiting for answers on a forum.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 64
try this:

public void updateprinterConsumableColumns(String stockLevel,
int recievedStock,
String averageUsage,
int partNumber) {

String queryString = "update PrinterConsumable set 'Stock Level' = ? 'Received Stock' = ? 'Average Usage' = ? where 'Part Number' = ?";
try {
conn = this.getDatabaseConnection();
PreparedStatement preparedStmt = conn.prepareStatement(queryString);
preparedStmt.setString(1, stockLevel);
preparedStmt.setInt(2, recievedStock);
preparedStmt.setString(3, averageUsage);
preparedStmt.setInt(4, partNumber);


} catch (SQLException sqe) {
//System.out.println("SQLException: updateprinterConsumableColumns return -> " + sqe.getMessage());
} catch (NullPointerException npe) {
//System.out.println("NullPointerException: updateprinterConsumableColumns return -> " + npe.getMessage());
} finally {
try {
} catch (SQLException e) {
// TODO Auto-generated catch block
I agree. Here's the link:
subject: Set multiple columns using prepare statement
jQuery in Action, 3rd edition