Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package carRentalRefactoring;
  2.  
  3. import com.opencsv.CSVReader;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import java.io.*;
  12. import java.sql.PreparedStatement;
  13.  
  14. public class CSVFileReader {
  15.  
  16. public static final String csvFile = "C:\\Users\\crm0093\\workspace\\car_data.csv";
  17.  
  18. public static void main(String[] args) {
  19.  
  20. Connection connection = null;
  21. Statement statement = null;
  22.  
  23. String url = "jdbc:oracle:thin:@localhost:1521:xe";
  24. String user = "admin_01";
  25. String password = "admin";
  26. int totalcount = 0;
  27. try {
  28.  
  29. CSVReader reader = new CSVReader(new FileReader(csvFile),';');
  30. String[] nextLine;
  31.  
  32. connection = DriverManager.getConnection(url, user, password);
  33. statement = connection.createStatement();
  34. PreparedStatement executeQuery = connection.prepareStatement(getQuery());
  35.  
  36. while ((nextLine = reader.readNext()) != null) {
  37. if(isValid(nextLine)) {
  38. executeQuery.setString(1, nextLine[0]);
  39. executeQuery.setInt(2, Integer.parseInt(nextLine[1]));
  40. executeQuery.setInt(3, Integer.parseInt(nextLine[2]));
  41. executeQuery.setFloat(4, Float.parseFloat(nextLine[3]));
  42. executeQuery.setInt(5, Integer.parseInt(nextLine[4]));
  43. executeQuery.setInt(6, Integer.parseInt(nextLine[5]));
  44. executeQuery.executeUpdate();
  45. }
  46. totalcount++;
  47. }
  48. reader.close();
  49. } catch (SQLException ex) {
  50. Logger logger = Logger.getLogger(CSVReader.class.getName());
  51. logger.log(Level.SEVERE, ex.getMessage(), ex);
  52.  
  53. } catch(Exception e){
  54. e.printStackTrace();
  55. } finally {
  56. try {
  57. if (statement != null) {
  58. statement.close();
  59. }
  60. if (connection != null) {
  61. connection.close();
  62. }
  63.  
  64. } catch (SQLException ex) {
  65. Logger logger = Logger.getLogger(CSVReader.class.getName());
  66. logger.log(Level.WARNING, ex.getMessage(), ex);
  67. }
  68. }
  69. System.out.println(totalcount);
  70. }
  71.  
  72. private static String getQuery() {
  73. return "insert into CARS(registration_number,current_mileage,daily_rate,engine_size,production_year,year_rating)"
  74. +"values(?,?,?,?,?,?)";
  75. }
  76.  
  77.  
  78. private static boolean isValid(String[] values) {
  79. if(values[0] == null ||
  80. Integer.parseInt(values[1]) < 0 ||
  81. Integer.parseInt(values[2]) < 0||
  82. Float.parseFloat(values[3]) < 0 ||
  83. Integer.parseInt(values[4]) < 0 ||
  84. Integer.parseInt(values[5]) < 0)
  85. throw new IllegalArgumentException("Input is too small.");
  86. return true;
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement