Thursday, April 12, 2012

MySQL How do you import a .sql file using jdbc?

Scenerio



I had a blank database I created in mysql



mysql> create database Frog;
mysql> exit


I'm trying to use java jdbc driver to import the tables from a sql file



frog.sql file



DROP TABLE IF EXISTS `Frog`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Frog` (
`Frog_ID` int(11) NOT NULL AUTO_INCREMENT,
`Frog_NAME` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Frog_ID`),
UNIQUE KEY `DEV_NAME` (`Frog_NAME`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;


java code



 public static void setUpTables()
{
try
{

ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c",
"mysql -u root Frog < ./Frog.sql");
Process pr = builder.start();


Problem: How do I go about this? Processbuilder ? Or some other type of jdbc api that I havent discovered yet?



Desired results: Be able to call java method to populate a database from a sql file



on the same lines as doing this but with java:



username$ mysql -uroot Frog < location/Frog.sql




No comments:

Post a Comment