简体   繁体   中英

import csv to mysql with java

so i have a csv file, and want to import it to mysql, so far in phpmyadmin i can import it with the following line:

LOAD DATA INFILE 'C:\\Users\\Angel Silva\\Documents\\test.csv' INTO TABLE registros FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY '\n' 

my java program is:

public class recover {
public static void main(String[] args){
    try {
        String filename ="C:\\Users\\Angel Silva\\Documents\\test.csv";
        String query=null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gg", "root", "");
        Statement stmt = conn.createStatement();
        query = "LOAD DATA INFILE 'C:\\Users\\Angel Silva\\Documents\\test.csv' INTO TABLE registros FIELDS ENCLOSED BY '\"' TERMINATED BY ',' ESCAPED BY '\"' LINES TERMINATED BY '\\n'";
        stmt.executeQuery(query);
        ResultSet rs = stmt.executeQuery(query);   
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(recover.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(recover.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(recover.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(recover.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

i get the following errors:

ene 24, 2016 1:28:33 PM recover main
SEVERE: null
java.sql.SQLException: Row 1 was truncated; it contained more data tha there were input columns
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at recover.main(recover.java:29)

as i already have said if i input the sentence from the query directly into phpymyadmin i can recover the table but in java doesnt work

any idea what im doing wrong?

LINES TERMINATED BY '\\r\\n'

(因为它是Windows)

所以最后在阅读mysql doc之后,我将代码修改为:

query="LOAD DATA INFILE 'C:\\\\Users\\\\Angel Silva\\\\Documents\\\\251.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n' ";

you must try by appending the table columns names to the query string

" LOAD DATA INFILE 'C:\\Users\\Angel Silva\\Documents\\251.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' (col1,col2,col3) ";

In my case it solved the problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM