源码如下:
/**
* 获取插入时的主键值
*/
public int getKey(Order o) {
final String sql = "insert into tb_order (user_id,order_money,address, postalcode,phone,mobile,email,sendType,payType, postscript) values (?,?,?,?,?,?,?,?,?,?)";
final int user_id = o.getUserId();
int i = 0;
final double order_money = o.getOrderMoney();
final String address = o.getAddress();
final String postalcode = o.getPostalcode();
final String phone = o.getPhone();
final String mobile = o.getMobile();
final String email = o.getEmail();
final String sendType = o.getSendType();
final String payType = o.getPayType();
final String postscript = o.getPostScript();
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection arg0)
throws SQLException {
PreparedStatement ps = DBUtils.getDs().getConnection()
.prepareStatement(sql);
ps.setInt(1, user_id);
ps.setDouble(2, order_money);
ps.setString(3, address);
ps.setString(4, postalcode);
ps.setString(5, phone);
ps.setString(6, mobile);
ps.setString(7, email);
ps.setString(8, sendType);
ps.setString(9, payType);
ps.setString(10, postscript);
return ps;
}
}, keyHolder);
i = keyHolder.getKey().intValue();
return i;
}
public static void main(String[] args) {
Order o = new Order(7, 25, "hk", "432222", "1111", "1214334", "3453435",
"1", "1", "无");
IOrderDaoImpl dao = new IOrderDaoImpl();
System.out.println(dao.getKey(o));
}
错误日志:
三月 01, 2017 10:45:47 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
三月 01, 2017 10:45:47 上午 org.springframework.jdbc.support.SQLErrorCodesFactory
信息: SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [null]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:607)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:824)
at dao.impl.IOrderDaoImpl.getKey(IOrderDaoImpl.java:58)
at dao.impl.IOrderDaoImpl.main(IOrderDaoImpl.java:85)
Caused by: java.sql.SQLException: 不允许的操作
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getGeneratedKeys(OracleStatement.java:4559)
at org.apache.commons.dbcp.DelegatingStatement.getGeneratedKeys(DelegatingStatement.java:318)
at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:829)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:591)
... 3 more
程序走到template.update方法时报错