How to sort the strings and insert into sql table in JAVA
In an excel sheet, i have 2 columns (first is the parent and second is child) like
Parent Child
BHW HLF
BHW Instr
BHW Interior
BHW Exterior
BR BRF
BR CR
BR Panel
BR Frame
BR Paint
BR Plastic
Both the parent columns and child columns will get inserted into a sql table. I am inserting all the parent columns first (only parent per set) When the child column gets inserted, the Parent_id column in the table will have the parent value of it.
- I am unable to sort the parent columns using Set . It returns first BR and then BHW.
- if I dont remove duplicates and insert it, how can I match child wiht the parent and insert into the table.
Edit : My 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> 开发者_StackOverflow社区 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;
}
}
Please help me. Thanks Ramm
Use a TreeSet
and implement a custom Comparator
with the ordering that you need.
精彩评论