How to write java sql statement
About the splicing rules of SQL statements in Java
The implementation target statement is the following. Note that the java variable here is idd
int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN '" + idd + "' AND 10" ;
The string in java can only be double quoted. If a variable needs to be spliced in the string, the variable must be single quoted. Surround it, then add two double quotes and then two plus signs, and the variable is in the middle.
Splicing steps
1. Write the specific SQL statement (no variables have specific values), such as the above sql
//查询picinfos表中id为2到10的id和piUrl值 String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN 2 AND 10" ;
2. At this time, if you want to change 2 to id (int type variable), you only need to modify it in the middle. First, delete 2 and replace
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' ' AND 10" ;
3 with single quote ' '. Then add double quotes between the single quotes " "
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " " ' AND 10" ;
4. Then add a plus sign between the double quotes
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " + + " ' AND 10" ;
5. Finally, add the variable idd between the plus sign and finish
String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN ' " + idd + " ' AND 10" ;
Function: Start with the SQL prototype, replace it with single quotes, add double quotes in the middle, add double plus signs in the middle, and add variables in the middle.
So how is the Java string converted into a SQL statement?
As shown in the picture
php Chinese Internet, a large number of freeJava introductory tutorials, welcome to learn online!
The above is the detailed content of How to write java sql statement. For more information, please follow other related articles on the PHP Chinese website!