【问题标题】:jdbc-hsqldb exception for text table文本表的 jdbc-hsqldb 异常
【发布时间】:2013-02-15 15:35:30
【问题描述】:

我的java代码是:

conn = DriverManager.getConnection(url, "user", "password"); // line 1

stm = conn.createStatement(); // line 2

stm.execute("CREATE TEXT TABLE someTableName("NLID" VARCHAR(20),
 "Scheduled.Primary.Scripting.Code" VARCHAR(20), "Scheduled.Site" VARCHAR(20),
 "Scheduled.Location.Long.Name" VARCHAR(20),
 "primary_key_1644" int PRIMARY KEY)"); // line 3

stm.execute("SET TABLE someTableName SOURCE 
"/some.csv;ignore_first=true;all_quoted=true;shutdown=true""); // line 4

链接到 hsqldb 的 CSV 是:

NLID,Scheduled.Primary.Scripting.Code,Scheduled.Site,Scheduled.Location.Long.Name,primary_key_1644   
100,INMRSB,Shopping,Shopping General-Banner-728x90-INMRSB-I,1 
100002,MSVT08,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 8-Streaming Media-300x60-MSVT08-S,2  
100004,MSV10T,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 10-Streaming Media-300x60-MSV10T-S,3 

我在第 4 行得到以下异常:

java.sql.SQLException: bad TEXT table source file - line number: 1196 java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" in statement [SET TABLE someTableName SOURCE "/some.csv;ignore_first=true;all_quoted=true;shutdown=true"] at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.Util.sqlException(Unknown Source) at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source) at java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" at org.hsqldb.error.Error.error(Unknown Source) at org.hsqldb.TextTable.connect(Unknown Source) at org.hsqldb.TextTable.openCache(Unknown Source) at org.hsqldb.TextTable.setDataSource(Unknown Source) at org.hsqldb.StatementCommand.getResult(Unknown Source) at org.hsqldb.StatementCommand.execute(Unknown Source) at org.hsqldb.Session.executeCompiledStatement(Unknown Source) at org.hsqldb.Session.executeDirectStatement(Unknown Source) at org.hsqldb.Session.execute(Unknown Source) ... 6 more Caused by: java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at org.hsqldb.rowio.RowInputText.readInteger(Unknown Source) at org.hsqldb.rowio.RowInputBase.readData(Unknown Source) at org.hsqldb.rowio.RowInputText.readData(Unknown Source) at org.hsqldb.rowio.RowInputBase.readData(Unknown Source) at org.hsqldb.rowio.RowInputText.readData(Unknown Source) at org.hsqldb.RowAVLDiskData.getRowData(Unknown Source) at org.hsqldb.persist.RowStoreAVLDiskData.get(Unknown Source) ... 14 more

任何帮助都将不胜感激。

已编辑: Hsqldb 在读取定义为 VARCHAR 的列时抛出 NumberFormatException !!!

MODIFIED : 发生异常是因为其中一个列值中有一个逗号,例如 "Mind, Body Connection-Banner-728x90-HEAMBA-S" 。由于 hsqldb 将 csv 作为表读取,因此附加逗号被 hsql 解释为附加列。谁能指导我如何规避这个问题?

【问题讨论】:

  • 很明显“Body Connection-Banner-728x90-HEAMBA-S”不是数字,所以报错。
  • 列定义为 VARCHAR(20) 。
  • 列可能定义为 varchar,但在您的代码中某处您正试图将其转换为数字,但您没有向我们展示该代码:)
  • 您确定向我们展示了正确的 CSV 文件吗?字符串 Body Connection-Banner-728x90-HEAMBA-S 未显示在 CSV 中,但 hsqldb 正在抱怨它。
  • stm.execute("SET TABLE someTableName SOURCE "/some.csv;ignore_first=true;all_quoted=true;shutdown=true"");抛出异常。请仔细跟踪堆栈跟踪,异常是从 jar 中抛出的,而不是我的代码。

标签: java jdbc hsqldb


【解决方案1】:

正如 OP 所发现的,遵循这些简单的步骤有助于在 CSV 文件中找到问题:

  1. 查看错误信息:bad TEXT table source file - line number: 1196 行号是CSV文件的行号。使用文本编辑器,转到该行。行从 1 开始编号。
  2. ava.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" note NumberFormatException 表示需要一个数字,而是找到了报告的字符串。
  3. 幸运的是,表中只有一个数字 (INT) 列,因此很容易看到 CVS 中有一个额外的逗号,它终止了最后一个 VARCHAR 字符串并导致问题。
  4. TEXT 表定义包含all_quoted=true,这意味着如果您在包含逗号的字符串周围使用双引号,整个字符串将被视为一个字段,问题就会消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2011-01-13
    • 2013-06-13
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多