0

I have created a database table with 4 columns

user , password , ipaddress , isVIP

I would like to insert the data to ipaddress where user = ? and password =?

I tried using this mySQL syntax

PreparedStatement ps = con.prepareStatement("INSERT INTO login(ipaddress) VALUES(?) select * from login where user=? and password=?");
        ps.setString(1,ipaddress);
        ps.setString(2,id);
        ps.setString(3, password);

Which it tells me that I have syntax error.

4
  • I think you look for INSERT INTO login(ipaddress, user, password) values(?,?,?) Commented Jan 3, 2017 at 14:25
  • Required reading: Best way to store password in database. Commented Jan 3, 2017 at 14:28
  • @e4c5 yes I did. It solves the problem Commented Jan 23, 2017 at 13:47
  • Glad to have been of help. Commented Jan 23, 2017 at 13:50

2 Answers 2

2

Your INSERT is not syntactically correct. Even when you fix it your INSERT will not work because you are selecting and inserting into the same table! That is not allowed in mysql.

You will need to use an update instead.

con.prepareStatement("UPDATE login SET ipaddress = ? WHERE user=? and password=?");
Sign up to request clarification or add additional context in comments.

Comments

-1

Can you try with below one.

 PreparedStatement ps = con.prepareStatement("INSERT INTO login(ipaddress) VALUES select ipaddress from login where user=? and password=?");
            ps.setString(1,id);
            ps.setString(2, password);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.