views:

368

answers:

3

I am calling mysql.exe from Java to load a database. Because the process just hangs, I need to create a command file and pass in the username and password.

Example contents of command.bat:

mysql --user="%1" --password="%2" mydatabase < myscript.sql

The problem is that I cannot see the output of the mysql command to see if there were any errors. They display on the command line, but I cannot seem to capture them in a file for parsing or an InputStream.

How can I see the output of the mysql command?

NOTE: Calling mysql.exe directly from Java hangs because the mysql does not appear to be sending the information to the buffer.

NOTE: We are using mysql.exe instead of JDBC because we need to update things like triggers. In order to submit all the statements to the db, we would need to parse all the commands and pass them in one at a time.

NOTE: This is a running MySQL database that needs to be upgraded.

+1  A: 

If you are only accessing this database from within Java, a better solution would be Connector/MXJ. This will allow you to simply make a well formed JDBC call, and the library will take care of the database startup for you automatically.

Basically, the jar file contains in instance (or, for multiple platforms, instances) of the mysql server executable. It also contains a skeleton where you can load prepopulated data for your database.

The first time you access the JDBC connection, it will pull the proper mysql server out of the jar, and create the database in the current directory (using the prepopulated data from above). Any changes from that point on will be persistent, as expected.

Here's some more info:

Launching via JDBC

Launching via a Java Object

James Van Huis
What he's asking is not how to start a MySQL database, but how to interact with a running database. The reason he can't use JDBC is because he wants to import a MySQL script that was created using mysqldump.
sjbotha
This was not clear from the original post. I will update when he posts the relevant code.
James Van Huis
A: 

Not sure why you can't view its output. Try making it output to a file, then use Java to read from the file at the same time.

I haven't tried this myself.

sjbotha
The following just does an empty file:mysql --user="%1" --password="%2" mydatabase < myscript.sql > myout.log
Joshua
A: 

You should look at Process Builder. It lets you get a handle on the input/oputput and error stream.

Milhous