Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rusanov.phone.book;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.util.Properties;
- import java.sql.*;
- public class PhoneBook {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String dbURL = "jdbc:mysql://localhost:3306/phones";
- String dbUsername = "root";
- String dbPassword = "root";
- java.util.Properties connProperties = new java.util.Properties();
- connProperties.put("user", dbUsername);
- connProperties.put("password", dbPassword);
- connProperties.put("useUnicode","true");
- connProperties.setProperty("characterEncoding","cp1251");
- Connection con = DriverManager.getConnection(dbURL,connProperties);
- PreparedStatement ps = con.prepareStatement("INSERT INTO T_SRC VALUES (?,?,?,?,?,?)");
- //truncateTable(con,"t_numbers");
- //truncateTable(con,"t_operator");
- //truncateTable(con,"t_region");
- String tableName = "T_SRC";
- cleanTable(con,tableName);
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Nik\\eclipse-workspace\\PhonesBook\\Kody_ABC-8kh.txt")
- , StandardCharsets.UTF_8));
- con.setAutoCommit(false);
- String line;
- String[] values;
- int i = 0;
- while ((line = reader.readLine()) != null) {
- values = line.split(";");
- ps.setInt(1,Integer.parseInt(values[0].trim()));
- ps.setString(2,values[1].trim());
- ps.setString(3,values[2].trim());
- ps.setString(4,values[3].trim());
- ps.setString(5,values[4].trim());
- ps.setString(6,values[5].trim());
- ps.addBatch();
- i++;
- if(i > 5000) {
- i = 0;
- ps.executeBatch();
- con.commit();
- ps.clearBatch();
- }
- }
- ps.clearBatch();
- close(con,ps);
- reader.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private static void cleanTable(Connection con, String TableName) throws SQLException {
- PreparedStatement ps = null;
- ps = con.prepareStatement("DELETE FROM ?;");
- ps.setString(1, TableName);
- ps.executeUpdate();
- close(con,ps);
- }
- private static void close(Connection con, PreparedStatement ps) {
- try {
- if (ps != null) {
- ps.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement