Connecting Tech Pros Worldwide Forums | Help | Site Map

Correct jdbc syntax for complicated update statement

Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#1: Jun 18 '09
Hi all(again)

I'm struggling with similar issue as yesterdays, I can't seem to find any decent info on how to build sql statements using java and it's variables. Any suggestions would be great. Here's my code ....


Expand|Select|Wrap|Line Numbers
  1. String strSQL = "UPDATE customers SET customerName='" +name + "', customerAddress='" +address + "', customerEmail = '" +email + "', customerNumber = '" +phone + "', customerPurchase = '" +purchase + "' WHERE id='" +number + "';
  2.  
  3.  
  4.  
  5.             s.executeUpdate(strSQL);
I'm trying to update database with java variables name, address, email, phone, purchase into columns where id(access auto increment column) = number(java variable)

I'm just getting a unclosed String literal but I know this won't be the correct way to do it anyway - it was just a guess.
Any one know how to do it?

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 18 '09

re: Correct jdbc syntax for complicated update statement


Start by reading Sun's JDBC tutorial.
Familiar Sight
 
Join Date: Nov 2007
Posts: 153
#3: Jun 18 '09

re: Correct jdbc syntax for complicated update statement


Hi, i have just read through the tutorial but still can't find reference to add java variables value correctly into a sql statement.

just tried this but getting null pointer exception

Expand|Select|Wrap|Line Numbers
  1. PreparedStatement updateSales ;
  2.  
  3. String updateString = "UPDATE customers " +
  4.                       "SET customerName = ? where id = ?";
  5. updateSales = con.prepareStatement(updateString);
  6. updateSales.setString(1, name);
  7. updateSales.setInt(2, idNumber);
  8. //updateSales = con.prepareStatement(updateString);
  9. updateSales.executeUpdate();
If anyone can tell me the syntax then I would know how to insert, delete, and update, don't need to know any more!!
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 18 '09

re: Correct jdbc syntax for complicated update statement


Quote:

Originally Posted by brendanmcdonagh View Post

Hi, i have just read through the tutorial but still can't find reference to add java variables value correctly into a sql statement.

just tried this but getting null pointer exception

Please think before you post: a NullPointerException is thrown when something is null which it shouldn't be. Look at your code: are all variables unequal to null? Hint: System.out.println() is your friend.

If the creation of the PreparedStatemnt fails another exception is thrown (always read the API documentation before you start coding so you don't have to guess).

kind regards,

Jos
Reply