简体   繁体   中英

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

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]

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

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:

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)

There is multiple tutorials and guides of how to setup a MySQL database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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