我是Java的新手,所以请耐心等待......我尝试使用Jackcess库将数据从CSV文件写入Access数据库。 CSV文件的第一行完美地写入数据库,但从第二行开始,它不断抛出NumberFormatException。这是代码:
public void GPXtoAccess() {
try {
Access = new Scanner(DummyCSV);
Access.useDelimiter(";");
while(Access.hasNextLine()) {
GPXlat = Access.next();
GPXlon = Access.next();
GPXtime = Access.next();
GPXname = Access.next();
GPXdesc = Access.next();
try {
GPXTable.addRow(Column.AUTO_NUMBER, GPXlat, GPXlon, GPXtime, GPXname, GPXdesc);
} catch (IOException E) {
System.out.println("Error: " + E);
System.out.println("Error is thrown while writing data to table");
}
}
} catch (FileNotFoundException ex) {
System.out.println("Error: " + ex);
}
}
它不断抛出NumberFormatException:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2016-11-28T11:36:34"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.lang.Double.valueOf(Double.java:502)
at com.healthmarketscience.jackcess.impl.ColumnImpl.toNumber(ColumnImpl.java:1443)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeFixedLengthField(ColumnImpl.java:1105)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeFixedLengthField(ColumnImpl.java:1072)
at com.healthmarketscience.jackcess.impl.ColumnImpl.writeRealData(ColumnImpl.java:1029)
at com.healthmarketscience.jackcess.impl.ColumnImpl.write(ColumnImpl.java:1021)
at com.healthmarketscience.jackcess.impl.ColumnImpl.write(ColumnImpl.java:1003)
at com.healthmarketscience.jackcess.impl.TableImpl.createRow(TableImpl.java:2708)
at com.healthmarketscience.jackcess.impl.TableImpl.createRow(TableImpl.java:2660)
at com.healthmarketscience.jackcess.impl.TableImpl.addRows(TableImpl.java:2193)
at com.healthmarketscience.jackcess.impl.TableImpl.addRow(TableImpl.java:2086)
at GPX.GPX_Parser.GPXtoAccess(GPX_Parser.java:196)
at GPX.GUI.GPX_Handle.btnExportDatabaseActionPerformed(GPX_Handle.java:234)
at GPX.GUI.GPX_Handle.access$300(GPX_Handle.java:21)
at GPX.GUI.GPX_Handle$4.actionPerformed(GPX_Handle.java:111)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
答案 0 :(得分:0)
我相信这里发生的事情是你的GPXTime不符合Access数据库标准,因此它不知道如何处理它。您可能希望查看api并查看所需的格式,然后从csv文件进行转换,该文件的格式为“2016-11-28T11:36:34'到Access数据库接受的任何格式。在调用addRow()
之前将日期转换为毫秒来尝试答案 1 :(得分:0)
不用担心,我们都是从知识有限开始的。
首先,我会就您的代码提供一些反馈:
1)你似乎有很多成员变量,甚至可能是静态变量。例如变量Access。如果您只在与我们分享的范围内使用它,即在该方法中,则没有理由这样做。
2)java命名约定:我强烈建议您遵循java命名约定,如下所述:http://www.oracle.com/technetwork/java/codeconventions-135099.html遵循这些命名约定使代码对其他人更具可读性,例如我们这里
3)在获取next()之前,你应该使用hasNext()来确保实际上有一个可用的令牌。我建议您尝试通过执行此操作来调试并修复您的问题:
while (Access.hasNext()) {
System.out.println(Access.next());
}
看看情况如何。