Reputation: 73
How do i force a sentence in Java to be a string?
String addBedrooms = "INSERT INTO Property.Bedrooms("3")";
Bedrooms row is a varchar which id like to insert the 3 as a string but i keep getting SQL error
Whats the correct statement? Thanks
Upvotes: 0
Views: 140
Reputation: 41291
Escape:
String addBedrooms = "INSERT INTO Property.Bedrooms(\\\"3\\\")";
One \
for javac, the next for your DBMS itself. The third for the quote mark.
Or, use:
String addBedrooms = "INSERT INTO Property.Bedrooms('3')";
Upvotes: 1
Reputation: 11841
You can use single quotes or escape the double quotes.
Single quotes
String addBedrooms = "INSERT INTO Property.Bedrooms('3')";
Double quotes:
String addBedrooms = "INSERT INTO Property.Bedrooms(\\\"3\\\")";
NOTE: \\
to create an escape character for the dbms and \"
escapes the double quote for java
Upvotes: 0
Reputation: 280181
Use PreparedStatement
and don't forget proper SQL (you're missing VALUES
for your INSERT
statement)
String addBedrooms = "INSERT INTO Property.Bedrooms VALUES (?)";
PreparedStatement ps = connection.prepareStatement(addBedrooms);
ps.setString(1, "3");
int rowCount = ps.executeUpdate();
See this tutorial for an in-depth description.
Upvotes: 4
Reputation: 178343
The SQL statement, even as a Java string, must enclose SQL strings with single-quotes, not double-quotes:
String addBedrooms = "INSERT INTO Property.Bedrooms('3')";
Upvotes: 0