当尝试对大分隔文本文件进行数据处理时,我决定使用数据库系统来完成工作,并提高速度。我需要我开发的程序来完成6个关键过程,以完成预期的目标。
我决定使用Apache的Derby数据库系统,因为它能够做到1并且能够做到其他4的能力。
我创建了数据库:
String connectionName = "jdbc:derby:" + databaseName;
if (createDatabase) {
connectionName += ";create=true";
}
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connectionToDatabase = DriverManager.getConnection(connectionName);
这样就可以了,然后我从文件中获取第一条记录并获取字段数量,以便了解所需的列数:
String statement = "CREATE TABLE " + tableName + " (";
for (int i = 1; i <= tableAmount; i++) {
statement += (char) (64 + i) + " VARCHAR(100)";
if (i != tableAmount) {
statement += ",";
}
}
PreparedStatement pstmt = connectionToDatabase.prepareStatement(statement + ")");
pstmt.executeUpdate();
connectionToDatabase.commit();
这会按照我选择的名称创建一个表(我尝试简单地创建一个名为“HELLO”的表)然后我尝试将文件导入到我制作的表中:
try {
String schemaName = "APP";
String tableName = "HELLO";
String fileName = "C:\\Hello.txt";
String columnDelimiter = fieldDelimiter;
String characterDelimiter = "";
String codeset = "UTF-8";
short replace = 0;
Import.importTable(connectionToDatabase, schemaName, tableName, fileName,
columnDelimiter, characterDelimiter, codeset,
replace, false);
} catch (SQLException e) {
do {
System.out.println("SQLState:" + e.getSQLState());
System.out.println("Error Code:" + e.getErrorCode());
System.out.println("Message:" + e.getMessage());
Throwable t = e.getCause();
while (t != null) {
System.out.println("Cause:" + t);
t = t.getCause();
}
e = e.getNextException();
StackTraceElement st[] = e.getStackTrace();
for (int i = 0; i < st.length; i++) {
System.out.println("Stack Trace " + i + ":" + st[i]);
}
} while (e != null);
}catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
但是,当我运行这一切时,我得到的是:
Exception: Syntax error: DERBY-PROPERTIES
我做错了什么,如果可能的话,我应该怎样做才能使这项工作?
编辑:它在调用Import.importTable的行上出错。在错误修改我的输出(上面)后,我现在得到以下输出:
SQLState:42X01
Error Code:30000
Message:Syntax error: DERBY-PROPERTIES.
Cause:java.sql.SQLException: Syntax error: DERBY-PROPERTIES.
Cause:ERROR 42X01: Syntax error: DERBY-PROPERTIES.
Stack Trace 0:org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
Stack Trace 1:org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
Stack Trace 2:org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
Stack Trace 3:org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
Stack Trace 4:org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
Stack Trace 5:org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
Stack Trace 6:org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
Stack Trace 7:org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
Stack Trace 8:org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
Stack Trace 9:org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
Stack Trace 10:org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
Stack Trace 11:org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
Stack Trace 12:org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
Stack Trace 13:org.apache.derby.impl.load.Import.performImport(Unknown Source)
Stack Trace 14:org.apache.derby.impl.load.Import.importTable(Unknown Source)
Stack Trace 15:root.DatabaseManager.<init>(DatabaseManager.java:46)
Stack Trace 16:root.Startup.main(Startup.java:21)
以下是我的derby.log
的输出Wed Nov 14 20:04:23 CST 2012:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.9.1.0 - (1344872): instance a816c00e-013b-01cf-55d8-000000c58148 on database directory -omitted- with class loader sun.misc.Launcher$AppClassLoader@1ba34f2 Loaded from file:/-omitted-/lib/derby.jar java.vendor=Sun Microsystems Inc. java.runtime.version=1.6.0_33-b05 user.dir=-omitted-
derby.system.home=null
Database Class Loader started - derby.database.classpath=''
找出问题所在:
我使用的功能不正确。它可以用于一些编辑,但正确的是
Statement s = connectionToDatabase.createStatement();
s.execute("CALL SYSCS_UTIL.SYSCS_IMPORT_TABLE(null,'" + tableName
+ "','" + filePath + "','" + columnDelimiter
+ "',null,null,1)");
connectionToDatabase.commit();
答案 0 :(得分:0)
首先,您可以从异常中获取更多信息;方法如下:http://wiki.apache.org/db-derby/UnwindExceptionChain
其次,你应该在磁盘上找到你的'derby.log'文件;它会有更多关于出了什么问题的信息。
第三,一旦你弄清楚你发给数据库的确切陈述,以及代码中你发表该陈述的地方,用这些信息更新你的问题,我们就可以提供更多的帮助。