Calling mysql.exe from Java - java

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.

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

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.

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

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

Related

Accessing Connected Database DB in Eclipse with Java Code

I successfully connected to a DB in eclipse through the Database Development perspective and am able to run queries in the SQL File Editor. However, I would like to write java code to automatically run a query and then extract the data to a flat file. What is the easiest way to do this? Since I am already connected to the database, can I bypass the java code that involves connecting to the database.
No, the Java JVM also needs to be able to connect to the database. There are many examples of JDBC connections on the internet, including this one.
Once you have got the data you needed, you should make use of FileWriter in order to write the information to file.

How do I back up a MySQL database

I have created a Web Application using Java JSP and for a Database I used MySql server 5.5. I am running on windows and the application works just fine. My question is, where is the actual physical database stored, i can not find it anywhere on my computer, and the reason why i am asking is portability, if i copy and paste and run the project on different machine, there is a problem connecting to the database, i can recreate the schema of the database, but the actual data stored in the database i can not find it.
Please help
Use mysqldump to backup your database:
The mysqldump client is a backup program originally written by Igor
Romanenko. It can be used to dump a database or a collection of
databases for backup or transfer to another SQL server (not
necessarily a MySQL server). The dump typically contains SQL
statements to create the table, populate it, or both. However,
mysqldump can also be used to generate files in CSV, other delimited
text, or XML format.
An alternative option if you don't want to use the commandline is to use a program like HeidiSQL

Java app with embedded DERBY DB

I am trying for a while to make executable JAVA application having embedded DB (derby DB), but facing some problems, and need your valuable help.
Namely, I am using Eclipse as environment.
I export Java app to RUNNABLE JAR file, and it works fine on my desktop machine.
The idea is to make EXE doubleclick icon and send it to another machine which have no JAVA background/environment....so point is to send it to another user who will just get exe file, double click it and work with it.
The DB is not only readable, since application is inserting data in tables.
So, it works fine on my machine, but when I send the same JAR file to another machine, I get error:
"Schema TEST does not exist"
I can see application but without any data, like there is no connection with DB. So, it is useless.
Even I use JSmooth, Install4j.... to convert JAR to exe file, I get the same error.
So, first I have to make JAR file working on another machine.
Seems to me, I am doing something wrong with DB files.
Please let me know what info u need more from my side, and let me know how I can do this.
If the application intends to read AND WRITE data when it is running, then it can't be entirely contained in a JAR file, because you can't put an updatable Derby database in a JAR file. You'll need to have some "installation procedure", perhaps some logic that you run the first time your application is double-clicked by the user, that (a) finds a suitable location for the Derby database on the user's machine, (b) creates the database and defines all the tables, views, etc, (c) loads any initial data.
Then, on subsequent runs of the application, it will be able to re-open the database and continue using it.

How to execute t-sql script using java?

I have found the following links for executing script using java i.e. How to execute sql-script file in java? . But it is specific to postgre and mysql.
I am looking for same solution for executing script in Sql server 2005. I am not acquinted with SQL Command Line. I have to make a batch containing series of calling different method of a class which also include the executing the script in the middle of batch. I googled it. But, i am not able to find the solution for my problem. Thanks in advance.
Instead of a dirty solution like stored procedures, why don't you try the one found in How to execute sql-script file in java?? Instead of the psql command, try
Sqlcmd -Shostname\dbname -U username -p password -i sqlscriptfile
This should connect to the database, using SQL Server authentication, and run the script you specify. Pablo's answer in the referenced question should do the rest. Depending on your path, you might need to alter the 'Sqlcmd' bit to reflect the location of the program on your machine.
To learn more about the command line utility, check out this article on databasejournal.com.

HSQLDB - hiding database structure/contents from users

I'm considering using HSQLDB version 1.8.x in a desktop app for storing local data. From what I can see, the database is stored on disk as a number of SQL statements to create the tables, insert the data, etc.
Is there a simple way I can hide this from users? I'm don't necessarily need it to be completely encrypted, etc - I'd just like to prevent the casual user from simply opening the file and seeing the structure of the database.
You could embed your database files within a jar file and connect to them using the notation:
jdbc:hsqldb:res:<path in jar>
Check out the Advanced Topics section of the HSQLDB guide for more information on this. However, I've never tried it so am not 100% sure it will work ...
The solution I've gone with for now is to call:
db.update("SET SCRIPTFORMAT COMPRESSED;");
to store the .script file in a human-unreadable form and:
db.update("SET PASSWORD password;");
to prevent more savvy users from opening the DB using their own HSQLDB client.
Unfortunately I was not able to execute below command
db.update("SET scriptformat COMPRESSED");
And was getting this error
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: SCRIPTFORMAT
This error solved with this command
db.update("SET FILES SCRIPT FORMAT COMPRESSED");
I am using HSQLDB 2.3.3

Categories

Resources