java - 在 Java Swing 中将 Excel 数据插入数据库时​​出现 ExceptionInInitializerError

标签 java mysql excel swing

我在执行 java swing 代码时遇到此错误。 怎么解决这个问题呢?我发现了一些与此类似的问题,但没有得到所需的答案。

我正在制作一个桌面应用程序,它将从 Excel 工作表中读取表格并更新数据库中的表格值。

这是代码片段: 我正在读取和调用数据库查询的主要代码

if (flag) {

    int j=0;
    String[] productArray= new String[2];


    for (int i = 0; i < cr.getPhysicalNumberOfCells(); i++) { 


        String colKeyOrTabName = getCellValueAsString((cr
                .getCell(firstCell + i)));
        colKeyOrTabName=colKeyOrTabName.replaceAll(" ", "");
        //colKeyOrTabName=colKeyOrTabName.replaceAll("[^a-zA-Z0-9-]", "");
        productArray[j]=colKeyOrTabName;
        j++;
        //System.out.println(" "+ colKeyOrTabName);


    }

    if(!productArray[0].equalsIgnoreCase("code")){
    DBConfig.insertCodes(productArray[0], productArray[1]); 
    }
    /*Ends Here*/
    rowNo++;
    continue;
}

数据库代码:

 public class DBConfig {


private static BasicDataSource bds = null;

static{
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    //  logger.error("Error - " + String.valueOf(e), e);

        throw new RuntimeException(
                "Error setting connection with SyntBots database");
    }

    bds = new BasicDataSource();

    // set driver class name
    bds.setDriverClassName("com.mysql.jdbc.Driver");

    // Define Server URL
    bds.setUrl(Config.get("config.db.url"));


    // Define Username
     bds.setUsername(Config.get("config.db.user"));


    // Define Your Password
    bds.setPassword(Config.get("config.db.password"));

}



public static void insertCodes(String code, String value) {
    // TODO Auto-generated method stub
    Connection con = null;
    Statement stmt = null;

    try {

        // Connection conn = null;
        con = bds.getConnection();

        stmt = con.createStatement();

        String sql = "insert into table(code,value) value('" + code+ "','"+ value+"')";
        try{
        stmt.executeUpdate(sql);
        }
        catch(SQLException e){
            if(e.getErrorCode() == MYSQL_DUPLICATE_PK ){
                System.out.println("Duplicate Entry");             }
        }

        // con.close();
    } catch (Exception e) {
        //logger.error("Ignore Error - " + String.valueOf(e), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }

        if (null != con) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }

}



}

这是错误(控制台输出):

    Button clicked
    D:\DesktopApplicationInputSheet
    Sample.xlsx
    D:\DesktopApplicationInputSheet/Sample.xlsx
    Reading sheet: 0, Name: Sheet1
    i: 1
    0
    Display Name :-Polaris Code rowNO - 1
    Display Name :-AOO1 rowNO - 2
    Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at 
    com.dataentry.excel.MainDataEntry.readRequestTable(MainDataEntry.java:249)
        at 

  com.dataentry.excel.MainDataEntry.readExcelandWriteonDB(MainDataEntry.java:149)
        at 
    com.dataentry.excel.MainDataEntry.readExcelPath(MainDataEntry.java:79)
        at 
    com.dataentry.excel.MainDataEntry$1.actionPerformed(MainDataEntry.java:57)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at 

  java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at 

  java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at 
 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.lang.RuntimeException: Error setting connection with SyntBots database
        at com.dataentry.excel.DBConfig.<clinit>(DBConfig.java:26)
        ... 40 more

更新: 问题已修复。这是 MySQL 驱动程序缺少 JAR 的情况。 但在那之后我面临着一个新问题。 看一下控制台输出:

 Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.apache.commons.pool2.impl.GenericObjectPool.setTestOnCreate(Z)V
    at org.apache.commons.dbcp2.BasicDataSource.createConnectionPool(BasicDataSource.java:2074)
    at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1920)
    at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
    at com.dataentry.excel.DBConfig.insertCodes(DBConfig.java:59)
    at com.dataentry.excel.MainDataEntry.readRequestTable(MainDataEntry.java:249)
    at com.dataentry.excel.MainDataEntry.readExcelandWriteonDB(MainDataEntry.java:149)
    at com.dataentry.excel.MainDataEntry.readExcelPath(MainDataEntry.java:79)
    at com.dataentry.excel.MainDataEntry$1.actionPerformed(MainDataEntry.java:57)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

好吧,我已经找到了问题的答案。 就我而言,它与 commons-pool2-2 jar 文件的版本有关。 我使用的是 2.0,而不是 2.4.2。 将 JAR 更改为 2.4.2 后,它开始按预期工作。

我还使用了mysql-connector-java-5.1.18来解决我之前的问题。

注意:我从互联网上发现,修复程序也需要 JAR 兼容性。

关于java - 在 Java Swing 中将 Excel 数据插入数据库时​​出现 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355236/

相关文章:

java - JMagick - 如何改变亮度和对比度?

java - 平等\不平等的字符

java - 在 Java 中分割文本

PHP 查询数据库内的 Session_ID

java - 如何仅使用数组列表的一部分填充 ListView

java - 在 JAXB 中将 JSON 对象转换为字符串

mysql - 为什么 MySQL 查询不能正确插入 BIT(50) 位域

c# - 如何在 Excel 共享插件中使用对话框

c# - 如何从 C# 应用程序中正确清理 Excel 互操作对象?

java - 性能问题: convert csv to excel using Powershell