我正在使用jdbc mysqldatasource类来处理我的应用程序的数据库。我正在尝试创建一个方法来格式化数据库以供应用程序使用—创建表和约束。基本上,它是由phpmyadmin导出选项生成的脚本。我用包含脚本的静态字符串创建了一个名为databasetemplate的类,然后在formatdatabase方法中使用它。
public void formatDatabase() {
try {
sql.executeUpdate(DatabaseTemplate.GetScript());
} catch (SQLException e) {
System.out.println("Connection not found.");
e.printStackTrace();
}
}
它导致sql异常“语法不正确”。我的脚本字符串如下所示:
CREATE TABLE `arenas` (
`arena_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`location` varchar(50) COLLATE utf16_polish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `contestants` (
`contestant_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`surname` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`nickname` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`score` int(11) NOT NULL,
`language` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`contact_info` text COLLATE utf16_polish_ci NOT NULL,
`additional_info` text COLLATE utf16_polish_ci,
`team_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `matches` (
`match_id` int(11) NOT NULL,
`sideA` int(11) NOT NULL,
`sideB` int(11) NOT NULL,
`sideA_score` int(11) DEFAULT NULL,
`sideB_score` int(11) DEFAULT NULL,
`time` datetime NOT NULL,
`tournament` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `system_users` (
`sys_usr_id` int(11) NOT NULL,
`login` varchar(20) COLLATE utf16_polish_ci NOT NULL,
`pw_hash` int(32) NOT NULL,
`permissions` varchar(5) COLLATE utf16_polish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `teams` (
`team_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`where_from` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`leader_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `tournaments` (
`tournament_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`type` enum('solo','team') COLLATE utf16_polish_ci NOT NULL,
`arena_id` int(11) NOT NULL,
`operator` int(11) NOT NULL,
`additional_info` text COLLATE utf16_polish_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
ALTER TABLE `arenas`
ADD PRIMARY KEY (`arena_id`);
ALTER TABLE `contestants`
ADD PRIMARY KEY (`contestant_id`),
ADD KEY `team_id` (`team_id`);
ALTER TABLE `matches`
ADD PRIMARY KEY (`match_id`),
ADD KEY `sideA` (`sideA`),
ADD KEY `sideB` (`sideB`),
ADD KEY `tournament` (`tournament`);
ALTER TABLE `system_users`
ADD PRIMARY KEY (`sys_usr_id`);
ALTER TABLE `teams`
ADD PRIMARY KEY (`team_id`),
ADD KEY `leader_id` (`leader_id`);
ALTER TABLE `tournaments`
ADD PRIMARY KEY (`tournament_id`),
ADD KEY `arena_id` (`arena_id`),
ADD KEY `operator` (`operator`);
ALTER TABLE `contestants`
MODIFY `contestant_id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `contestants`
ADD CONSTRAINT `contestants_ibfk_1` FOREIGN KEY (`team_id`) REFERENCES `teams` (`team_id`);
ALTER TABLE `matches`
ADD CONSTRAINT `matches_ibfk_1` FOREIGN KEY (`sideA`) REFERENCES `teams` (`team_id`),
ADD CONSTRAINT `matches_ibfk_2` FOREIGN KEY (`sideB`) REFERENCES `teams` (`team_id`),
ADD CONSTRAINT `matches_ibfk_3` FOREIGN KEY (`tournament`) REFERENCES `tournaments` (`tournament_id`);
ALTER TABLE `teams`
ADD CONSTRAINT `teams_ibfk_1` FOREIGN KEY (`leader_id`) REFERENCES `contestants` (`contestant_id`);
ALTER TABLE `tournaments`
ADD CONSTRAINT `tournaments_ibfk_1` FOREIGN KEY (`arena_id`) REFERENCES `arenas` (`arena_id`),
ADD CONSTRAINT `tournaments_ibfk_2` FOREIGN KEY (`operator`) REFERENCES `system_users` (`sys_usr_id`);
我猜这是因为java中的字符串如何使用\n。我尝试删除它,将其更改为\r\n,但仍然是相同的例外。如何格式化此脚本以使其正确执行?
1条答案
按热度按时间4dc9hkyq1#
阅读有关发出sql查询的java文档可能会更好,因为您的代码中似乎使用了错误的方法。
如果您对ddl(数据定义语言)和dml(数据操作语言)之间的区别感到好奇,那么wikipedia上就有关于它们的有价值的文章。
您可能会发现将sql放在专用文件中更容易
setup.sql
然后在需要时加载文件并从文件中执行sql。如果代码包含多个sql语句,则可以将每个sql语句存储在单独的文件中。这样,您的sql查询将:
实际上是可读的人和
java中的多行字符串不会有问题。