Java 如何创建数据库来存储我使用ApachePOI从excel文件读取的数据?

Java 如何创建数据库来存储我使用ApachePOI从excel文件读取的数据?,java,database,Java,Database,我使用ApachePOI从excel文件中读取数据。现在,我想用控制台中的结果创建数据库。 我的excel文件有6列8行。 有人能帮我吗?几天来,我一直在努力寻找解决方案(对于一个简单的应用程序,您可以使用JDBC动态创建数据库并插入记录,如果这是您想要的。否则,在您从excel文件读取数据时,请先创建数据库并插入行。您需要的解决方案有很多,您可以使用对象模型并将其与ORM工具相结合 然而,我要从这里开始: 这篇文章写得很早,因为他们把它称为雅加达POI,但这些概念可能会流传下来。而且,即使这

我使用ApachePOI从excel文件中读取数据。现在,我想用控制台中的结果创建数据库。 我的excel文件有6列8行。
有人能帮我吗?几天来,我一直在努力寻找解决方案(

对于一个简单的应用程序,您可以使用JDBC动态创建数据库并插入记录,如果这是您想要的。否则,在您从excel文件读取数据时,请先创建数据库并插入行。

您需要的解决方案有很多,您可以使用对象模型并将其与ORM工具相结合

然而,我要从这里开始:

这篇文章写得很早,因为他们把它称为雅加达POI,但这些概念可能会流传下来。而且,即使这是一种基于Oracle的方法,你也可以概括


如果您在此论坛中需要更具体的内容,您必须提供更具体的需求。

假设您以这种方式阅读内容,例如将值保存在列表中(字符串或对象取决于您的目的):

然后,如果列表中保存了数据,则指定数据库引擎,如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";
然后继续创建数据库并插入记录:

 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
我知道代码中有很多改进,但我想给你一些想法和“模板”来实现它。祝你好运

 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