2

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.

7
  • Did you try looking at the error stream instead of the output stream? Commented Feb 20, 2009 at 18:04
  • Try the -B switch to mysql.exe? Commented Feb 20, 2009 at 18:08
  • Looks like this guy did the same thing with C# so it should be possible. jdconley.com/blog/archive/2006/08/01/… IF you paste the code we can see if you have the order wrong. Remember to read output before calling waitFor(). Commented Feb 20, 2009 at 18:13
  • I have a streamgobbler to read both the error and input streams into a stringbuffer. It extends thread so it can do them both at the same time. Commented Feb 20, 2009 at 19:02
  • Could you post some code? Specifically, I would like to see your stream handling and Runtime.exec() calls. Commented Feb 20, 2009 at 19:21

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
This was not clear from the original post. I will update when he posts the relevant code.
0

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.

1 Comment

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

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

Comments

0

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.

Use 2>yourfilename

E.g.

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

should capture the STDERR (to where MySQL.exe sends its output here)

E.g.

mysql --user="%1" --password="%2" mydatabase < myscript.sql >myoutput.txt 2>&1

will capture both STDOUT and STDERR in the same file (=myoutput.txt).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.