简体   繁体   中英

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. Now, i would like to create database with the result that i have in console. My excel files have 6 columns and 8 rows. 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. Otherwise, create database before hand and insert rows as and when you read data from excel files.

What you are asking for has many solutions, you can use an object model and tie that in with an ORM tool.

I'd start here however:

Generate External Tables from an Excel Spreadsheet Using Apache Jakarta POI

The article is dated as they reference it as Jakarta POI but the concepts likely carry over. Also, even though it is an Oracle based approach you can generalize.

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:

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.

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