import java.io.*;
import java.sql.*;
class  simpledbBatch
{
	public static void main(String[] args) throws SQLException,IOException,ClassNotFoundException,InstantiationException,IllegalAccessException
	{  //加载ODBC驱动器
		Class.forName("sun.jdbc.odbj.JdbcOdbcDriver");
		//String sdURL=;
		Connection con=DriverManager.getConnection("jdbc:odbc:aaa","","");
		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();
			con.commit();
			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();}	    
}
	}
 }
