user3555182
user3555182

Reputation: 3

MySql update statement in jdbc

I am trying to update my database when there is a change in the inventory on the client program. The update statement ends up with the correct values in it but i get a mysql syntax error when trying to execute the update.

String sql = "UPDATE video_device SET serialNumber='" + device.getSerialNumber() + "', inventoryNumber=" + device.getInventoryNumber() + ", ownerName='" + device.getServicePerson().getName() 
                    + "', ownerTitle='" + device.getServicePerson().getJobTitle() + "', ownerEmail='" + device.getServicePerson().getEmail() + "', ownerPhoneNumber='" + device.getServicePerson().getPhoneNumber() 
                            + "', dateYear=" + device.getDateAquired().getYear() + ", dateMonth=" + device.getDateAquired().getMonth() + ", dateDay=" + device.getDateAquired().getDay() + ", typeDevice='" + device.getTypeDevice() 
                                   + "', description='" + device.getDescription() + "', price=" + device.getPrice() + ", locationBuilding='" + device.getLocation().getBuilding() + "', locationRoomNumber=" 
                                    + device.getLocation().getRoomNumber() + " WHERE serialNumber='" + orig.getSerialNumber() + "', inventoryNumber=" + orig.getInventoryNumber() + ", ownerName='" + orig.getServicePerson().getName() 
                    + "', ownerTitle='" + orig.getServicePerson().getJobTitle() + "', ownerEmail='" + orig.getServicePerson().getEmail() + "', ownerPhoneNumber='" + orig.getServicePerson().getPhoneNumber() 
                            + "', dateYear=" + orig.getDateAquired().getYear() + ", dateMonth=" + orig.getDateAquired().getMonth() + ", dateDay=" + orig.getDateAquired().getDay() + ", typeDevice='" + orig.getTypeDevice() 
                                   + "', description='" + orig.getDescription() + "', price=" + orig.getPrice() + ", locationBuilding='" + orig.getLocation().getBuilding() + "', locationRoomNumber=" 
                                    + orig.getLocation().getRoomNumber() + ";";

Upvotes: 0

Views: 69

Answers (1)

domdomcodecode
domdomcodecode

Reputation: 2443

It seems like you're separating your WHERE conditions with commas. You have to use AND/OR to separate them.

What it looks like you're doing:

SELECT ...
FROM ...
WHERE a=1, b=2, c=3
etc

What you should be doing:

SELECT ...
FROM ...
WHERE a=1 AND b=2 AND C=3
ETC

Upvotes: 1

Related Questions