Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package migaenko.ru;
  2.  
  3. import java.io.*;
  4. import java.nio.charset.Charset;
  5. import java.sql.*;
  6.  
  7.  
  8. /**
  9.  * Created by Егор Мигаенко on 19.10.2017.
  10.  */
  11. public class Solution {
  12.     public static void main (String[] atrgs){
  13.         //load from file
  14.         File file = new File("Kody_ABC-4kh.txt");
  15.  
  16.  
  17.         //connecting to database
  18.  
  19.         try {
  20.             Class.forName("com.mysql.jdbc.Driver");
  21.         } catch (ClassNotFoundException e) {
  22.             e.printStackTrace();
  23.         }
  24.         try {
  25.             Connection connection = DriverManager.getConnection(
  26.                     "jdbc:mysql://localhost:3306/lab2_2", "root", "524656bnm"
  27.             );
  28.  
  29.  
  30.             PreparedStatement statement = connection.prepareStatement("insert into `data` " +
  31.                                                                         "(prefix, range_from, range_to, capacity, operator, region) " +
  32.                                                                         "value (?,?,?,?,?,?);");
  33.  
  34.  
  35.             String[] str;
  36.             String line;
  37.  
  38.  
  39.  
  40.  
  41.             try {
  42.                 BufferedReader reader = new BufferedReader(
  43.                         new InputStreamReader(
  44.                                 new FileInputStream(file), Charset.forName("utf8")));
  45.                 int count = -1;
  46.                 System.out.print("Loading");
  47.                 try{
  48.                     while ((line = reader.readLine())!=null) {
  49.                         if (count++ == -1) continue;
  50.                         else {
  51.                             str = line.split(";");
  52.                             if (str.length > 1) {
  53.                                 statement.setString(1, str[0].trim());
  54.                                 statement.setString(2, str[1].trim());
  55.                                 statement.setString(3, str[2].trim());
  56.                                 statement.setString(4, str[3].trim());
  57.                                 statement.setString(5, str[4].trim());
  58.                                 statement.setString(6, str[5].trim());
  59.                                 statement.addBatch();
  60.  
  61.                                 if (count > 200) {
  62.                                     statement.executeBatch();
  63.                                     statement.clearBatch();
  64.                                     count = 0;
  65.                                     System.out.print(".");
  66.  
  67.                                 }
  68.  
  69.                             }
  70.  
  71.  
  72.                         }
  73.  
  74.                     }
  75.                     statement.executeBatch();
  76.                     statement.clearBatch();
  77.                     statement.close();
  78.                     connection.close();
  79.                 }
  80.                 catch (IOException e){
  81.                     e.printStackTrace();
  82.                 }
  83.             } catch (FileNotFoundException e) {
  84.                 e.printStackTrace();
  85.             }
  86.  
  87.  
  88.         } catch (SQLException e) {
  89.             e.printStackTrace();
  90.         }
  91.  
  92.  
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement