如何在JAVA中对字符串进行排序并插入sql表
在excel工作表中,我有两列(第一列是父列,第二列是子列),如如何在JAVA中对字符串进行排序并插入sql表,java,Java,在excel工作表中,我有两列(第一列是父列,第二列是子列),如 父列和子列都将插入到sql表中。我首先插入所有父列(每组仅插入父列),当插入子列时,表中的parent_id列将具有它的父值 我无法使用Set对父列进行排序。它首先返回BR,然后返回BHW 如果我不删除重复项并插入它,我如何将子项与父项匹配并插入到表中 编辑:我的代码: import java.io.*; import java.text.*; import java.sql.*; import java.util.*; i
父列和子列都将插入到sql表中。我首先插入所有父列(每组仅插入父列),当插入子列时,表中的parent_id列将具有它的父值
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;
import java.io.*;
导入java.text.*;
导入java.sql.*;
导入java.util.*;
导入org.apache.poi.ss.usermodel.*;
公共类可生产表{
字符串strBSepath=“D:\\Project\\Temp.xlsx”;
ArrayList arrListLevel_1_SCT=new ArrayList();
ArrayList arrListLevel_2_SC=新建ArrayList();
Set setUnqProducts=new HashSet();
ArrayList arrListLevel_1_Unique=新建ArrayList();
ArrayList strInsertQuery=新建ArrayList();
int iRowCount=0;
字符串strSheetName=“Sheet1”;
字符串strDBName=“ProductsDB”;
int intID=0;
int intRG_ID=0;
int intWG_ID=0;
int intRowIncrement=1;
int-intInstQuery;
公共ProdTable()
{
}
公共空间插入()
{
试一试{
//将excel中的数据提取到arrListLevel_1_SCT和arrListLevel_2_SC
System.out.println(“arrListLevel_1_SCT=“+arrListLevel_1_SCT.size());
System.out.println(“arrListLevel_2_SC=“+arrListLevel_2_SC.size());
//查找重复项-独特产品的排他性列表
setUnqProducts=findDuplicates(arrListLevel_1_SCT);
arrListLevel_1_Unique.addAll(setUnqProducts);
System.out.println(“arrListLevel_1_Unique=“+arrListLevel_1_Unique.size());
//SQL Server的连接。
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”).newInstance();
//DFFST
String url=“jdbc:sqlserver://DFFST:1433;DatabaseName=“+strDBName+”+
“用户=sa;密码=sa;”;
连接conn=DriverManager.getConnection(url);
ResultSet generatedKeys=null;
PreparedStatement PreparedStatement=null;
如果(conn!=null){
System.out.println(“连接成功!”);
}
//创建一个语句对象
语句sql_stmt=conn.createStatement();
//创建一个语句对象
语句sql_stmt_1=conn.createStatement();
//PRODUCT表格的结果集
结果集rs=sql_stmt.executeQuery(“从“+strDBName+”[dbo].Product”中选择MAX(ID)、MAX(RG_ID)、MAX(WG_ID));
如果(rs.next()){
//检索自动生成的密钥。
intID=rs.getInt(1);
intRG_ID=rs.getInt(2);
intWG_ID=rs.getInt(3);
}
对于(int iCount=0;iCount
}
}
请帮帮我。
谢谢
Ramm使用并实现定制的
比较器,并按您需要的顺序进行排序。用于1。)您的意思是分拣数据集
?对于SortedSet
的实现,您可以使用TreeSet
。我编辑了我的问题并添加了代码。请帮忙。ThanksI尝试了多种方法,但它总是返回父元素,而不是按照我给出的顺序。就像订单是AA,AA,BB,BB,CC,DD一样,它应该给出AA,BB,CC,DD。它以杂乱的顺序发出。
BHW HLF
BHW Instr
BHW Interior
BHW Exterior
BR BRF
BR CR
BR Panel
BR Frame
BR Paint
BR Plastic
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;