2017-06-10 30 views
-2

以下是我的代碼: 在此文件夾中的所有文件逐一讀取。在每個文件中搜索多個關鍵字。如果找到關鍵字,則將其插入到數據庫中。在條件成立後在數據庫中插入多行(每列大約100)

public class MyLogs { 

    String folderPath="path to your folder"; 
    String filePath=""; 
    public void readLog() throws IOException{ 
     try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
     } catch (ClassNotFoundException e1) { 

      e1.printStackTrace(); 
     } 

     Connection connection = null; 
     try { 
      connection = DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:dbName", "username", "password"); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
      return; } 
     FileInputStream inputStream = null; 
     Scanner sc = null; 

     try { 
      File folder = new File("path to your folder"); 

      for (File file : folder.listFiles()) { 
       if(file.isFile()){ 
        filePath=folderPath+"\\"+file.getName(); 
        String fileName=file.getName(); 
        inputStream = new FileInputStream(filePath); 
        sc = new Scanner(inputStream, "UTF-8"); 
        while (sc.hasNextLine()) { 
         String nextLine = sc.nextLine(); 

         if (nextLine.contains("Status")) 
         { 
          String str=nextLine.substring(nextLine.lastIndexOf(" ")+1); 
          String str1=str.split(":")[0].replace("\'", ""); 
           String sql="INSERT INTO table" 
            + "(columnName) VALUES" 
            + "(?)"; 
          PreparedStatement preparedStatement = connection.prepareStatement(sql); 
          preparedStatement.setString(1, str1); 
          preparedStatement .executeUpdate(); 
         } 
         if(nextLine.contains("row")) 
         { 


          String str = nextLine.split(" ")[0]; 
         // System.out.println("\t\t\t\t\t\t\t\t\t"+str); 
          String sql="INSERT INTO table" 
            + "(columnname) VALUES" 
            + "(?)"; 
          PreparedStatement preparedStatement = connection.prepareStatement(sql); 
          preparedStatement.setString(1, str); 
          preparedStatement .executeUpdate(); 
         } 

          } 
       } 
      } 
       if (sc.ioException() != null) { 
        throw sc.ioException(); 
       } 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
      if (sc != null) { 
       sc.close(); 
      } 
     } 
    } 
    public static void main(String[] args) throws IOException { 
     MyLogs ml=new MyLogs(); 
     ml.readLog(); 
    } 
} 
+1

你確切的問題是什麼?沒有明確說明。 –

+0

我建議首先查找所有關鍵字,將它們放入字符串列表中,然後在關鍵字「VALUES」之後構建值String,如下所示:String valueStr = String.join(「,」,Collections.nCopies(keywords。 size(),「(?,?)」));'此後,您可以將關鍵字發佈到PreparedStatement'int pos = 1; for(String keyword:keywords){preparedStatement.setString(pos,keyword); POS ++; }' –

回答

1

你必須在這種情況下使用插入所有語句我已經對你的代碼做了一些改變,請嘗試一下。

String folderPath="path to your folder"; 
      String filePath=""; 
      public void readLog() throws IOException{ 
    try { 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
    } catch (ClassNotFoundException e1) { 

     e1.printStackTrace(); 
    } 

    Connection connection = null; 
    try { 
     connection = DriverManager.getConnection(
       "jdbc:oracle:thin:@localhost:dbName", "username", "password"); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
     return; } 
    FileInputStream inputStream = null; 
    Scanner sc = null; 

    try { 
     File folder = new File("path to your folder"); 

     for (File file : folder.listFiles()) { 
      if(file.isFile()){ 
       filePath=folderPath+"\\"+file.getName(); 
       String fileName=file.getName(); 
       inputStream = new FileInputStream(filePath); 
       sc = new Scanner(inputStream, "UTF-8"); 
       String sql="INSERT ALL "; 
       while (sc.hasNextLine()) { 
        String nextLine = sc.nextLine(); 

        if (nextLine.contains("Status")) 
        { 
         String str=nextLine.substring(nextLine.lastIndexOf(" ")+1); 
         String str1=str.split(":")[0].replace("\'", ""); 
          sql=sql+" INTO table 
           + "(columnName) VALUES" 
           + "(?) ";      
        } 
        if(nextLine.contains("row")) 
        { 
         String str = nextLine.split(" ")[0]; 
        // System.out.println("\t\t\t\t\t\t\t\t\t"+str); 
         sql=sql+" INTO table" 
           + "(columnName) VALUES" 
           + "(?) "; 
        } 

         } 
         sql=sql+" SELECT * FROM dual "; 
         PreparedStatement preparedStatement = 
        connection.prepareStatement(sql); 
         preparedStatement.setString(1, str); 
         preparedStatement .executeUpdate(); 
      } 
     } 
      if (sc.ioException() != null) { 
       throw sc.ioException(); 
      } 

    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     if (inputStream != null) { 
      inputStream.close(); 
     } 
     if (sc != null) { 
      sc.close(); 
     } 
    } 
} 
public static void main(String[] args) throws IOException { 
    MyLogs ml=new MyLogs(); 
    ml.readLog(); 
}