基本上我打算做的是从Agree1out文件中读取信息,其中包含以下信息:
1 | 1
2 | 0
以下代码链接到我的mySQL数据库,使用前两个数字(在“|”之前)获取xf.user.user_id
。
目前字符串打印是:
equalsdavid 1 6af87ef8-e620-4d08-8320-183fc469eb0d
用户0 ac899bce-4a87-4977-957c-05013534b25b
我需要一种方法来删除字符串的第二部分小于1的行。 现在我尝试做类似的事情,但既然我在这里你可能会告诉它不起作用:
temp = temp.replaceAll("(?m)^0.*", "");
以下是代码:
package io.chuchoter.stats2;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import io.chuchoter.core.Main;
public class RATE2_agree {
public static void main(String [] args) {
String line = null;
try {
PrintStream p = new PrintStream(new File(Main.agree2out));
System.setOut(p);
FileReader fileReader =
new FileReader(Main.agree1out);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] parts = line.split("|");
String part1 = parts[0];
String part3 = parts[2];
Connection con = DriverManager.getConnection(Main.host, Main.uName, Main.uPass);
Statement rstmt = con.createStatement();
String r = "SELECT * FROM `xf_user` WHERE `user_id` = " + part1 + ";";
ResultSet rs = rstmt.executeQuery(r);
while (rs.next()) {
String uuid = UUID.randomUUID().toString();
String username = rs.getString("username");
String print = username + " " + part3 + " " + uuid;
System.out.println(print);
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file");
}
catch(IOException ex) {
System.out.println(
"Error reading file");
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}
感谢您的帮助!
答案 0 :(得分:0)
对于字符串“1 | 1”,当您应用分割
时String[] parts = line.split("|");
你得到一个大小为2的数组。因此你的代码就像这样
String[] parts = line.split("|");
String part1 = parts[0];
String part3 = parts[1];
Connection con = DriverManager.getConnection(Main.host, Main.uName, Main.uPass);
Statement rstmt = con.createStatement();
String r = "SELECT * FROM `xf_user` WHERE `user_id` = " + part1 + ";";
ResultSet rs = rstmt.executeQuery(r);
StringBuilder sb = new StringBuilder();
while (rs.next()) {
String uuid = UUID.randomUUID().toString();
String username = rs.getString("username");
String print = username + " " + part3 + " " + uuid;
if(Integer.parseInt(part3) >= 0)
sb.append(print + "\n");
}
System.out.println(sb.toString());
希望这有帮助!