gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:35:19 25 4
gpt4 key购买 nike

我在执行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com