Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. public class dbconnection {
  2.  
  3. /**
  4. *
  5. */
  6. static DataInputStream dis;
  7. static Connection databaseConnection;
  8. static FileInputStream fis;
  9. static BufferedInputStream bis;
  10. static File file;
  11.  
  12.  
  13.  
  14. private static void openFile(String taxitraces) {
  15.  
  16. File file = new File(taxitraces);
  17. try
  18. {
  19. FileInputStream fis = new FileInputStream(file);
  20. BufferedInputStream bis = new BufferedInputStream(fis);
  21. dis = new DataInputStream(bis);
  22. }
  23. catch (FileNotFoundException ex)
  24. {
  25. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  26. }
  27.  
  28. }
  29.  
  30. public static void connectDB(String host,String port,String dbname,String username,String passwd)
  31. {
  32. try
  33. {
  34. Class.forName("org.postgresql.Driver");
  35. }
  36. catch (ClassNotFoundException e)
  37. {
  38. System.out.print("Where is your PostgreSQL JDBC Driver? Include in your library path!");
  39. e.printStackTrace();
  40. }
  41. try
  42. {
  43. databaseConnection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+dbname, username, passwd);
  44. }
  45. catch (SQLException e)
  46. {
  47. System.out.println("Connection Failed! Check output console: ");
  48. e.printStackTrace();
  49. System.exit(1);
  50. }
  51. if (databaseConnection == null)
  52. System.out.println("Failed to make connection!");
  53. else
  54. System.out.println("Connected to: "+"jdbc:postgresql://"+host+":"+port+"/"+dbname);
  55. }
  56.  
  57. public static void closeFile() {
  58. try
  59. {
  60. if(fis != null) fis.close();
  61. if(bis != null) bis.close();
  62. if(dis != null) dis.close();
  63. }
  64. catch (IOException ex)
  65. {
  66. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  67. }
  68. file = null;
  69. fis = null;
  70. bis = null;
  71. dis = null;
  72. }
  73.  
  74. /**
  75. * @param args
  76. * @throws IOException
  77. * @throws NumberFormatException
  78. */
  79. public static void main(String[] args) throws NumberFormatException, IOException {
  80. // TODO Auto-generated method stub
  81.  
  82. String fileName = "****";
  83. String host="***";
  84. String port="***";
  85. String dbname="***";
  86. String username="***";
  87. String passwd="***";
  88.  
  89. openFile(fileName);
  90.  
  91. connectDB(host,port,dbname,username,passwd);
  92. Statement sql=null;
  93. try {
  94. sql = databaseConnection.createStatement();
  95. }catch (SQLException e)
  96. {
  97. System.err.println("Could not create statement in JDBC: " + e.getErrorCode());
  98. e.printStackTrace();
  99. }
  100. while(dis.available() != 0){
  101. try {
  102. // read line
  103. String[] fileData;
  104. String line = dis.readLine().replaceAll(""", "");
  105. fileData = line.split(",");
  106. int id = Integer.parseInt(fileData[0]);
  107. int driver_id = Integer.parseInt(fileData[1]);
  108. String timestamp = fileData[2];
  109. boolean in_service = Boolean.parseBoolean(fileData[3]);
  110. int trips_pos = Integer.parseInt(fileData[4]);
  111. double lat = Double.parseDouble(fileData[5]);
  112. double lon = Double.parseDouble(fileData[6]);
  113. int parish_id = Integer.parseInt(fileData[7]);
  114. double distance = Double.parseDouble(fileData[8]);
  115. int duration = Integer.parseInt(fileData[9]);
  116. int trip_id = Integer.parseInt(fileData[10]);
  117. double taxi_fare = Double.parseDouble(fileData[11]);
  118.  
  119.  
  120.  
  121. String query = " INSERT INTO aid_atidivyakumarpatra "
  122. + "(id, driver_id, timestamp, in_service, trip_pos,lat, lon, parish_id, distamce, duration, trip_id, taxi_fare)"
  123. + "VALUES ("+id+",'"+driver_id+"',"+timestamp+","+in_service+","+trips_pos+","
  124. + lat+","+lon+",ST_GeomFromText('POINT("+lat+" "+lon+")', 4326) , "+parish_id+","+distance+", "
  125. + duration+","+trip_id +","+taxi_fare +")";
  126.  
  127. try {
  128. System.out.println(query);
  129. sql.executeUpdate(query);
  130.  
  131. }
  132. catch (SQLException e)
  133. {
  134. System.err.println("Could not create statement in JDBC: " + e.getErrorCode());
  135. e.printStackTrace();
  136. }
  137. //…
  138. }
  139. catch (IOException ex)
  140. {
  141. Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
  142. }
  143. }
  144.  
  145. closeFile();
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement