我有点新的Java,所以请忍受...我正在尝试使用JackCess库将数据从CSV文件写入访问数据库。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)
我相信这里发生的事情是您的gpxtime不符合访问数据库标准,因此它不知道如何处理。您可能需要查看API并查看所需的格式,然后从具有'2016-11-28T11:36:34'的CSV文件中进行转换,以对任何格式访问数据库接受。通过在调用Addrow()
不用担心,我们都从有限的知识开始。
我首先给出您的代码的一些反馈:
1)您似乎有很多成员变量,甚至可能是静态的。例如,可变访问。没有理由要做,如果您仅在与我们共享的范围中使用它,即在该方法中。
2)Java命名大会:我强烈建议您关注Java命名约定,如下所述:http://www.oracle.com/technetwork/java/java/codeconventions-conventions-1350999.html这些命名惯例使代码更加易读对于其他人,例如我们,这里
3)在获得下一个()之前,您应该使用hasnext()放心,实际上有一个令牌。我建议您尝试通过这样做来调试和解决您的问题:
while (Access.hasNext()) {
System.out.println(Access.next());
}
查看如何进行。