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!
Asked
Active
Viewed 3,127 times
1 Answers
4
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

Manse
- 37,765
- 10
- 83
- 108
-
Thank you.How can I spesify the full path of the backup? I now have it in the same dir that the classes of my netbeans are. – Vagelism Mar 05 '12 at 14:11
-
2Replace `<
` with `< /full/path/to/your/backup/file` if on not windows and with `< \\full\\path\\to\\your\\backup\\file` if on windows. – DwB Mar 05 '12 at 14:14 -
@Vagelism just put the full path where it currently says `
` – Manse Mar 05 '12 at 14:14 -
Runtime.getRuntime().exec("mysql -u "+username+ " -p "+ password + "TestDB < "+ getClass().getResourceAsStream("File.sql")); – Vagelism Mar 05 '12 at 14:15
-
Please note the spacing / no spacing between the switches and the username / passwords – Manse Mar 05 '12 at 14:16
-
After some experiment I show that this command will runn only if the Database is in the server even empty , just the name. But how to do it work with empty sql server that I have access? – Vagelism Mar 05 '12 at 14:43
-
@Vagelism [create the database first](http://stackoverflow.com/questions/717436/create-mysql-database-from-java) ? – Manse Mar 05 '12 at 14:45