0

I'm familiar with using JDBC and parsing ResultSet etc.

I'd like to know if there's a way to retrieve a MySql query result as pure string, just as if I were running it from the command line, with all the "+----------+" and stuff, just like here, or anywhere on MySql official site.

I'd like to be able to get queries from the user, pass them to some MySql blackbox as if I were passing it to the command line, and print the result to the user.

All solutions on the internet uses JDBC and isn't what I'm looking for. Any ideas?

Jjang
  • 11,250
  • 11
  • 51
  • 87
  • I want to say that what you're looking for is unique to the MySQL client. The client is what formats the data in that way after it requests the data. Therefore, it seems that you would have to perform the type of formatting you want yourself after requesting the data with JDBC. – Garrett Aug 02 '15 at 20:56

1 Answers1

0

You could call the command line from Java, see for instance Running command line in java, or search Google for something like “call command line from java”.

The command you should invoke from your Java program is the mysql client, passing to it the query you want to be executed (the way will depend on the operating system used) and grabbing the output of the command, that will contain “all the "+----------+" and stuff”.

Community
  • 1
  • 1
Renzo
  • 26,848
  • 5
  • 49
  • 61
  • Yes but will my program be portable? I can't guarantee that mysql client is installed on every machine I'm gonna run my program... unless there's a JAR I can use – Jjang Aug 03 '15 at 17:36
  • What you require is the output of a typical text-oriented mysql client, like the command `mysql`, which *must* be installed on the machine where your Java code must be run. If you want a portable, Java-only solution, then you *must* use JDBC. You could, for instance, by using JDBC, write a method that executes a generic SQL query, then, from the ResultSetMetadata, gets the name of all the fields returned and generates a string in the way that you need. It is not too difficult, I did something similar some years ago in a few lines of code. – Renzo Aug 03 '15 at 18:37