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.

Fouzy
  • 125
  • 1
  • 1
  • 7

2 Answers2

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=?");
e4c5
  • 52,766
  • 11
  • 101
  • 134
-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);
rsh
  • 69
  • 4