我有这种方法从CSV文件导入数据。 但该方法仅添加PID。任何消化来修复这个??
public void importData(){
try{
Import path = new Import();
String filename = path.getFileName();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
st = con.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_UPDATABLE);
String query = "LOAD DATA LOCAL INFILE '" + filename + "' INTO TABLE Pacient (PID, Nume,Prenume, Varsta, CNP, Adresa);";
st.execute(query);
}
catch (SQLException | HeadlessException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
st = null;
}
}
答案 0 :(得分:2)
您尝试使用默认[加载数据]选项加载文件。 您尝试上传的csv文件可能使用不同的格式,例如不同的FIELD Enclosed By,或不同的TERMINATED By或不同的LINE TERMINATED By选项。 以下是转储csv和amp;的示例加载csv文件。请检查您的csv文件格式&用不同的选项验证。
-- -- To dump sql data to csv file
SELECT *
FROM test.yourTableName
INTO OUTFILE 'C:/test/yourTableName.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
-- -- To load data to sql database from csv
LOAD DATA INFILE 'C:/test/yourTableName.csv' INTO TABLE test.yourTableName
FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';