MySQL使用JDBC Load Data InFile导入数据注意事项

本文介绍如何解决在使用JDBC进行LoadDataInFile操作时遇到的MySQL语法异常问题,并提供正确的SQL语句示例及测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天测试JDBC Connection使用Load Data InFile往数据表中导入数据,Java程序如下:

public class LoadDataTest {

    @Test
    public void test_loadData() throws Exception {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = DBUtils.fetchConnection();
            stmt = conn.createStatement();

            String sql = "load data infile 'c:/test_key_value.txt' into table test_key_value fields terminated by ',' enclosed by '\'' lines terminated by '\r\n'";
            boolean result = stmt.execute(sql);

            System.out.println("Load执行结果:" + result);
        } finally {
            DBUtils.freeConnection();
            DBUtils.closeQuietly(stmt);
            DBUtils.closeDataSource();
        }
    }
}

可是一直报MySQL语法异常:

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 ''' at line 2
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.Util.getInstance(Util.java:386)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2569)
	at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:813)
	at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:656)
	at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
	at org.apache.commons.dbcp.DelegatingStatement.execute(DelegatingStatement.java:264)
	at com.alipay.mbill.loaddata.LoadDataTest.test_loadData(LoadDataTest.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
	at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
	at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

可是把这条语句拿到MySQL命令行中,却是对的,在网上找了一大圈,也没有找到结果,于是自己Debug调试一下,发现SQL的内容如下:

load data infile 'c:/test_key_value.txt' into table test_key_value fields terminated by ',' enclosed by ''' lines terminated by '
'

我X,竟然SQL内容都断开了,那当然不行了,这完全跟在命令行的SQL内容不一样啊,顿时豁然开朗,原来在Java的SQL中少加了转义字符,把SQL改成如下,便可测试通过:

String sql = "load data infile 'c:/test_key_value.txt' into table test_key_value fields terminated by ',' enclosed by '\\'' lines terminated by '\\r\\n'";

这样,最终的SQL内容才为:load data infile 'c:/test_key_value.txt' into table test_key_value fields terminated by ',' enclosed by '\'' lines terminated by '\r\n'

 

附上数据表结构和测试文件内容:

CREATE TABLE `test_key_value` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `key` varchar(32) CHARACTER SET latin1 DEFAULT NULL,
  `value` varchar(128) CHARACTER SET latin1 DEFAULT NULL,
  `gmt_create` timestamp NULL DEFAULT NULL,
  `gmt_modify` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=gbk

测试文件内容:

'1','KEY01','Value01','2012-06-08 15:50:30','2012-06-08 16:50:30'
'2','KEY02','Value02','2012-06-08 15:50:30','2012-06-08 16:50:30'
'3','KEY03','Value03','2012-06-08 15:50:30','2012-06-08 16:50:30'
'4','KEY04','Value04','2012-06-08 15:50:30','2012-06-08 16:50:30'
'5','KEY05','Value05','2012-06-08 15:50:30','2012-06-08 16:50:30'

 

 

根据MySQL的官方文档,继续完善测试用例,可以Load部分数据:

public class LoadDataTest {

    /**
     * 完全导入,包括自增字段
     */
    @Test
    public void test_loadData_ALL() throws Exception {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = DBUtils.fetchConnection();
            stmt = conn.createStatement();

            String sql = "load data infile 'c:/test_key_value.txt' replace into table test_key_value character set GBK fields terminated by ',' enclosed by '\\'' lines terminated by '\\r\\n'";
            boolean result = stmt.execute(sql);

            System.out.println("Load执行结果:" + result);
        } finally {
            DBUtils.freeConnection();
            DBUtils.closeQuietly(stmt);
            DBUtils.closeDataSource();
        }
    }

    /**
     * 部分导入,自增字段自动加1
     */
    @Test
    public void test_loadData_PART() throws Exception {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = DBUtils.fetchConnection();
            stmt = conn.createStatement();

            String sql = "load data infile 'c:/test_key_value_02.txt' replace into table test_key_value character set GBK fields terminated by ',' enclosed by '\\'' lines terminated by '\\r\\n' (`key`,`value`,`gmt_create`,`gmt_modify`)";
            boolean result = stmt.execute(sql);

            System.out.println("Load执行结果:" + result);
        } finally {
            DBUtils.freeConnection();
            DBUtils.closeQuietly(stmt);
            DBUtils.closeDataSource();
        }
    }

}

 

 

转载于:https://www.cnblogs.com/obullxl/archive/2012/06/11/jdbc-mysql-load-data-infile.html

在Java中,可以使用"load data local"命令将数据写入流中,其中只写入一行数据需要遵循以下步骤: 首先,需要确保数据库支持"load data local"命令。使用JDBC连接数据库时,可以通过设置连接属性来指定是否支持该命令。例如,对于MySQL数据库,可以在连接URL中添加 "allowLoadLocalInfile=true"来启用该功能。 接下来,需要创建一个文件流来读取数据文件。可以使用java.io包中的FileReader类对数据文件进行读取操作。例如,可以使用以下代码创建一个FileReader对象: ``` FileReader fileReader = new FileReader("data.txt"); ``` 然后,需要创建一个PreparedStatement对象,并为数据文件中的每一列定义占位符(?)。可以使用JDBC的PreparedStatement对象来执行插入操作。例如,可以使用以下代码创建一个PreparedStatement对象: ``` String sql = "LOAD DATA LOCAL INFILE ? INTO TABLE table_name"; PreparedStatement statement = connection.prepareStatement(sql); ``` 接下来,可以使用PreparedStatement对象的set方法为每个占位符设置相应的值。对于只写入一行数据的情况,可以使用以下代码将数据文件的名称作为参数传递给set方法: ``` statement.setString(1, "data.txt"); ``` 最后,可以执行PreparedStatement对象的executeUpdate方法来执行插入操作,并将数据文件中的数据写入流中。 ``` statement.executeUpdate(); ``` 以上就是在Java中使用"load data local"命令只写入一行数据的简单步骤。需要注意的是,在实际应用中,还需根据具体情况对代码进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值