how to mysqldump on java? - java

I made my database in mySQL and I exported it in a file using mysqldump.
Is there a way to make my program on JAVA to connect in mysql and create an empty database with the stracture I saved in the file, only if the above database is not allready exist to the server?
Thank you!

Try something like :
Runtime.getRuntime().exec("mysql -u <username> -p<password> <youdbname> < <youbackupfile>");
you will need to replace <username> with your username / <password> with your password / <yourdbname> with you database name / <yourbackupfile> with the file you used for backup
FAQ for MySQL Backup

Related

How to enable the MySQL connection string "ENABLE_LOCAL_INFILE" when using Flyway in a gradle project?

There are several .txt files in my Java gradle project, which I use to populate a database using the MySQL statement LOAD DATA LOCAL INFILE in sql scripts.
There is a MySQL server running on my PC, whose hostname is 127.0.0.1, port is 3306 and DB name is test, so in the gradle project's build.gradle file I configure flyway in the following way:
flyway {
url = 'jdbc:mysql://127.0.0.1:3306/test'
user = 'root'
password = '111111'
}
An example .sql file is
LOAD DATA LOCAL INFILE 'data/categories.txt' INTO TABLE category
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n';
but when running migration using gradle flywayMigrate -i, I got the following error:
Loading local data is disabled; this must be enabled on both the client and server sides
I think I have enabled it in the server side as shown below, which is a MySQL Command Line Client screenshot.
Hope I did it correctly.
After this step, the error still show up, so I think I also need to enable it in the client side, which is in the Java gradle project, is that correct?
Based on https://dev.mysql.com/doc/refman/8.0/en/load-data-local-security.html, I should set ENABLE_LOCAL_INFILE = 1.
But how to do that? In where should I add this ENABLE_LOCAL_INFILE = 1 connection string?
Hope someone can help me.
Thanks in advance!
Use the JDBC connection string property allowLoadLocalInfile=true
See https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-connp-props-security.html#cj-conn-prop_allowLoadLocalInfile

Corrupt H2 Database. Failed to recovery using the Recovery Tool

Today one of my H2 database failed to connect and presented the following error message:
Unable to obtain connection from database (jdbc:h2:file:C:\Users\Username\.appfiles\db\appdb) for user 'sa': File corrupted while reading record: null. Possible solution: use the recovery tool [90030-200]
SQL State : 90030
Error Code : 90030
Message : File corrupted while reading record: null. Possible solution: use the recovery tool [90030-200]
As suggested I tried to use the recover tool as instructed by the documentation, the steps I executed were the following:
Go to your h2 data file directory
java -cp h2-1.4.200.jar org.h2.tools.Recover
Use SQL file generated by the recovery tool to recreate the database
The steps created two files: a .sql and a .txt file, but the SQL generated by the tool didn't have any data or DDL from the database, just some aliases and a bunch of comments. The content of the files are linked below, if they can help shed any light on what went wrong during the process.
This is the .sql file output: https://pastebin.com/DFfwPemP
This is the .txt file output: https://pastebin.com/6zwCgqN3
Is there any step I'm not doing right or is any other thing I can try to recover this db? Any suggestion is welcome.
Run that files with
java -cp h2-1.4.200.jar org.h2.tools.RunScript -url jdbc:h2:[path to destination db file]/[db name] -user [user] -password [password] -script [text file/sql file]

H2: generate insert scripts initialization script

