Code snippet to parse mysql output from Java
I'll write it, but if there's a ready, tested code snippet that does this I'll be happy to steal it.
COUNT(*) SUM(cnt)
0 1
Edit - Thanks for the votes to close, I can always count on the good souls in Stack Overf开发者_如何学JAVAlow to be quick with the close votes. I'll add some details to the question.
I am looking for a java code snippet that can parse the output of running "mysql" executable. The snippet above is an example of such an output that I would like to parse.
This is close to trivial to parse using java.util.Scanner
.
Yes, it's called JDBC. You can find the MySql JDBC driver here.
I ended up writing a specific snippet for my needs. I only need to support specific queries, so I didn't write a generic parser for mysql output. Here is my code, anyway:
String[] lines = sqlOutput.toLowerCase().split("\n");
if (lines.length != 2) {
throw new Exception("Failed to parse, bad header line");
}
String headers = lines[0];
String body = lines[1];
if (!headers.startsWith("count(*)"))
throw new Exception("Query output must start with 'count(*)'")
Scanner scanner = new Scanner(body);
return scanner.nextInt()
精彩评论