java-无法确定为什么此SQL语句无法执行(Derby)
作者:互联网
我的表是这样设置的,似乎可以正确创建. (没有引发异常)
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超类中,并且对于其他子类也可以正常工作.
代码如下:
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
您的表创建很可能成功了,您只是因为没有读取结果而变得错误.
正如a_horse_with_no_name在评论中提到的那样,使用PreparedStatement.executeUpdate()
更适合此类声明.
标签:derby,sql,java 来源: https://codeday.me/bug/20191111/2018325.html