I have full h2 database with lots data in it. I want to launch integration tests agains that data.
Question1: Is it possible to generate *.sql insert files/scripts from full h2 database?
I've trie SCRIPT TO 'fileName' as described here. But it generates only CREATE/ALTER TABLE/CONSTRAINT queries, means creating schema without data.
If answer to the first question is - "Impossible", than:
Question2: Are *.sql insert files the only way to insert initial dataset into h2 db for integration tests?
Question1: Is it possible to generate *.sql insert files/scripts from
full h2 database?
I have just tested with one of my H2 file databases and as result the export exports both structure and data.
I tested with the 1.4.193version of H2.
The both ways of exporting work :
The SCRIPT command from H2 console
org.h2.tools.Script tool from command line.
1) I have tested first the org.h2.tools.Script tool as I had already used it.
Here is the minimal command to export structure and data :
java -cp <whereFoundYourH2Jar> org.h2.tools.Script -url <url>
-user <user> -password <password>
Where :
<whereFoundYourH2Jar> is the classpath where you have the h2.jar lib (I used that one which is my m2 repo).
<url> is the url of your database
<user> is the user of the database
<password> the password of the database
You have more details in the official help of the org.h2.tools.Script tool :
Creates a SQL script file by extracting the schema and data of a database.
Usage: java org.h2.tools.Script <options>
Options are case sensitive. Supported options are:
[-help] or [-?] Print the list of options
[-url "<url>"] The database URL (jdbc:...)
[-user <user>] The user name (default: sa)
[-password <pwd>] The password
[-script <file>] The target script file name (default: backup.sql)
[-options ...] A list of options (only for embedded H2, see SCRIPT)
[-quiet] Do not print progress information
See also http://h2database.com/javadoc/org/h2/tools/Script.html
2) I have tested with SCRIPT command from the H2 console. It also works.
Nevertheless, the result of the SCRIPT command may be misleading.
Look at the official documentation :
If no 'TO fileName' clause is specified, the script is returned as a
result set. This command can be used to create a backup of the
database. For long term storage, it is more portable than copying the
database files.
If a 'TO fileName' clause is specified, then the whole script
(including insert statements) is written to this file, and a result
set without the insert statements is returned.
You have used the SCRIPT TO 'fileName' command. In this case, the whole script
(including insert statements) is written to this file and as result in the H2 console, you have everything but the insert statements.
For example, enter the SCRIPT TO 'D:\yourBackup.sql' command (or a Unix friendly directory if you use it), then open the file, you will see that SQL insertions are present.
As specified in the documentation, if you want to get both structure and insert statements in the output result of the H2 console, don't specify the TO argument.
Just type : SCRIPT.
Question2: Are *.sql insert files the only way to insert initial
dataset into h2 db for integration tests?
As a long time discussed :) you can with DBunit dataset (a solution among others).

How to access *.mv.db file of H2 database?

I have created database with my own program and it appeared as mydatabase.mv.db file.
But when I tried to access the same database with DbVisualizer, with apparently same parameters, it created two files mydatabase.lock.db and celebrity.h2.db and didn't see tables, created in the program.
What was the incompatibility?
UPDATE
both setups are follows:
In H2 version 1.3.x, the database file <databaseName>.h2.db is the default. (The storage engine "PageStore" is used).
In H2 version 1.4.x, the database file <databaseName>.mv.dbis the default. (The storage engine "MVStore" is used). The MVStore is still beta right now (November 2014). But you can disable the MVStore by appending ;mv_store=false to the database URL.
The accepted answer is now several years old and since others may be looking for a more "current" solution...
To get it to work just update the H2 JDBC driver that DBVizualizer uses. Basically download the "Platform-Independent Zip" from http://www.h2database.com/html/download.html and copy the h2/bin/h2-X.X.X.jar file to ~/.dbvis/jdbc/ and then restart DBVizualizer so it can pick up the updated driver.
Also, make sure you remove .mv.db from the file name when setting the Database file name in DBVizualizer.
For Windows Users:
The excellent way to read a *.db.mv file would be locally installing the h2 database and then running that database locally with the java command.
Then your path to the file will definitely show the data from your table until and unless any errors occur.
You can download the h2 database form:
http://www.h2database.com/html/download-archive.html
Note: choose the database version for H2 which supports your file.
You can install the H2 database by installing the downloaded .exe file would be around 7 MB.
then in the bin directory of H2 open a command prompt and run the command
java -jar in my case it is
command: java -jar h2-1.4.200.jar
It will show the console of the H2 database on the browser
Provide the database details:
Driver Class: org.h2.Driver JDBC
URL: jdbc:h2:~/h2 "file path"
User Name: "blank by default"
Password: "blank by default"
Refer SS below
enter image description here

import .sql file in postgres using \i command from java

I am trying to import database from .sql file in postgres using "\i E:/dump.sql" command , its working fine from postgres command prompt but when i try the same from java it raise an error at "\" , my code is
connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/database","postgres", "passwd");
PreparedStatement ps3 = connection.prepareStatement("\\i E:/dump.sql");
boolean res3= ps3.execute();
System.out.println("imported succesfully .."+res3);
With the JDBC driver/interface you can only talk SQL, what you're trying is to issue PostgreSQL commandline tool (psql) specific commands. That won't work.
If you insist on doing this, you could use the Runtime.getRuntime().exec(...) approach, something like
Runtime.getRuntime().exec( "psql -f dump.sql" );
Cheers,
Long story short - you can't. \i is not PostgreSQL command (as in: PostgreSQL database engine). It's command of psql - which is command line tool for interacting with database.
If you're connecting to database via JDBC you're not using psql, so you can't use its commands (\i, \o and alike).

Categories

Resources