连接数据库后从Java代码运行.sql文件

时间:2015-05-22 16:05:31

标签: java mysql jdbc

我有以下.sql文件:

CREATE DATABASE  IF NOT EXISTS `peertopeer` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `peertopeer`;
DROP TABLE IF EXISTS `songs`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `songs` (
  `idsongs` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `artist` varchar(45) DEFAULT NULL,
  `album` varchar(45) DEFAULT NULL,
  `genre` varchar(45) DEFAULT NULL,
  `year` int(11) DEFAULT NULL,
  `hash` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`idsongs`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `songs`
--

LOCK TABLES `songs` WRITE;
/*!40000 ALTER TABLE `songs` DISABLE KEYS */;
INSERT INTO `songs` VALUES (1,'requiem','mozart','unknown','classical',1791,'9a1be396c886d97a60f5d4f9d6cd1f08'),(2,'four seasons','vivaldi','unknown','classical',1725,'425f51ff63ff6621819dedb448704dba'),(3,'temple of the king','\"\"','ritchie blackmore\'s rainbow','classical  rock',1975,'a6369ea7a1fd7fb1fc7c055218f894ac'),(4,'holy diver','dio','holy diver','heavy metal',1983,'5cc1847e0788eb9aaf7cab43460a22b5'),(5,'vodka','korpiklaani','karkelo','folk metal',NULL,'97ffd653375a1d2aadda9206c92b03c0'),(6,'\"\"','miles davis','\"\"','\"\"',NULL,'82f7d372319db9b5f0b54e2c8517dd81'),(7,'i don\'t want to set the world on fire','the ink spots','smoothie','jazz',1941,'8539ef14ef46a611d346ad15f32d784d');
/*!40000 ALTER TABLE `songs` ENABLE KEYS */;
UNLOCK TABLES;

现在在我的java代码中,我已经使用JDBC连接到mysql。但是,我找不到运行此文件的方法,以便执行sql命令并创建相应的表。我怎么能这样做?我知道有很多这样的问题,但大多数人都说JDBC不支持这样的操作。

0 个答案:

没有答案