MySQL Syntax Error while updating multiple columns - java

I am trying to update user profile by taking input through a jsp form and storing it into a table in MYSQL Workbench using servlet. I am getting a Syntax Error while updating the table.
ps = (PreparedStatement) con.prepareStatement("UPDATE user SET user_id =?, FName=? , LName=?, City=? , Email=? , Country=? , DOB=?, JobPosition=?, Profile_Picture=? WHERE user_id=123");
ps.setInt(1, UserId);
ps.setString(2, Fname);
ps.setString(3, Lname);
ps.setString(4, City);
ps.setString(5, EmailId);
ps.setString(6, Country);
ps.setDate(7, java.sql.Date.valueOf(DOB));
ps.setString(8, JobPosition);
if (inputStream != null) {
ps.setBlob(9, inputStream);
}
[EDIT]
Error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''techquo'.'user' set user_id =123, FName='Default' , LName='Default', City='' ,' at line 1
This is my SQL query. I am new to this and cannot figure out what's wrong with the syntax. The table name and the name of attributes are correct. I have rechecked. Any help would be appreciated. Thanks!

user is a reserved keyword. see the docs here
try wrapping it in backticks:
ps = (PreparedStatement) con.prepareStatement("Update `user` set user_id =?, FName=? , LName=?, City=? , Email=? , Country=? , DOB=?, JobPosition=?, Profile_Picture=?");
also, there is no where clause, so the update will be applied to all the rows - Probably not what you want.

Related

Update data netbeans

I have a problem when updating data with NetBeans. When I update without changing the id, the error message appears key duplicate '10' for 'primary'
SQL = "update sumber set nama_sumber=?, alamat_sumber=?, kapasitas_sumber=? WHERE id_sumber='"+kodegudang+"'";
ps = conn.prepareStatement(SQL);
ps.setString(1, txt_id.getText());
ps.setString(2, txt_nama.getText());
ps.setString(3, txt_alamat.getText());
ps.setString(4, kapasitasdos.getText());
JOptionPane.showMessageDialog(null, "Sumber Air berhasil Diedit");
ps.executeUpdate();
All the primary key values in a table must be different. I'm guessing that you already have a row with nama_sumber = 10 (or whatever the primary key is). You can check that by connecting to the database directly and running select * from sumber.

SELECT inside an UPDATE query

I'm trying to select a inner table while running an update, but I keep receiving an syntax error can anyone see what I'm doing wrong Thanks.
Syntax error: Encountered "SELECT" at line 1, column 94.
String sql = "UPDATE MEMBER SET FIRSTNAME=?, LASTNAME=?, STREETADDRESS=?, CITY=?, STATE=?, ZIP=?, PHONE=?, SELECT MEMBERSHIPID WHERE MEMNAME=? WHERE MEMBERID=?";
PreparedStatement stmt = db.getPreparedStatement(sql);
stmt.setString(1, fName);
stmt.setString(2, lName);
stmt.setString(3, streetA);
stmt.setString(4, city);
stmt.setString(5, state);
stmt.setString(6, zc);
stmt.setString(7, phon);
stmt.setString(8, memSelection);
stmt.setInt(9, Integer.parseInt(memberID));
You need to write your SELECT as an assignment from a subquery (enclosed in parentheses), and also specify the table you are selecting the value from:
UPDATE MEMBER
SET FIRSTNAME=?,
LASTNAME=?,
STREETADDRESS=?,
CITY=?,
STATE=?,
ZIP=?,
PHONE=?,
MEMBERSHIPID = (SELECT MEMBERSHIPID
FROM MEMBERSHIP
WHERE MEMNAME=?)
WHERE MEMBERID=?

How to convert " ' " to " ` " in java

The syntax that from the java code is not applicable to mysql. The setString() from java will come out with ' and not ` which is not accepted in mysql.
I tried in my localhost to run the code, it really not accepting 'doctor' and only accept ``doctor`.
Below are my code:
PreparedStatement ps = con.prepareStatement("SELECT id FROM ? WHERE id = ?");
ps.setString(1, "doctor");
ps.setInt(2, 123);
ResultSet rs = ps.executeQuery();
and there is an error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''doctor' WHERE id = 123' at line 1
It's because your code produces following query:
SELECT id FROM 'doctor' WHERE id = 123
As you can see table name is used as a String which is invalid SQL Syntax, so you can either hard code table name and or if you really want it to be dynamic you can achieve it like:
String sql = String.format("SELECT id from %s where id = ?", tblName);
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 123);
ResultSet rs = ps.executeQuery();

database not updating for some reason

This sql query is not updating the database, instead returning error. Any suggestions?
PreparedStatement ps10 = con.prepareStatement("UPDATE payroll_system.payslip SET hours_worked = (SELECT SUM(Hours) FROM payroll_system.monthly_timesheet WHERE employeeID=?) WHERE employeeID=?");
ps10.setString(1, employee_id);
ps10.setString(2, employee_id);
ps10.executeUpdate();
monthly_timesheet table:
payslip table:
Insert don't have where clause
"INSERT INTO payroll_system.payslip(expense_claims)
WHERE employeeID=?
SELECT SUM(expense) FROM payroll_system.expense_master"
eventually you are looking for update?
or use
"INSERT INTO payroll_system.payslip(expense_claims)
SELECT SUM(expense) FROM payroll_system.expense_mast"
for update
PreparedStatement ps9 = con.prepareStatement("UPDATE payroll_system.payslip
SET expense_claims = (SELECT SUM(Expense)
FROM payroll_system.expense_master
WHERE employeeID=?) WHERE employeeID=?");
ps9.setString(1, employee_id);
ps9.setString(2, employee_id);
ps9.executeUpdate();

Inserting value into table in java

I am new to java and trying to learn to insert the username and password and a profile image path into a Mysql database. When I run the following code, it will insert into table but the path filed of the table 'table_profile' 3rd column like as follows
if path equals "C:/Users/Manohar/Documents/FileUplaodDemo/build/web/uploads/x.jpg"
then it is inseerted as path equals "C:UsersManoharDocumentsFileUplaodDemo uildwebuploads
stmt = connection.prepareStatement(
"insert into table_profile values('"+userId+"','"+userName+"','"+path+"')");
User parameter binding like this:
stmt = connection.prepareStatement("insert into table_profile values(?, ?, ?)");
stmt.setInt(1, userId);
stmt.setString(2, userName);
stmt.setString(3, path);
Let Java do all the hard work for you :)

Categories

Resources