Guest User

Untitled

a guest
Jan 30th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package aisehi2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.Statement;
  12.  
  13. public class App {
  14.  
  15. public static void main(String[] args) throws Exception {
  16.  
  17. FileReader fr = new FileReader("myfile2.txt");
  18. BufferedReader br = new BufferedReader(fr);
  19.  
  20. String str = null;
  21. int count = 0;
  22.  
  23. long start = System.currentTimeMillis();
  24.  
  25. Path path = Paths.get("myfile2.txt");
  26. long lineCount = Files.lines(path).count();
  27. System.out.println("Data enties available in file:" + (lineCount-1));
  28.  
  29. if(lineCount==1000001)
  30. {
  31. String query = "insert into data2 values(?,?,?,?,?)";
  32. String url = "jdbc:oracle:thin:@localhost:1521:xe";
  33. Class.forName("oracle.jdbc.driver.OracleDriver");
  34. Connection con = DriverManager.getConnection(url, "system", "system");
  35. PreparedStatement ps = con.prepareStatement(query);
  36. Statement st = con.createStatement();
  37. st.executeUpdate("truncate table data2");
  38.  
  39. while (!(str = br.readLine()).equalsIgnoreCase("Trailer1000001")) {
  40. String[] str1 = str.split("\\|");
  41.  
  42. ps.setString(1, str1[0]);
  43. ps.setString(2, str1[1]);
  44. ps.setString(3, str1[2]);
  45. ps.setString(4, str1[3]);
  46. ps.setString(5, str1[4]);
  47.  
  48. count = count + ps.executeUpdate();
  49. }
  50.  
  51. long end = System.currentTimeMillis();
  52. System.out.println("Time elapsed = "+(end-start)+"ms");
  53.  
  54. System.out.println(count+"row/s updated");
  55.  
  56. }
  57.  
  58. else {
  59. try {
  60. throw new Exception() ;
  61. }
  62. catch(Exception e) {
  63. System.out.println("Exception: The entries in file are either less or more than 1 million records.");
  64. }
  65. }
  66.  
  67. }
  68. }
Add Comment
Please, Sign In to add comment