Today your leader to find you, hope you can help him a little busy, he is now anxious to go to the meeting. What can I do for you? You're curious.
He said to you, the current database of your project has a user information table, which contains a very user data, now need to complete a selective query user information function. He said he'd pass it on to you. An array that contains a lot of user names, and you need to check out their corresponding data according to the username.
This function is very simple, you readily agreed. Since your project is using a MySQL database, you quickly write the following code:
public class Queryutil {public
void Finduserinfo (string[] usernames) throws Exception {
Class.forName (" Com.mysql.jdbc.Driver ");
Connection conn = drivermanager.getconnection
("Jdbc:mysql://localhost:3306/test", "root",
"123456");
Statement stat = conn.createstatement ();
StringBuilder sql = new StringBuilder ("SELECT * from User_info where");
for (String user:usernames) {
sql.append ("username = '");
Sql.append (user);
Sql.append ("' or");
}
SYSTEM.OUT.PRINTLN (SQL);
ResultSet ResultSet = Stat.executequery (sql.tostring ());
while (Resultset.next ()) {
//processing data read from the database
}
//The read data should be assembled into an object return, omitted here.
}
}
Here, the SQL statements are assembled according to the incoming user array group, and then the corresponding rows are found in the database. For the sake of debugging, you will also print out the assembled SQL statements.
Then, you write the following code to test this method:
public class Test {public
static void Main (string[] args) throws Exception {
queryutil query = new Queryutil ();
query.finduserinfo (new string[] {"Tom", "Jim", "Anna"});
}
Now run the test code and you find that the program has gone wrong. So you immediately checked the printed SQL statement, and found the problem.
SELECT * from user_info where username = ' Tom ' or username = ' Jim ' or username = ' Anna ' or
The assembled SQL statements add an OR keyword at the end! Because the For loop does not have to add or to the last piece of data, the code is stupid enough to add an or keyword to the last piece of data, causing the SQL statement syntax to go wrong.
What can we do about it?
There it is! You have a flash of inspiration and come up with a solution. After the SQL statement is assembled, it is better to delete the last or deleted. So you change the code to look like the following:
public class Queryutil {public
void Finduserinfo (string[] usernames) throws Exception {
Class.forName (" Com.mysql.jdbc.Driver ");
Connection conn = drivermanager.getconnection
("Jdbc:mysql://localhost:3306/test", "root",
"123456");
Statement stat = conn.createstatement ();
StringBuilder sql = new StringBuilder ("SELECT * from User_info where");
for (String user:usernames) {
sql.append ("username = '");
Sql.append (user);
Sql.append ("' or");
}
Sql.delete (Sql.length ()-"or". Length (), sql.length ());
SYSTEM.OUT.PRINTLN (SQL);
ResultSet ResultSet = Stat.executequery (sql.tostring ());
while (Resultset.next ()) {
//processing data read from the database
}
//The read data should be assembled into an object return, omitted here.
}
}
Using the StringBuilder Delete method, remove the last redundant or delete it, then run the test code again, and the printed SQL statement looks like this:
SELECT * from user_info where username = ' Tom ' or username = ' Jim ' or username = ' Anna '
All right, finish it! You are full of confidence.