You can use the mysql command mysqldump to copy contents of your database from the command prompt.

I know its not java code but if you do it programatically you will first have to install java...