简体   繁体   English

如何创建数据库以存储我用Apache POI从excel文件读取的数据?

[英]How to create database to store data that i read with Apache POI from excel files?

i read the data from excel files using APACHE POI. 我使用APACHE POI从excel文件中读取数据。 Now, i would like to create database with the result that i have in console. 现在,我想用控制台中的结果创建数据库。 My excel files have 6 columns and 8 rows. 我的Excel文件有6列8行。 Can anyone help me? 谁能帮我? I am trying to find a solution for a couple of days.. :( 我想找几天的解决方案.. :(

For a simple application, you can use JDBC to create database on the fly and insert records, if this is what you want. 对于简单的应用程序,如果需要的话,可以使用JDBC即时创建数据库并插入记录。 Otherwise, create database before hand and insert rows as and when you read data from excel files. 否则,请先创建数据库,然后在从excel文件读取数据时插入行。

What you are asking for has many solutions, you can use an object model and tie that in with an ORM tool. 您要的东西有很多解决方案,您可以使用对象模型并将其与ORM工具配合使用。

I'd start here however: 我将从这里开始:

Generate External Tables from an Excel Spreadsheet Using Apache Jakarta POI 使用Apache Jakarta POI从Excel电子表格生成外部表

The article is dated as they reference it as Jakarta POI but the concepts likely carry over. 文章过时,因为他们将其称为Jakarta POI,但这些概念可能会延续下去。 Also, even though it is an Oracle based approach you can generalize. 此外,即使它是基于Oracle的方法,您也可以进行概括。

If you need something more specific in this forum you would have to provide something more concrete in terms of needs. 如果您在此论坛中需要更具体的内容,则必须根据需求提供更具体的内容。

Assuming you're reading your content this way, like saving the values in an List (String or Objects depending on your purposes): 假设您正在以这种方式读取内容,例如将值保存在列表中(取决于您的目的是字符串还是对象):

//Some code of yours here
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}

sheetData.add(data);

Then if you have the data saved in your list, then, specify your database engine, like MySQL: 然后,如果您将数据保存在列表中,则指定数据库引擎,例如MySQL:

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://localhost:3306/";
static final String USER = "username";
static final String PASS = "password";

Then you proceed to create the database and insert your records: 然后,您继续创建数据库并插入记录:

 Connection conn = null;
 Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");


 conn = DriverManager.getConnection(DB_URL, USER, PASS);

     System.out.println("About to create a database");
      stmt = conn.createStatement();
      String dbName = "MyExcelDB";
      String sql = "CREATE DATABASE " + dbName;
      stmt.executeUpdate(sql);
      System.out.println("Database created successfully...");
      //Now, create a table based on what you need, maybe you can get the headers of your excel
String tableName = "TBUSER";
String createTableSQL = "CREATE TABLE" + tableName + "("
                + "USER_ID NUMBER(5) NOT NULL, "
                + "USERNAME VARCHAR(20) NOT NULL, "
                + "CREATED_BY VARCHAR(20) NOT NULL, "
                + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
                + ")";
Statement createTableStatement = conn.createStatement();
createTableStatement.execute(createTableSQL);
//Now insert
String insertQuery = "insert into " + dbName + "." + tableName + "(user_id,username,created_by,created_date) values (?,?,?,?)";
PreparedStatement insertStatement = conn.prepareStatement(insertQuery);
insertStatement.setInt(1,Integer.parseInt(sheetData.get(0));
insertStatement.setString(2,sheetData.get(1));
//and so on... then
insertStatement.executeUpdate();//This returns an int, if the insert works, the value must be greater than 0

I know there are a lot of improvements in the code, but I want to give you the idea and the "template" to do it. 我知道代码中有很多改进,但是我想为您提供实现的想法和“模板”。 Best regards. 最好的祝福。

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

相关问题 如何将唯一数据从 excel 存储到数据库(使用 apache poi)? - How to store unique data from excel to database(using apache poi)? 如何使用Apache POI从excel工作表的单元格中读取外来字符,并使用SQLITE将它们存储在数据库中? - How to read foreign characters from a cell in excel sheet using Apache POI and store them in database using SQLITE? 如何使用apache poi从excel读取数据并将数据存储到String [] []数组中? - How to read data from excel using apache poi and store the data into String[][] array? 使用Java Apache POI将数据库数据存储到Excel工作表中 - Store database data into the excel sheet using java Apache POI 使用Apache POI从java中的excel表中读取数据 - Read data from excel sheet in java with Apache POI Excel 读取多个文件 Java Apache POI - Excel Read Multiple Files Java Apache POI 如何使用Apache POI读取和搜索Excel数据 - how to read & search excel data using Apache POI 如何使用Apache POI读取excel文件中特定行的数据? - How to read data in specific row in an excel file using Apache POI? 如何忽略列数中的最后一列? 我正在使用Apache POI读取Excel数据 - How to ignore last column in column count? I am using Apache POI to read excel data 如何使用Java中的Apache POI从excel中读取/写入XML映射? - How to read/write XML maps from/in excel with Apache POI in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM