日期:2014-05-20  浏览次数:20815 次

大过年的,多撒些分,参与就有分,呵,请教一个有点怪的问题。。。。
说明如下:
是关于执行数据库脚本时出现的,先看如下代码(主要):
  public void execute(String sql) {  
  Connection conn = SqlConnection.getConnection();
  Statement stmt = null;  
  try {  
  conn.setAutoCommit(false);  
  stmt = conn.createStatement();  

  stmt.addBatch(sql);  
  int[] rows = stmt.executeBatch();  
  System.out.println("rows :" + Arrays.toString(rows));  
 
  conn.commit(); 
  conn.setAutoCommit(true);
   
  } catch (Exception e) {  
  conn.rollback();  
  conn.setAutoCommit(true);
  throw e;  
  } finally { 
  stmt.close();
  conn.close();  
  } 
   
  } 

脚本内容(一):
if exists(select * from TUser where userid = N'01')  
begin
  create table TUser 
  (
  userid varchar(10),
  userName varchar(50),
  userAge varchar(4),
  userAddress varchar(500)  
  )
end

脚本内容(二):
if not exists(select * from TUser where userid = N'01')  
begin
  create table TUser 
  (
  userid varchar(10),
  userName varchar(50),
  userAge varchar(4),
  userAddress varchar(500)  
  )
end

细心的朋友,应该知道,这第二个脚本就比前多一个"not"吧?
现在就取两个脚本的字符串内容,分别为:String sql1 和 String sql2,
现在分别执行:
execute(sql1);//此处执行成功,并任何异常
execute(sql2);//此处执行失败,控制台异常信息是:
Exception in thread "main" com.microsoft.jdbc.base.BaseBatchUpdateException: [Microsoft][SQLServer 2000 Driver for JDBC]Statements that produce result sets are not allowed in batch commands.
at com.microsoft.jdbc.base.BaseStatement.executeBatchEmulation(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeBatch(Unknown Source)
  ...

补充说明:两个脚本在查询分析器中执行均正常,由此可见第二个应该并非语法错误,sql2000也打开sp4补丁,我的jdk是1.6。
问题出来了,为何执行第二个会出现异常呢?请指教,若你没做过,估计很难靠经验解答,呵,若不相信会出错,那只好试试看了,应该不大可能是我系统问题吧?

------解决方案--------------------
up
------解决方案--------------------
顶一下,顺便接分!!!
------解决方案--------------------
应该就是有无not的问题
------解决方案--------------------
happy new year~^^
------解决方案--------------------
顶一下吧,真希望哪为高手能给个官方的解答。
------解决方案--------------------
顶一下,对脚本还不是太熟悉
------解决方案--------------------
Java code

try { 
            conn.setAutoCommit(false); 
            stmt = conn.createStatement();           
            
            stmt.execute("SET NOCOUNT ON");//加上这句实验下
            
            stmt.addBatch(sql);   
            int[] rows = stmt.executeBatch(); 
            System.out.println("rows :" + Arrays.toString(rows));

------解决方案--------------------
探讨
顶一下,顺便接分!!!

------解决方案--------------------
JDBC包的版本问题
------解决方案--------------------