Home > Java > JavaBase > body text

How to write java sql statement

angryTom
Release: 2019-11-18 10:51:07
Original
8453 people have browsed it

How to write java sql statement

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" ;
Copy after login

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" ;
Copy after login

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" ;
Copy after login

3 with single quote ' '. Then add double quotes between the single quotes " "

String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN  ' "    " ' AND 10" ;
Copy after login

4. Then add a plus sign between the double quotes

String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEEN  ' " +  + " ' AND 10" ;
Copy after login

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" ;
Copy after login

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

How to write java sql statement

php Chinese Internet, a large number of free Java 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template