Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. private static String getCreateTable1(Connection con, String tablename) {
  2.  
  3. try {
  4. Class.forName("com.mysql.jdbc.Driver");
  5. Statement stmt = con.createStatement();
  6. String createtable = "CREATE TABLE " + tablename
  7. + " ( text VARCHAR(255), price INT , day VARCHAR(255), litres FLOAT )";
  8. System.out.println("Create a new table in the database");
  9. stmt.executeUpdate(createtable);
  10. } catch (Exception e) {
  11. System.out.println(((SQLException) e).getSQLState());
  12. System.out.println(e.getMessage());
  13. e.printStackTrace();
  14. }
  15.  
  16. return null;
  17. }
  18.  
  19. private static String importData(Connection con, File txtFile,
  20. String tablename) {
  21.  
  22. try {
  23. Statement stmt;
  24.  
  25. stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  26. ResultSet.CONCUR_UPDATABLE);
  27. String path = txtFile.getAbsolutePath();
  28. String importingdata = "LOAD DATA INFILE '"
  29. + path.replace('\', '/')
  30. + "' INTO TABLE " + tablename
  31. + " FIELDS TERMINATED BY 't'";
  32. System.out.println("fill the table");
  33. stmt.executeUpdate(importingdata);
  34.  
  35. } catch (Exception e) {
  36. System.out.println(((SQLException) e).getSQLState());
  37. System.out.println(e.getMessage());
  38. e.printStackTrace();
  39.  
  40. }
  41. return null;
  42. }
  43.  
  44. LOAD DATA INFILE 'myfile.txt'
  45. INTO TABLE tablename
  46. FIELDS TERMINATED BY 't'
  47. (COL1, @litres, COL3, ...) -- Note the @ in front of the float column
  48. SET litres = REPLACE(@litres, ',', '.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement