I have this method to import data from a CSV file. But the method adds only the PID. Any sugestion to fix this??
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;
}
}
You're trying to load the file with default [load data] options. Might be the chance that the csv file that you're trying to upload have used different format like different FIELD Enclosed By, or different TERMINATED By or different LINE TERMINATED By option. here are the examples to dump csv & load csv files. Please check your csv file format & verify with different options.
-- -- 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';
You can also use csv2jdbc to easily import and export data across csv files and tables no matter which database you are using:
The following statement will create table MY_TABLE
with all columns existing in file.csv
then import the data to the table.
CSV2J COPY MY_TABLE
FROM '/tmp/file.csv'
WITH CSV HEADER CREATE_TABLE DELIMITER ','
The following statement will extract the select statement to file.csv
CSV2J COPY (
SELECT 1 AS ID, 10.99 AS AMOUNT, TIMESTAMP '2022-01-31 23:59:58.987' AS DAT_CREATION
UNION ALL
SELECT 2 AS ID, 7.50 AS AMOUNT, TIMESTAMP '2023-01-31 21:59:58.987' AS DAT_CREATION
) TO '/tmp/file.csv' WITH CSV HEADER
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.