Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1. package databaseLab;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.nio.charset.Charset;
  11. import java.sql.*;
  12. import java.util.Properties;
  13.  
  14.  
  15.  
  16. public class ImportInDb {
  17.     public static void main(String[] arg) throws ClassNotFoundException{
  18.         Class.forName("com.mysql.jdbc.Driver");
  19.         try {
  20.             Properties p=new Properties();
  21.             p.setProperty("user","root");
  22.             p.setProperty("password","");
  23.             p.setProperty("useUnicode","true");
  24.             p.setProperty("characterEncoding","cp1251");
  25.  
  26.             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaselab", p);
  27.             PreparedStatement stmt = con.prepareStatement("INSERT INTO databaselab.src (strABCDEF, strFrom, strTo, Amount, Operator, Region) VALUES (?,?,?,?,?,?)");
  28.             delete(stmt, "src");
  29.             File file = new File("Kody_DEF-9kh.txt");
  30.             try {
  31.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader (new FileInputStream(file),Charset.forName("utf8")));
  32.                 String[] str;
  33.                 String line;
  34.                 int count = 0;
  35.                 try {
  36.                     while((line = bufferedReader.readLine())!=null){
  37.                         str = line.split(";");
  38.                         //if(isInt(str[3])){
  39.                         if(str.length>1){
  40.                           stmt.setString(1, str[0].trim());
  41.                           stmt.setString(2, str[1].trim());
  42.                           stmt.setString(3, str[2].trim());
  43.                           stmt.setInt(4, Integer.parseInt(str[3].trim()));
  44.                           stmt.setString(5, str[4].trim());
  45.                           stmt.setString(6, str[5].trim());
  46.                           stmt.addBatch();
  47.                           count++;
  48.                           if(count>200){
  49.                               stmt.executeBatch();
  50.                               stmt.clearBatch();
  51.                               count = 0;
  52.                               System.out.println("Я сделяль");
  53.                           }
  54.                          
  55.                         }
  56.                         stmt.executeBatch();
  57.                         stmt.clearBatch();
  58.                        
  59.                        // }
  60.                        
  61.                        
  62.                     }
  63.                    
  64.                 } catch (IOException e) {
  65.                     // TODO Auto-generated catch block
  66.                     e.printStackTrace();
  67.                 }
  68.             } catch (FileNotFoundException e) {
  69.                 // TODO Auto-generated catch block
  70.                 e.printStackTrace();
  71.             }
  72.             ResultSet res = stmt.executeQuery("SELECT DISTINCT Region from src;");
  73.             stmt = con.prepareStatement("insert into regions (strName) VALUES (?);");
  74.             while(res.next()){
  75.                 stmt.setString(1, res.getString(1));
  76.                 stmt.addBatch();
  77.             }
  78.             stmt.executeBatch();
  79.             stmt.clearBatch();
  80.             res = stmt.executeQuery("SELECT DISTINCT Operator from src;");
  81.             stmt = con.prepareStatement("insert into operators (strName) VALUES (?);");
  82.             while(res.next()){
  83.                 stmt.setString(1, res.getString(1));
  84.                 stmt.addBatch();
  85.             }
  86.             stmt.executeBatch();
  87.             stmt.clearBatch();
  88.             res.close();
  89.             stmt.close();
  90.             con.close();
  91.            
  92.         } catch (SQLException e) {
  93.             // TODO Auto-generated catch block
  94.             e.printStackTrace();
  95.         }
  96.        
  97.        
  98.     }
  99.     public static void delete(Statement stmt, String strName){
  100.         try {
  101.            stmt.executeUpdate("delete from "+strName+";");
  102.            stmt.executeUpdate("ALTER TABLE src AUTO_INCREMENT=0;");
  103.         } catch (SQLException e) {
  104.             // TODO Auto-generated catch block
  105.             e.printStackTrace();
  106.         }
  107.     }
  108.    
  109.     public static Boolean isInt(String str){
  110.         try{
  111.             Integer.parseInt(str);
  112.             return true;
  113.         }
  114.         catch(Exception e){
  115.             return false;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement