Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class dbconnection {
- /**
- *
- */
- static DataInputStream dis;
- static Connection databaseConnection;
- static FileInputStream fis;
- static BufferedInputStream bis;
- static File file;
- private static void openFile(String taxitraces) {
- File file = new File(taxitraces);
- try
- {
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- dis = new DataInputStream(bis);
- }
- catch (FileNotFoundException ex)
- {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void connectDB(String host,String port,String dbname,String username,String passwd)
- {
- try
- {
- Class.forName("org.postgresql.Driver");
- }
- catch (ClassNotFoundException e)
- {
- System.out.print("Where is your PostgreSQL JDBC Driver? Include in your library path!");
- e.printStackTrace();
- }
- try
- {
- databaseConnection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+dbname, username, passwd);
- }
- catch (SQLException e)
- {
- System.out.println("Connection Failed! Check output console: ");
- e.printStackTrace();
- System.exit(1);
- }
- if (databaseConnection == null)
- System.out.println("Failed to make connection!");
- else
- System.out.println("Connected to: "+"jdbc:postgresql://"+host+":"+port+"/"+dbname);
- }
- public static void closeFile() {
- try
- {
- if(fis != null) fis.close();
- if(bis != null) bis.close();
- if(dis != null) dis.close();
- }
- catch (IOException ex)
- {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- file = null;
- fis = null;
- bis = null;
- dis = null;
- }
- /**
- * @param args
- * @throws IOException
- * @throws NumberFormatException
- */
- public static void main(String[] args) throws NumberFormatException, IOException {
- // TODO Auto-generated method stub
- String fileName = "****";
- String host="***";
- String port="***";
- String dbname="***";
- String username="***";
- String passwd="***";
- openFile(fileName);
- connectDB(host,port,dbname,username,passwd);
- Statement sql=null;
- try {
- sql = databaseConnection.createStatement();
- }catch (SQLException e)
- {
- System.err.println("Could not create statement in JDBC: " + e.getErrorCode());
- e.printStackTrace();
- }
- while(dis.available() != 0){
- try {
- // read line
- String[] fileData;
- String line = dis.readLine().replaceAll(""", "");
- fileData = line.split(",");
- int id = Integer.parseInt(fileData[0]);
- int driver_id = Integer.parseInt(fileData[1]);
- String timestamp = fileData[2];
- boolean in_service = Boolean.parseBoolean(fileData[3]);
- int trips_pos = Integer.parseInt(fileData[4]);
- double lat = Double.parseDouble(fileData[5]);
- double lon = Double.parseDouble(fileData[6]);
- int parish_id = Integer.parseInt(fileData[7]);
- double distance = Double.parseDouble(fileData[8]);
- int duration = Integer.parseInt(fileData[9]);
- int trip_id = Integer.parseInt(fileData[10]);
- double taxi_fare = Double.parseDouble(fileData[11]);
- String query = " INSERT INTO aid_atidivyakumarpatra "
- + "(id, driver_id, timestamp, in_service, trip_pos,lat, lon, parish_id, distamce, duration, trip_id, taxi_fare)"
- + "VALUES ("+id+",'"+driver_id+"',"+timestamp+","+in_service+","+trips_pos+","
- + lat+","+lon+",ST_GeomFromText('POINT("+lat+" "+lon+")', 4326) , "+parish_id+","+distance+", "
- + duration+","+trip_id +","+taxi_fare +")";
- try {
- System.out.println(query);
- sql.executeUpdate(query);
- }
- catch (SQLException e)
- {
- System.err.println("Could not create statement in JDBC: " + e.getErrorCode());
- e.printStackTrace();
- }
- //…
- }
- catch (IOException ex)
- {
- Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- closeFile();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement