Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package migaenko.ru;
- import java.io.*;
- import java.nio.charset.Charset;
- import java.sql.*;
- /**
- * Created by Егор Мигаенко on 19.10.2017.
- */
- public class Solution {
- public static void main (String[] atrgs){
- //load from file
- File file = new File("Kody_ABC-4kh.txt");
- //connecting to database
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- Connection connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/lab2_2", "root", "524656bnm"
- );
- PreparedStatement statement = connection.prepareStatement("insert into `data` " +
- "(prefix, range_from, range_to, capacity, operator, region) " +
- "value (?,?,?,?,?,?);");
- String[] str;
- String line;
- try {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(
- new FileInputStream(file), Charset.forName("utf8")));
- int count = -1;
- System.out.print("Loading");
- try{
- while ((line = reader.readLine())!=null) {
- if (count++ == -1) continue;
- else {
- str = line.split(";");
- if (str.length > 1) {
- statement.setString(1, str[0].trim());
- statement.setString(2, str[1].trim());
- statement.setString(3, str[2].trim());
- statement.setString(4, str[3].trim());
- statement.setString(5, str[4].trim());
- statement.setString(6, str[5].trim());
- statement.addBatch();
- if (count > 200) {
- statement.executeBatch();
- statement.clearBatch();
- count = 0;
- System.out.print(".");
- }
- }
- }
- }
- statement.executeBatch();
- statement.clearBatch();
- statement.close();
- connection.close();
- }
- catch (IOException e){
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement