Connect to database and export to sql script

138 views Asked by At

I am looking for some way to connect to a MySQL database and export the structure and data into an SQL script that I could run again to import the data.

I am trying to do this using Java but have had no luck finding anything other than hibernates SchemaExport tool which I can't use because I do not have access to the hibernate cfg file.

Can anyone recommend any other tools or methods of doing this using Java?

Thanks

Edit:

 AnnotationConfiguration cfg = new AnnotationConfiguration().configure( "hibernate.cfg.xml" );
    SchemaExport schemaExport = new SchemaExport( cfg );
    schemaExport.setOutputFile( destination );
    schemaExport.setFormat( format );
    schemaExport.execute( true, false, false, create );

The above code is what I have tried to do using SchemaExport that comes with hibernate, but I do not have access to hibernate.cfg.xml.

1

There are 1 answers

0
Gauthier JACQUES On BEST ANSWER

I would not recommend doing this using Java, but with 'mysqldump' tool (provided with MySQL) instead (more info on mysqldump).

Anyway, if you really want to do this with java, you can invoke 'mysqldump' in Java with Runtime().getRuntime().exec("..."), or preferably with some libraries like Apache Commons Exec.