| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:求助,经检查,JDBC程序里面的executeBatch ( )语句没有执行,为什么?
取消只看楼主 加入收藏
csy294745333
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2012-3-27
结帖率:9.09%
收藏
已结贴  问题点数:10 回复次数:0 
求助,经检查,JDBC程序里面的executeBatch ( )语句没有执行,为什么?
程序代码:
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

public class SimpledBatch
{
    public static void main ( String [ ] args ) throws Exception
    {
        // 加载ODBC驱动器
        Class.forName ( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";
        String usr = "sa";
        String password = "chensy";
        Connection con = DriverManager.getConnection ( url , usr , password );
        if ( con == null )
            System.out.println ( "can not establish connection" );
        else
        {
            System.out.println ( "connection is established." );
            SQLWarning w = con.getWarnings ( );
            if ( w != null )
            {
                w.printStackTrace ( );
            }
            while ( w != null )
            {
                String logMessage = "an SQLWarning occurred:" + w.getMessage ( ) + "error code:" + w.getErrorCode ( ) + "SQLState:" + w.getSQLState ( ) + "\n";
                System.out.println ( logMessage );
                w = w.getNextWarning ( );
            }
            try
            {
                Statement sta = con.createStatement ( );
                int rc = 0;
                rc = sta.executeUpdate ( "CREATE TABLE student(" + "SNO INTEGER," + "SNAME CHAR(10)," + "SAGE INTEGER," + "SDEPT CHAR(10))" );
                con.setAutoCommit ( false );
                sta.addBatch ( "insert into student" + "values(1,'张三',18,'计算机')" );
                sta.addBatch ( "insert into student" + "values(2,'李四',19,'信息')" );
                sta.addBatch ( "insert into student" + "values(3,'王五',18,'计算机')" );
                sta.addBatch ( "insert into student" + "values(4,'赵六',20,'信息')" );

                // 未执行
                int [ ] updateCount = sta.executeBatch ( );
                // 未执行

                System.out.println ( "测试" );
                 ( );
                con.setAutoCommit ( true );
                System.out.println ( "Update counts:" );
                for ( int i = 0 ; i < updateCount.length ; i ++ )
                {
                    System.out.println ( updateCount [ i ] );
                }
                ResultSet rset = sta.executeQuery ( "select * from student where sdept='计算机'" );
                while ( rset.next ( ) )
                {
                    int number = rset.getInt ( 1 );
                    String name = rset.getString ( 2 );
                    int age = rset.getInt ( 3 );
                    String dept = rset.getString ( 4 );
                    System.out.println ( "学号:" + number + "姓名:" + name + "年龄:" + age + "所在系:" + dept );
                }
                rset.close ( );
                sta.close ( );
                con.close ( );
            } catch ( BatchUpdateException se )
            {
                con.rollback ( );
            } catch ( SQLException se )
            {
                con.rollback ( );
                se.printStackTrace ( );
                while ( se != null )
                {
                    String logMessage = "an SQL Error occurred:" + se.getMessage ( ) + "error code:" + se.getErrorCode ( ) + "SQLState:" + se.getSQLState ( ) + "\n";
                    System.out.println ( logMessage );
                    se = se.getNextException ( );
                }
            } finally
            {
                con.close ( );
            }
        }
    }
}



测试了一下就是卡在executeBatch ( )那句话了,已经标出来了,求助
2013-12-11 15:27
快速回复:求助,经检查,JDBC程序里面的executeBatch ( )语句没有执行,为什么? ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019873 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved