database not updating for some reason - java

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();

Related

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=?

MySQL Syntax Error while updating multiple columns

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.

Java PreparedStatement Comment on Table

I'm familiar with using java prepared statements to insert/update on a table. In oracle you can add a comment on a table, how would I use a preparedstatement to do that?
This was my initial attempt with no luck;
PreparedStatement stmt = con.prepareStatement("comment on table my_table is q'[?]'");
stmt.setString(1, description);
stmt.executeUpdate();
You can use system Oracle table and set comment there with PreparedStatement, like this:
PreparedStatement stmt = con.prepareStatement(
"UPDATE user_tab_comments SET comments = ? WHERE table_name = 'my_table'");
Or try to use simple statement:
String commentOnTableSQL = String.format("COMMENT ON TABLE my_table is '%s'", comment);
Statement statement = dbConnection.createStatement();
statement.execute(commentOnTableSQL);

jdbc combine 2 sql update statements

Is there a way to combine statements 1 and 2 into 1 query? Thanks.
String statement1 = "UPDATE thing SET status = ? WHERE id = ?";
String statement2 = "UPDATE thing SET error_message = ? WHERE id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(statement1);
preparedStatement.setInt(1,status);
preparedStatement.setInt(2, id);
connection.prepareStatement(statement2);
preparedStatement.setInt(1,error_message);
preparedStatement.setInt(2, id);
Looks like you are trying to set 2 columns of same table for an ID. You can change the query like
"UPDATE thing SET status = ? ,error_message = ? WHERE id = ?"
Further more, if the 2 update statements are updating different tables, you can execute both in same transaction. In this way, you can be sure that the commit will take place if both the statement successfully updates the table. Check the example here
Do it like this:
String statement1 = "UPDATE thing SET status = ? ,error_message = ? WHERE id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(statement1);
preparedStatement.setInt(1,status);
preparedStatement.setInt(2,error_message);
preparedStatement.setInt(3, id);
preparedStatement.executeUpdate();

Get last inserted auto increment id in mysql

I am trying to get the mysql command like mysql_insert_id(); which retrieve the last inserted row's auto_increment id. What can I do to get it in Java?
rs = st.executeQuery("select last_insert_id() from schedule");
lastid = rs.getString("last_insert_id()");
my lastid was declared as INT. I dono what to use in rs.get and also the parameter..
Using JDBC, you can use Connection.PreparedStatement(query, int) method.
PreparedStatement pstmt = conn.prepareStatement(Query, Statement.RETURN_GENERATED_KEYS);
pstmt.executeUpdate();
ResultSet keys = pstmt.getGeneratedKeys();
keys.next();
key = keys.getInt(1);
Try using an alias
rs = st.executeQuery("select last_insert_id() as last_id from schedule");
lastid = rs.getString("last_id");
see this post for answer & explanation
Statement stmt = db.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
numero = stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()){
risultato=rs.getInt(1);
}
Why not
SELECT MAX(id) FROM schedule
If id your column has a different name than id, you need to replace it accordingly in the above query.
You can use it like:
rs = st.executeQuery("SELECT MAX(id) AS id FROM schedule");
int lastid = rs.getInt("id");
You can use following query to get last auto_incremented ID of last inserted row.
SELECT (max(auto_incr_id)) from table_name;
Another option:
SELECT id FROM table_name ORDER BY id DESC LIMIT 1;
another way:
ResultSet rs = stmt.executeQuery("SELECT last_insert_id()");
while(rs.next()){
System.out.println("id = " + rs.getLong(1));
}

Categories

Resources