求助,经检查,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 ( )那句话了,已经标出来了,求助









