gpt4 book ai didi

mysql - 在jpa中运行sql脚本时sql语法错误

转载 作者:行者123 更新时间:2023-11-29 10:02:46 25 4
gpt4 key购买 nike

我正在 java 中的 persistence.xml 文件中运行 MySQL 脚本,以将一些值输入到一些 MySQL 表中:

INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Ida','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Ellen','Mortensen','female'); INSERT INTO names(name,surname,gender) VALUES('Mathilde','Sørensen','female'); INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); INSERT INTO names(name,surname,gender) VALUES('Ella','Rasmussen','female'); INSERT INTO names(name,surname,gender) VALUES('Maja','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Sofie','Madsen','female'); INSERT INTO names(name,surname,gender) VALUES('Silje','Vad','female'); INSERT INTO names(name,surname,gender) VALUES('Sofie','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Vigga','Petersen','female'); INSERT INTO names(name,surname,gender) VALUES('Victoria','Christensen','female'); INSERT INTO names(name,surname,gender) VALUES('Liv','Larsen','female'); INSERT INTO names(name,surname,gender) VALUES('Malou','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Merle','Olsen','female'); INSERT INTO names(name,surname,gender) VALUES('Alberte','Andersen','female'); INSERT INTO names(name,surname,gender) VALUES('Emma','Kristensen','female'); INSERT INTO names(name,surname,gender) VALUES('Filippa','Christiansen','female'); INSERT INTO names(name,surname,gender) VALUES('Johanne','Poulsen','female'); INSERT INTO names(name,surname,gender) VALUES('Vigga','Jørgensen','female'); INSERT INTO names(name,surname,gender) VALUES('Ellen','Thomsen','female'); INSERT INTO names(name,surname,gender) VALUES('Caroline','Møller','female'); INSERT INTO names(name,surname,gender) VALUES('Laura','Møller','female'); INSERT INTO names(name,surname,gender) VALUES('Thea','Knudsen','female'); 

当我在 MySQL 工作台中运行该脚本时,它运行良好(某些条目具有相同的名称,因为我将其用作外键)。

但是当我从 persistence.xml 文件运行相同的脚本时,我收到此错误:

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female'); INSERT ' at line 1

错误代码:1064

这是我的 persistence.xml 代码:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="samplePU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/Women?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="*****"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source" value="createData.sql"/>
</properties>
</persistence-unit>
</persistence>

当我测试了语法后,为什么会出现这个问题?

最佳答案

您应该更新您的“脚本”以在所有 INSERT 语句之间放置行结束符。像这样

INSERT INTO names(name,surname,gender) VALUES('Emilie','Knudsen','female'); 
INSERT INTO names(name,surname,gender) VALUES('Aya','Knudsen','female');
INSERT INTO names(name,surname,gender) VALUES('Ida','Jørgensen','female');
INSERT INTO names(name,surname,gender) VALUES('Ellen','Mortensen','female');

一些 JPA 提供程序可以确定行结束位置并分隔 JDBC 语句,但其他提供程序则不这样做。您似乎将所有 JDBC 内容都放在一个语句中,这将会失败,正如您所看到的。您还应该在 JPA 提供程序上提出错误来处理此类情况。

关于mysql - 在jpa中运行sql脚本时sql语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com