简体   繁体   English

如何将arraylist数据存储到数据库中

[英]How to store arraylist data into database

Hello Guys I want to insert the data stored in the Array list into Database I have tried like this I have read from an excel file and stored that into an array list so now i want to insert that data into database now i am not getting how to store that into database 大家好,我想将存储在数组列表中的数据插入数据库中,我已经尝试从Excel文件中读取并存储到数组列表中,所以现在我想将数据插入数据库中,但现在还不知道如何将其存储到数据库中

public class ReadExcelFile {

int ColumnCount = 0;

public void readExcel(String filePath, String fileName, String sheetName) throws IOException {

    File file = new File(filePath + "\\" + fileName);

    FileInputStream inputStream = new FileInputStream(file);

    Workbook Workbook = null;

    String fileExtensionName = fileName.substring(fileName.indexOf("."));

    if (fileExtensionName.equals(".xlsx")) {

        Workbook = new HSSFWorkbook(inputStream);

    } else if (fileExtensionName.equals(".xls")) {

        Workbook = new HSSFWorkbook(inputStream);

    }

    Sheet MSheet = Workbook.getSheet(sheetName);

    int rowCount = MSheet.getLastRowNum() - MSheet.getFirstRowNum();
    ArrayList<String> arra = new ArrayList<String>();
    for (int i = 0; i < rowCount + 1; i++) {
        Row row = MSheet.getRow(i);
        if (i == 0) {
            ColumnCount = row.getLastCellNum();

        }

        for (int j = 0; j < row.getLastCellNum(); j++) {

            // System.out.print(row.getCell(j).getStringCellValue() + "|| ");
            //ArrayList<String> getdata=new ArrayList<String>();
            arra.add(row.getCell(j).getStringCellValue());
            //System.out.println(arra);
            ArrayList<String> keyword = arra;
            keyword.contains(row.getCell(j).getStringCellValue());

            // System.out.println(row.getCell(j).getStringCellValue());
            // System.out.println(arra);
            //Dictionary getdata = new Hashtable();
            //getdata.put(row.getCell(j).getStringCellValue()," ");
            //System.out.println(getdata);
        }

        System.out.println(arra);

    }

}

public static void main(String args[]) throws IOException {

    ReadExcelFile objExcelFile = new ReadExcelFile();

    String filePath = System.getProperty("user.dir");

    objExcelFile.readExcel(filePath, "RuleBook.xls", "Data");

}

} }

Output I got is :[Client Name, Location Name, Service Point Location, Location City, State/Province, Location Country, Account Number, Alternate Account Number, Meter Number, Invoice ID, Invoice Month, Invoice Date, Service Begin Date, Service End Date, Service Days, Vendor Name, Vendor Service Name, Unique ID, Data Category Type, Data Category, Sub-Category/Sub-bucket, Value Name, Value, Unit of Measure, Total Invoice Amount, Due Date, Invoice Receipt Date, Adjustment Flag, Estimated vs Actual Read, Image Details, Secondary Vendor, Nom du client, Lieu de consommation ?, TBD, TBD, TBD, TBD, compte de facturation, compte commecial, identifiant de comptage, detail de votre facture du 14/09/2017 n, To be extracted?, detail de votre facture du, index de debut, index de fin, To be calculated?, TBD?, TBD?, TBD?, TBD?, TBD, TBD, TBD, TBD, TBD, FactureTTC, a regler avant le, TBD, TBD, Relve Estime - which mon th?, TBD, TBD] 我得到的输出是:[客户名称,位置名称,服务点位置,位置城市,州/省,位置国家/地区,帐号,备用帐号,仪表编号,发票ID,发票月份,发票日期,服务开始日期,服务结束日期,服务天数,供应商名称,供应商服务名称,唯一ID,数据类别类型,数据类别,子类别/子桶,值名称,值,计量单位,总发票金额,到期日,发票收据日期,调整标志,估计值与实际读取值,图像详细信息,第二个供应商,客户名称,无偿购买?,待定,待定,待定,待定,补偿,商业,身份识别,投票细节,14 / 09/2017 n,待提取?,细节,首次亮相,索引,要计算?,TBD ?, TBD ?, TBD ?, TBD ?, TBD,TBD,TBD,TBD,TBD ,FactureTTC,先行者,TBD,TBD,Relve Estime-哪个月份?,TBD,TBD]

So I want to store Client Name till Secondary vendor as Data base column name and From Nom du client i want to store under Client Name column so Please help me 因此,我想将“客户端名称”存储到“二级供应商”作为“数据库”列名称,并希望从“ Nom du客户端”存储在“客户端名称”列下,因此请帮助我

I'm not quite sure what the question is, but storing an arraylist in a database could be done with a JDBC driver and a MySQL database like this: 我不太清楚问题是什么,但是可以使用JDBC驱动程序和MySQL数据库来将arraylist存储在数据库中,如下所示:

var host = "localhost";
var databaseName = "databasename";
var port = "3306";

var url = "jdbc:mysql://" + host + ":" + port + "/" + databaseName;

try(Connection con = DriverManager.getConnection(url, "username", "password")) {

    String SQL = "INSERT INTO tablename(client_name) VALUES(?)";
    PreparedStatement stmt = con.prepareStatement(SQL);
    for(String clientName : listOfClientNames) {
        stmt.setString(1, clientName);
        stmt.addBatch();
    }
    stmt.executeBatch();

} catch (SQLException e) {
    System.out.println("Could not connect");
}

You will need to store your ClientName in a list (I called it listOfClientNames) 您需要将ClientName存储在一个列表中(我称之为listOfClientNames)

There is multiple tutorials and guides of how to setup a MySQL database. 关于如何设置MySQL数据库,有许多教程和指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM