Advertisement
Guest User

aaaaa

a guest
Oct 20th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package rusanov.phone.book;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.nio.charset.StandardCharsets;
  9. import java.util.Properties;
  10. import java.sql.*;
  11. public class PhoneBook {
  12.  
  13.     public static void main(String[] args) {
  14.        
  15.         try {
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.              String dbURL = "jdbc:mysql://localhost:3306/phones";
  18.             String dbUsername = "root";
  19.             String dbPassword = "root";
  20.          
  21.             java.util.Properties connProperties = new java.util.Properties();
  22.             connProperties.put("user", dbUsername);
  23.             connProperties.put("password", dbPassword);
  24.             connProperties.put("useUnicode","true");
  25.             connProperties.setProperty("characterEncoding","cp1251");
  26.             Connection con = DriverManager.getConnection(dbURL,connProperties);
  27.             PreparedStatement ps = con.prepareStatement("INSERT INTO T_SRC VALUES (?,?,?,?,?,?)");
  28.            
  29.            
  30.             //truncateTable(con,"t_numbers");
  31.             //truncateTable(con,"t_operator");
  32.             //truncateTable(con,"t_region");
  33.             String tableName = "T_SRC";
  34.             cleanTable(con,tableName);
  35.            
  36.            
  37.            
  38.            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Nik\\eclipse-workspace\\PhonesBook\\Kody_ABC-8kh.txt")
  39.                                                                         , StandardCharsets.UTF_8));
  40.            con.setAutoCommit(false);
  41.            String line;
  42.            String[] values;
  43.            int i = 0;
  44.            while ((line = reader.readLine()) != null) {
  45.                values = line.split(";");    
  46.                ps.setInt(1,Integer.parseInt(values[0].trim()));
  47.                ps.setString(2,values[1].trim());
  48.                ps.setString(3,values[2].trim());
  49.                ps.setString(4,values[3].trim());
  50.                ps.setString(5,values[4].trim());
  51.                ps.setString(6,values[5].trim());
  52.                ps.addBatch();
  53.                i++;
  54.                if(i > 5000) {
  55.                    i = 0;
  56.                    ps.executeBatch();
  57.                    con.commit();
  58.                    ps.clearBatch();
  59.                }
  60.            }
  61.            ps.clearBatch();
  62.            close(con,ps);
  63.            reader.close();
  64.         } catch (ClassNotFoundException e) {
  65.             e.printStackTrace();
  66.         } catch (SQLException e) {
  67.             // TODO Auto-generated catch block
  68.             e.printStackTrace();
  69.         } catch (FileNotFoundException e) {
  70.             // TODO Auto-generated catch block
  71.             e.printStackTrace();
  72.         } catch (IOException e) {
  73.             // TODO Auto-generated catch block
  74.             e.printStackTrace();
  75.         }
  76.        
  77.        
  78.  
  79.     }
  80.  
  81.    
  82.     private static void cleanTable(Connection con, String TableName) throws SQLException {
  83.         PreparedStatement ps = null;
  84.         ps = con.prepareStatement("DELETE FROM  ?;");
  85.         ps.setString(1, TableName);
  86.         ps.executeUpdate();
  87.         close(con,ps);
  88.     }
  89.  
  90.  
  91.     private static void close(Connection con, PreparedStatement ps) {
  92.         try {
  93.                 if (ps != null) {
  94.                     ps.close();
  95.                 }
  96.                 if (con != null) { 
  97.                     con.close();
  98.                 }
  99.             } catch (SQLException e) {
  100.                 // TODO Auto-generated catch block
  101.                 e.printStackTrace();
  102.             }
  103.         }
  104.        
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement