我有一个名为StringInput的接口,代码如下。
public static ArrayList<String[]> fileInput() throws IOException {
ArrayList<String[]> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] formatted = format(line);
lines.add(formatted);
System.out.println(line);
}
}
return lines;
}
public static String[] format(String s) {
String[] data = new String[9];
String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"";
System.out.println("Using RE Pattern:");
System.out.println(logEntryPattern);
System.out.println("Combined Apache Log Format (VERBOSE)");
System.out.println("Input line is:");
System.out.println(s);
Pattern p = Pattern.compile(logEntryPattern);
Matcher matcher = p.matcher(s);
if (!matcher.matches()
|| NUM_FIELDS != matcher.groupCount()) {
System.err.println("Bad log entry (or problem with RE?):");
System.err.println(s);
return null;
}
System.out.println("IP Address: " + matcher.group(1));
String clientIP = matcher.group(1);
System.out.println("Identd: " + matcher.group(2));
String identd = matcher.group(2);
System.out.println("UserID: " + matcher.group(3));
String userID = matcher.group(3);
System.out.println("Date&Time: " + matcher.group(4));
String dateTime = matcher.group(4);
System.out.println("Request: " + matcher.group(5));
String protocol = matcher.group(5);
System.out.println("Response: " + matcher.group(6));
String respCode = matcher.group(6);
System.out.println("Bytes Sent: " + matcher.group(7));
String respSize = matcher.group(7);
System.out.println("Referer: " + matcher.group(8));
String refer = matcher.group(8);
System.out.println("Browser: " + matcher.group(9));
String userAgent = matcher.group(9);
data[0] = clientIP;
data[1] = identd;
data[2] = userID;
data[3] = dateTime;
data[4] = protocol;
data[5] = respCode;
data[6] = respSize;
data[7] = refer;
data[8] = userAgent;
return data;
}
和另一个名为ParseUpload的类,它实现了接口并包含一个main方法。在main方法中,我有一个准备好的语句试图从接口调用一个变量。但是,我不能。我不知道,因为我通常不使用接口,没有实例化它对我来说很奇怪。 有帮助吗?感谢。
编辑:这是另一个班级。
public class ParseUpload implements StringInput {
public static void main(String argv[]) {
try {
StringInput.fileInput();
} catch (Exception e) {
e.printStackTrace();
}
//while (logEntryLine !=null){
// for (int i = 0; i < file.length(); i++){
//System.out.println(fileExists); ignore/For testing purposes
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:5294/mysql", "root", "Iu&#KR489)(");
System.out.println("Database Connection Established!");
String query = " insert into alogs(clientIP, identd, userID, dateTime, protocol, respCode, respSize, refer, userAgent)"
+ " values (?, ?, ?, ? ,? ,? ,?, ?, ?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
preparedStmt.setString(1, data);
preparedStmt.setString(2, identd);
preparedStmt.setString(3, userID);
preparedStmt.setString(4, dateTime);
preparedStmt.setString(5, protocol);
preparedStmt.setString(6, respCode);
preparedStmt.setString(7, respSize);
preparedStmt.setString(8, refer);
preparedStmt.setString(9, userAgent);
preparedStmt.execute();
con.close();
} catch (SQLException ex) {
System.out.println("****************************Can't Connect to the database***********************************");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
System.out.println("Class Not Found");
}
// }
}
}
答案 0 :(得分:0)
您的问题不是界面问题。
在你的情况下,你应该:
这是一个简短的例子:
public class StringInput {
public static String[] format() {
String[] datas = new String[2];
datas[0] = "Some";
datas[1] = "thing";
return datas;
}
}
并且:
public class ParseUpload {
public static void main(String[] args) {
String[] resultOfFormatCall = StringInput.format();
// Now you have the result of your format method, and you are able to use it
System.out.println("result[0]" + resultOfFormatCall[0] + ";result[1]" + resultOfFormatCall[1]);
}
}