membersound
membersound

Reputation: 86747

How to allow LOAD DATA LOCAL INFILE on mysql-connector-java?

In the past I was using v5 of mysql-connector-java a lot. During LOAD DATA LOCAL INFILE, I usually set the connection to allow loading data from inmemory as follows:

com.mysql.jdbc.Connection con;
con.setAllowLoadLocalInfile(true);

Lateron it then set the file as inputstream directly for the database statement:

((com.mysql.jdbc.Statement) ps).setLocalInfileInputStream(new ByteArrayInputStream(...))

Now I'm migrating to version 8.0.13 of the connector, that the method does not exist anymore. Moreover, one has to use java.sql.Connection now.

How can I now set the property to allow load data on the connection explicit?

Especially as java.sql.Statement does not have the .setLocalInfileInputStream() method?

The developer guide even mentions that method, but does not tell how to get access to it: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-implementation-notes.html

Upvotes: 3

Views: 9333

Answers (5)

Victor Ekpo
Victor Ekpo

Reputation: 41

This can also work in application.properties:

spring.datasource.hikari.data-source-properties.allowLoadLocalInfile=true

For more Data Source properties (not particular to the engine): https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

More info: https://mysqlconnector.net/troubleshooting/load-data-local-infile/#:~:text=To%20allow%20LOAD%20DATA%20LOCAL,connected%20to%20a%20trusted%20server.

Upvotes: 1

Atul
Atul

Reputation: 3357

This is how works for me:

jdbc:mysql://localhost:3306/tempDB?allowLoadLocalInfile=true

It is working for Mysql8.0.18

Upvotes: 4

FilipeSilva
FilipeSilva

Reputation: 366

The preferred method is to set the connection property allowLoadLocalInfile=true in the connection string and then call the method setLocalInfileInputStream(InputStream stream) from a Statement object cast to the interface com.mysql.cj.jdbc.JdbcStatement.

Upvotes: 2

samlii
samlii

Reputation: 21

In case anyone comes looking at this the class needed moved. It is now in com.mysql.cj.jdbc.StatementImpl at least in 8.0.15

Upvotes: 1

membersound
membersound

Reputation: 86747

At the end I switched to mariadb connector, which is a dropin replacement for mysql, but still has the localinfile method.

Upvotes: 1

Related Questions