文本表的jdbc-hsqldb异常

时间:2013-03-01 07:18:32

标签: java jdbc hsqldb

我的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

任何帮助都会很明显。

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

MODIFIED:发生异常是因为其中一个列值中包含逗号"Mind, Body Connection-Banner-728x90-HEAMBA-S"。由于hsqldb将csv作为表读取,因此hsql将附加的逗号解释为附加列。任何人都可以指导我如何规避这个吗?

1 个答案:

答案 0 :(得分: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"注意NumberFormatException表示预期的数字,而是找到报告的字符串。
  3. 幸运的是,表中只有一个数字(INT)列,因此很容易看到CVS中有一个额外的逗号,它会终止最后一个VARCHAR字符串并导致问题。
  4. TEXT表定义包含all_quoted=true,这意味着如果在包含逗号的字符串周围使用双引号,则整个字符串将被视为字段,问题将消失。