Jump to content

How to take a value of a variable in java and assign it to a field in mysql?

Shammikit

i have a variable called sal and i want to assign it's value to a field called salary in my mysql databse. when i execute the code below it updates the salary as the world sal and not its value.how can i update the record with its value.please help.

 

sal=sal+new_amount;


String pay ="UPDATE employees Set salary = sal where EMP_No='"+empno+"'";

 

Link to comment
Share on other sites

Link to post
Share on other sites

String pay ="UPDATE employees Set salary = '"+sal+"' where EMP_No='"+empno+"'";

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, SCHISCHKA said:

String pay ="UPDATE employees Set salary = '"+sal+"' where EMP_No='"+empno+"'";

i got this error when i tried that : Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax 

Link to comment
Share on other sites

Link to post
Share on other sites

every database has its quirks around quotes. try removing the single quotes. theres also two different kinds of quote symbols in the keyboard character set and copying from the internet often results in the wrong type being used. im on a mac keyboard so it wouldnt surprise me.

Set salary = "+sal+"

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, SCHISCHKA said:

 


Set salary = "+sal+"

i tried removing the singe quote and using double quotes only but then the entire line becomes incorrect.it says: semicolons expected but i have put semicolons at the end

Link to comment
Share on other sites

Link to post
Share on other sites

when i debug sql statements i like to print them out to std out to test them on the DB directly.

System.out.println(pay);

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, SCHISCHKA said:

when i debug sql statements i like to print them out to std out to test them on the DB directly.

System.out.println(pay);

Below is the output of System.out.println(pay)  :


UPDATE employees Set Salary = 'sal' where EMP_no='35'

 

 

its updating the salary column of EMP_no 35 as sal not the value of sal.

 

Link to comment
Share on other sites

Link to post
Share on other sites

what is the primitive type of sal?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, SCHISCHKA said:

what is the primitive type of sal?

its a string

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, SCHISCHKA said:

what is the primitive type of sal?

 forgot to mention, below the update query i have this statement otherwise nothing happens

 

int resultset = statement.executeUpdate(pay);

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Shammikit said:

its a string

sorry i assumed it was a number. what is this suppose to be doing?

sal=sal+new_amount;

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/27/2016 at 1:06 AM, SCHISCHKA said:

sorry i assumed it was a number. what is this suppose to be doing?


sal=sal+new_amount;

 

Thank you for your help, i eventually found myself the code.i had to convert pay to a int.then only the value got assigned to the variable so that i can use it to update my db

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×