i am making a program where I would read data from txt files and store them in tables in mysql. In my program I am making the table with the fields that I would like to have and then with the command of "Load Data Infile" I am inserting the values in the table.
The files that I have contains a column (named as litres)
with data that are float numbers such as
0,234
12,234
3,004 etc.
At the beginning of my program when i create the table, the litres field is described as FLOAT. But when i ran my program I get an SQLException : Data truncated for column 'litres' at row 1.
The code for creating the table and inserting the values are the below:
private static String getCreateTable1(Connection con, String tablename) {
try {
Class.forName("com.mysql.jdbc.Driver");
Statement stmt = con.createStatement();
String createtable = "CREATE TABLE " + tablename
+ " ( text VARCHAR(255), price INT , day VARCHAR(255), litres FLOAT )";
System.out.println("Create a new table in the database");
stmt.executeUpdate(createtable);
} catch (Exception e) {
System.out.println(((SQLException) e).getSQLState());
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
private static String importData(Connection con, File txtFile,
String tablename) {
try {
Statement stmt;
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String path = txtFile.getAbsolutePath();
String importingdata = "LOAD DATA INFILE '"
+ path.replace('\\', '/')
+ "' INTO TABLE " + tablename
+ " FIELDS TERMINATED BY '\t'";
System.out.println("fill the table");
stmt.executeUpdate(importingdata);
} catch (Exception e) {
System.out.println(((SQLException) e).getSQLState());
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
How can i fix it? Could anyone help me? I think that the problem is the comma? How can i make it recognize it?