mysql jar 包版本更新问题

 最近将以前写的代码重构了一遍,在重构的过程中一时兴起将mysql的jar包由5.0.4更新为5.1.16,启动项目后发现出现了一些异常情况。
首先是在插入数据的时候获得主键时抛出异常:org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL []; SQL state [S1009]; error code [0]; Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().; nested exception is java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().


解决方法:
将PreparedStatement ps = con.prepareStatement(sql);修改为
PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
  @Override
  public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
      PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
      return ps;
  }
}, keyHolder);

其次就是在同一张表进行连接查询时老是抛出找不到列的异常:

Nested in org.springframework.jdbc.InvalidResultSetAccessException: 列名无效; nested exception is java.sql.SQLException: 列名无效:java.sql.SQLException: 列名无效

该查询的sql语句为:select t1.a, t1.b, t1.c, t1.d,  t1.e,  t2.a x, t2.b y, t2.c z, from table_a t1 left join table_a t2 on t1.c = t2.a where t1.a=?

SqlRowSet srs = jdbcTemplate.queryForRowSet(sql);
if(srs.next()){
    ...
    srs.getInt("x");
    ...
}

 在执行到srs.getInt("x")代码时就抛出上述异常了,事实上是所有的t2列都出现列名无效的错误,该问题还没有解决掉,但将jar包换回之前的版本5.0.4就一切正常了。

  这次汲取了一个经验:不到万不得已的情况下不要轻易的将项目的环境升级,如果实在要升级也要将项目中与升级了的相关功能模块重新测试一编。

猜你喜欢

转载自dmh920.iteye.com/blog/1104374