2011-07-18 2 views
0

Dans une feuille Excel, j'ai 2 colonnes (premier est le parent et le second est adapté aux enfants) commeComment trier les chaînes et insérer dans la table sql en JAVA

Parent Child 

BHW  HLF 
BHW  Instr 
BHW  Interior 
BHW  Exterior 
BR  BRF 
BR  CR 
BR  Panel 
BR  Frame 
BR  Paint 
BR  Plastic 

Les colonnes parent et les colonnes enfants seront insérées dans une table sql. J'insère d'abord toutes les colonnes parentes (seul parent par ensemble) Lorsque la colonne enfant est insérée, la colonne Parent_id de la table aura la valeur parente. Je ne parviens pas à trier les colonnes parentes à l'aide de l'option Définir.

  1. Il renvoie d'abord BR puis BHW.
  2. Si je ne supprime pas les doublons et ne l'insère pas, comment puis-je faire correspondre l'enfant avec le parent et insérer dans la table.

Edit: Mon code:

import java.io.*; 
import java.text.*; 
import java.sql.*; 
import java.util.*; 


import org.apache.poi.ss.usermodel.*; 

public class ProdTable { 

    String strBasePath = "D:\\Project\\Temp.xlsx"; 


    ArrayList<String> arrListLevel_1_SCT  = new ArrayList<String>(); 
    ArrayList<String> arrListLevel_2_SC  = new ArrayList<String>(); 
    Set<String>  setUnqProducts   = new HashSet<String>(); 
    ArrayList<String> arrListLevel_1_Unique = new ArrayList<String>(); 
    ArrayList<String> strInsertQuery   = new ArrayList<String>(); 

    int iRowCount = 0; 
    String strSheetName = "Sheet1"; 
    String strDBName = "ProductsDB"; 
    int intID = 0; 
    int intRG_ID = 0; 
    int intWG_ID = 0; 
    int intRowIncrement = 1; 

    int intInstQuery; 

    public ProdTable() 
    { 

    } 
    public void ProdInsert() 
    { 
     try{ 

     // Fetched out the data in excel to the arrListLevel_1_SCT and arrListLevel_2_SC 

     System.out.println("arrListLevel_1_SCT = " + arrListLevel_1_SCT.size()); 
     System.out.println("arrListLevel_2_SC = " + arrListLevel_2_SC.size()); 

     // Finding duplicates - Exclusive list of the distinct Products 
     setUnqProducts = findDuplicates(arrListLevel_1_SCT); 
     arrListLevel_1_Unique.addAll(setUnqProducts); 
     System.out.println("arrListLevel_1_Unique = " + arrListLevel_1_Unique.size()); 

     // Connection for SQL Server. 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
     // DFFST 
     String url = "jdbc:sqlserver://DFFST:1433;DatabaseName=" + strDBName + ";" + 
       "User=sa;Password=sa;"; 

     Connection conn = DriverManager.getConnection(url); 
     ResultSet generatedKeys = null; 
     PreparedStatement preparedStatement = null; 

     if (conn != null) {     
      System.out.println("Connection Successful!");    
     } 

     //Create a Statement object 
     Statement sql_stmt = conn.createStatement(); 

     //Create a Statement object 
     Statement sql_stmt_1 = conn.createStatement(); 

     //Result Set for Prouduct Table 
     ResultSet rs = sql_stmt.executeQuery("SELECT MAX(ID), MAX(RG_ID), MAX(WG_ID) FROM " + strDBName + ".[dbo].Product"); 

     if (rs.next()) {  
      // Retrieve the auto generated key(s).  
      intID = rs.getInt(1); 
      intRG_ID = rs.getInt(2); 
      intWG_ID = rs.getInt(3); 
     } 

     for (int iCount = 0 ;iCount < arrListLevel_1_Unique.size(); iCount++) 
     { 

     //Result Set for Prouduct Table 


     sql_stmt_1.executeUpdate("\n IF NOT EXISTS(SELECT 1 FROM " + strDBName + ".[dbo].Product WHERE [Name] NOT LIKE '" + arrListLevel_1_Unique.get(iCount) + "') " 
       + "\nINSERT INTO " + strDBName + ".[dbo].Product ([Name] ," 
       + "[RG_ID],[WG_ID],[Parent_Product]) " 
       + "VALUES ('" + arrListLevel_1_Unique.get(iCount) + "'," 
       + + (intWG_ID + intRowIncrement) + ", " + (intWG_ID + intRowIncrement + 1) + ", 5828)"); 


     intRowIncrement++ ; 
     } 

    rs.close(); 
     sql_stmt.close(); 
     sql_stmt_1.close(); 


     //Close the database connection 
     conn.close(); 
     } 
     catch(Exception E) 
     { 
      E.printStackTrace(); 
     } 

    } 

public static Set findDuplicates(ArrayList<String> inpArrayList){ 
Set<String> set = new HashSet<String>(); 

for(int i=0; i < inpArrayList.size(); i++){ 

    if(!set.contains(inpArrayList.get(i))) 
    { 
     set.add(inpArrayList.get(i)); 
    } 
} 
return set; 

}

}

S'il vous plaît aidez-moi. Merci Ramm

+0

S'il vous plaît montrer votre code –

+0

Pour 1.) voulez-vous dire 'SortedSet'? Pour une implémentation de 'SortedSet', vous pouvez utiliser' TreeSet'. –

+0

J'ai modifié ma question et ajouté mon code. S'il vous plaît aider. Merci – Ramm

Répondre

0

Utilisez un TreeSet et implémentez un Comparator personnalisé avec la commande dont vous avez besoin.

+0

J'ai essayé plusieurs façons, mais elle renvoyait toujours les éléments Parent pas dans l'ordre que j'ai donné. Comme si l'ordre est AA, AA, BB, BB, CC, DD, il donnera AA, BB, CC, DD. Il donne dans l'ordre embrouillé. – Ramm

Questions connexes