我的表是这样设置的,似乎可以正确创建。 (没有引发异常)
String sqlString = String.format(
"CREATE TABLE %s(%s BIGINT,
%s VARCHAR(%d),
%s VARCHAR(%d),
%s VARCHAR(%d),
%s INT,
%s INT,
%s BIGINT,
PRIMARY KEY (%s))",
//
TABLE_NAME, //
Column.ID.name, //
Column.MAKE.name, Column.MAKE.length, //
Column.MODEL.name, Column.MODEL.length, //
Column.SERIAL_NUMBER.name, Column.SERIAL_NUMBER.length, //
Column.YEAR.name, //
Column.MILEAGE.name, //
Column.CUSTOMER_ID.name, //
Column.ID.name);
我正在尝试插入以下内容:
String sqlString = String.format(//
"INSERT INTO %s values(?, ?, ?, ?, ?, ?, ?)", TABLE_NAME);
boolean result = execute(sqlString, //
motorcycle.getId(), // long
motorcycle.getMake(), // String
motorcycle.getModel(), // String
motorcycle.getSerialNumber(), // String
motorcycle.getYear(), // int
motorcycle.getMileage(), // int
motorcycle.getCustomerId()); //long
execute(sqlString, args[])
返回false
,但不会引发任何异常。derby.log也没有有关发生问题的信息。
我使用了错误的SQL数据类型吗?
编辑:
execute(sqlString, args[])
方法在Dao父类(super class)中,并且对于其他子类也可以正常工作。代码如下:
protected boolean execute(String preparedStatementString, Object... args) throws SQLException {
LOG.debug(preparedStatementString);
boolean result = false;
PreparedStatement statement = null;
try {
Connection connection = Database.getConnection();
statement = connection.prepareStatement(preparedStatementString);
int i = 1;
for (Object object : args) {
statement.setObject(i, object);
i++;
}
statement.executeUpdate();
result = true;
} finally {
close(statement);
}
return result;
}
最佳答案
从PreparedStatement javadoc for execute(String sql):
很可能您的表创建成功,因为没有要读取的结果,所以只是得到false
。
正如a_horse_with_no_name在评论中提到的那样,使用 PreparedStatement.executeUpdate()
更适合于此类声明。