addBatch()用法
查看文章
PreparedStatement.addbatch()的使用
Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用.
1.建立链接
Connection connection =getConnection();
2.不自动 Commit
connection.setAutoCommit(false);
3.预编译SQL语句,只编译一回哦,效率高啊
PreparedStatement statement = connection.prepareStatement("INSERT INTO TABLEX VALUES(?, ?)");
//记录1
statement.setInt(1, 1);
statement.setString(2, "Cujo");
statement.addBatch();
//记录2
statement.setInt(1, 2);
statement.setString(2, "Fred");
statement.addBatch();
//记录3
statement.setInt(1, 3);
statement.setString(2, "Mark");
statement.addBatch();
//批量执行上面3条语句.
int [] counts = statement.executeBatch();
//Commit it 到(DB)里面
**
已安装了microsoft 驱动程序
jdk1.4; win2000 server; MS sql2000;
*/
import java.sql.*;
class sql
{
public static void main(String[] agrs)
{
Connection cn=null;
Statement stmt=null;
String sql=null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println("Not find the Driver!");
}
try
{
String urls="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=webroot";//webroot 库名.TALBE 是表名;
String user="sa";
String password="password";
cn= DriverManager.getConnection(urls,user,password);
//stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
sql="select top 10 * from TABLE1";
ResultSet rs= stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString(2)+" "+rs.getString(3));
}
rs.first();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 1 "+rs.getString(3));
rs.last();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 2 "+rs.getString(3));
rs.previous();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 3 "+rs.getString(3));
rs.next();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 4 "+rs.getString(3));
rs.absolute(2);
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" 5 "+rs.getString(3));
/*
rs.afterLast();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" "+rs.getString(3));
System.out.print(rs.isAfterLast());
rs.beforeFirst();
System.out.print(rs.getRow()+" ");
System.out.println(rs.getString(2)+" "+rs.getString(3));
*/
String sql1="update TABLE1 set 题目=? where id=? ";
PreparedStatement stmt1 = cn.prepareStatement(sql1);
String stat = new String("盛夏话足部保健");
String stat1 = UnicodeToGB(stat);//解决中文问题
stmt1.setString(1,stat1);
stmt1.setInt(2,3423);
stmt1.executeUpdate();
cn.commit();
//System.out.println(stat1);
//System.exit(0);
//cn.setAutoCommit(false);
stmt.addBatch("update TABLE1 set 题目="盛夏话足部保健1" where id="3407"");
stmt.addBatch("update TABLE1 set 题目="夏季预防中暑膳食1" where id="3408"");
stmt.addBatch("INSERT INTO TABLE1 VALUES("11","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("12","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("13","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("14","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("15","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("16","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("17","12","13","","")");
stmt.addBatch("INSERT INTO TABLE1 VALUES("18","12","13","","")");
int [] updateCounts=stmt.executeBatch();
cn.commit();
stmt.close();
cn.close();
}
catch(SQLException e)
{
System.out.println("The SQLException error!");
}
}
/*
*/
public static String UnicodeToGB(String strIn){
byte[] b;
String strOut = null;
if(strIn == null || (strIn.trim()).equals(""))
return strIn;
try{
b = strIn.getBytes("GBK");
strOut = new String(b,"ISO8859_1");
}
catch(Exception e){
System.out.println("unicodeToGB exception : " + e.getMessage() + "\n");
}
return strOut;
}
/*
*/
}