1

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;
        }
    }

1 Answer 1

2

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';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.