我的表是这样设置的,似乎创建正确。 (不抛出异常)
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) :
Returns:
true if the first result is a ResultSet object; false if it is an update count or there are no results
很可能您的表创建成功了,您只是得到了 false
,因为没有可读取的结果。
作为a_horse_with_no_name在评论中提到,使用 PreparedStatement.executeUpdate()
会更适合这种说法。
关于java - 无法确定此 SQL 语句不执行的原因 (Derby),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44820382/