tags:

views:

61

answers:

3

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 Overflow 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.

+2  A: 

This is close to trivial to parse using java.util.Scanner.

aioobe
+1  A: 

Yes, it's called JDBC. You can find the MySql JDBC driver here.

matt b
What I specifically wanted here was not to run the queries via JDBC, but rather parse queries that are run by the stand alone mysql client. I have my reasons (not very interesting ones), but I admit they're not particularly strong. I might switch to JDBC.
ripper234
A: 

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()
ripper234