sql server2005 jdbc解决自动增长列统一处理问题纪实
|
源代码网整理以下背景:系统要支持多种数据库,统一insert的时候获取自动增长列的处理方式 源代码网整理以下问题1:insert+select方案 源代码网整理以下sqlserver2000可以使用insertSql + SELECT @@IDENTITY AS "Identity"的方式来获得,但是mysql则只能使用executeUpdate(insertSql); 源代码网整理以下executeQuery("SELECT last_insert_id() ")这样的方式,否则会抛出异常:java.sql.SQLException: Can not issue data manipulation statements with executeQuery() 源代码网整理以下而两句话分开处理,总让人感觉不放心,如果并发操作比较多,存在潜在的危险。 源代码网整理以下问题2:getGeneratedKeys方案 源代码网整理以下mysql的驱动支持jdbc3.0,实现了getGeneratedKeys(),但sqlserver2000 jdbc驱动不支持getGeneratedKeys() 源代码网整理以下解决思路: 源代码网整理以下google了一下,据说2005是支持的,所以下下来看看 源代码网整理以下下载地址:url:http://download.microsoft.com/download/2/8/9/289dd6a3-eeeb-46dc-9045-d0c6b59bfbc1/sqljdbc_1.1.1501.101_chs.exe 源代码网整理以下使用小结: 源代码网整理以下1.2005jdbc驱动是支持jdbc 3.0 的。也就是说,的确支持getGeneratedKeys()方法。 源代码网整理以下2.2005jdbc驱动url和包组织变动较大。 源代码网整理以下举例对比: 源代码网整理以下================ 源代码网整理以下3.包的位置:环境变量CLASSPATH不是必须的。但sqljdbc.jar必须放置在tomcat/common/lib中(如果是租用空间,可能得跟对方商量了,看是否支持2005的驱动)。 源代码网整理以下否则会发生异常:Cannot load JDBC driver class"com.microsoft.sqlserver.jdbc.SQLServerDriver" 源代码网整理以下测试示例方法: 源代码网整理以下 public String executeUpdate(String sql) throws SQLException ...{ 源代码网整理以下 |
