我正在尝试使用以下示例查询从本地csv文件加载内容:
SET GLOBAL local_infile=1; LOAD DATA LOCAL INFILE 'C:/afc_orders.csv' INTO TABLE `afc_report`.`afc_orders` FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS (`type_o`,`n_order`,`n_return`);从MySQL执行查询可以很好地工作,但是,当我尝试从JDBC执行相同的查询时,我得到一个语法错误:
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 'LOAD DATA LOCAL INFILE 'C:/afc_orders.csv' INTO TABLE `afc_report`.`afc_orders` ' at line 1下面是我的代码:
Connection conn = null;
String url = "jdbc:mysql://" + mysqlHost + ":" + mysqlPort + "/" + mysqlDatabase + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try {
conn = DriverManager.getConnection(url, mysqlUser, mysqlPassword);
Statement stmt = conn.createStatement();
String sql = "SET GLOBAL local_infile=1; LOAD DATA LOCAL INFILE 'C:/afc_orders.csv' INTO TABLE `afc_report`.`afc_orders` FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (`type_o`,`n_order`,`n_return`);";
System.out.println(sql);
stmt.execute(sql);
} catch(Exception e) {
System.out.println(e.getMessage());
}下面是我使用的驱动程序:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>我遗漏了什么?
发布于 2020-05-22 02:07:44
您的代码试图一次执行两个语句( SET语句和LOAD DATA语句),但是Statement.execute(...)只接受单个语句,而不接受多个语句。
您可以尝试单独执行这些语句(我通常不使用MySQL,因此我不确定在这种情况下是否有效),或者可以通过在连接字符串中添加连接属性allowMultiQueries=true,将MySQL连接器/J驱动程序配置为允许多个语句。
发布于 2020-05-22 14:56:49
为了便于将来参考,我还必须添加:
&allowLoadLocalInfile=true设置为连接字符串以避免错误:
ERROR 3950 (42000): Loading local data is disabled; this must be enabled on both the client and server sidehttps://stackoverflow.com/questions/61936555
复制相似问题