I have written the following codes, however I will need to skip the first line as it is the field name. My file here is cart.csv. When the code read the first line i will get errors.
cart.csv
Number,Item,Quantity
1,Book,10
2,Fruit,14
3,Toy,2
code
try {
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] data = line.split(",");
String sql = "INSERT INTO DB (Number,Item,Quantity) values (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, data[0]);
pstmt.setString(2, data[1]);
pstmt.setString(3, data[2]);
pstmt.executeUpdate();
System.out.println("Upload Completed");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException ex) {
Logger.getLogger(POI.class.getName()).log(Level.SEVERE, null, ex);
}
Add a readLine() before the while loop to skip the first line.
br = new BufferedReader(new FileReader(file));
br.readLine(); //read the first line and throw it away
while ((line = br.readLine()) != null) {
br = new BufferedReader(new FileReader(file));
br.readLine() // Read first line and skip...
while ((line = br.readLine()) != null) { .......
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With