473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Correct jdbc syntax for complicated update statement

153 100+
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?
Jun 18 '09 #1
3 3312
r035198x
13,262 8TB
Start by reading Sun's JDBC tutorial.
Jun 18 '09 #2
brendanmcdonagh
153 100+
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!!
Jun 18 '09 #3
JosAH
11,448 Expert 8TB
@brendanmcdonagh
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
Jun 18 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Pastrovick | last post by:
I use a function, myrandomPIN (), to generate random PIN numbers. The following sql query updates records with the SAME PIN number but. I want to generate DIFFERENT pin numbers for every record....
3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
3
by: Narine | last post by:
Hi All, I need to write one complicated update statement and I'm looking at maybe finding a simpler way to do it. I have 2 tables: 1.Photo Table PhotoID FileName 1 111.jpg
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
6
by: HeadScratcher | last post by:
I am trying to speed up my update statements by removing inner select statements. Example: update orders set shipname = (select contactName from customers where customerid = orders.customerID)...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
7
by: Otto Carl Marte | last post by:
Hi, When using a CallableStatement with the IBM DB2 Universal JDBC Driver the executeUpdate and getUpdateCount() methods on CallableStatement always return -1. According to the JDBC...
1
Fary4u
by: Fary4u | last post by:
Hi, I'm attempting to update a record via the recordset.update method, However, the recordset object is defined and accesses the correct record that I want to update, as I can retrieve field...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.