Java??????????
???????????? ???????[ 2016/4/19 10:56:36 ] ??????????????????? ?????
???????leader???3??????????????????????С????????????????????????????????檔
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????飬???????????Щ??????????????????????????????
?????????????????????????????????????????????MySQL??????????д???????′???
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()) {
// ?????????????????????
}
// ?????????????????????????????????????
}
}
????????????????????????????SQL?????????????в?????????С????????????????????SQL????????????
?????????д?????′??????????????????
????public class Test {
????public static void main(String[] args) throws Exception {
????QueryUtil query = new QueryUtil();
????query.findUserInfo(new String[] { "Tom"?? "Jim"?? "Anna" });
????}
????}
??????????????2?????????????????????????????????????′????SQL????????????????
????select * from user_info where username = 'Tom' or username = 'Jim' or username = 'Anna' or
????????????SQL????????????? or ?????????for?????е?????????????????????or????????????????????????????or??????????SQL????????????
???????????????
????????????????????????????????????SQL???????????????or??????????????????????????????????
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()) {
// ?????????????????????
}
// ?????????????????????????????????????
}
}
???????StringBuilder??delete????????????????or?????????????????в?????????????????????SQL????????????
????select * from user_info where username = 'Tom' or username = 'Jim' or username = 'Anna'
?????????????????????????
???????leader?????????????????????????????????????????????????????SQL???????????????о???Щ???????????????????????á??????????????????????SQL????????????????????У??????????????????????????????????????????????????????????????????????????????????????????????
public class QueryUtil {
public void findUserInfo(String[] usernames?? int strategy) 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 ");
if (strategy == 1) {
for (String user : usernames) {
sql.append("username = '");
sql.append(user);
sql.append("' or ");
}
sql.delete(sql.length() - " or ".length()?? sql.length());
} else if (strategy == 2) {
boolean needOr = false;
for (String user : usernames) {
if (needOr) {
sql.append(" or ");
}
sql.append("username = '");
sql.append(user);
sql.append("'");
needOr = true;
}
}
System.out.println(sql);
ResultSet resultSet = stat.executeQuery(sql.toString());
while (resultSet.next()) {
// ?????????????????????
}
// ?????????????????????????????????????
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11