天天看點

oracle PreparedStatement批量插入復原

1、建立連接配接

這個是一切連接配接資料庫的基礎,當然這個也比較簡單。

一下是一個連接配接Sql2000的資料庫程式,還有一個顯示一個表的方法。

import java.*;

public class ConnectData{

private java.sql.Connection con = null;

private java.sql.Statement stmt=null;

private final String url = "jdbc:microsoft:sqlserver://";

private final String serverName= "192.168.0.100";

private final String portNumber = "1433";

private final String databaseName= "hkmis"; //資料庫名稱

//mydata是手動建的資料庫

private final String userName = "hkmisuser";

private final String password = "hkmis123456";

public ConnectData(){}

private String getConnectionUrl(){

return url+serverName+":"+portNumber+";databaseName="+databaseName+";";

}

private java.sql.Connection getConnection(){

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);

if(con!=null) System.out.println("Connection Successful!");

}catch(Exception e){

e.printStackTrace();

System.out.println("Error Trace in getConnection() : " + e.getMessage());

}

return con;

}

public void displayData(){

java.sql.ResultSet rs = null;

try{

con= this.getConnection();

if(con!=null){

stmt=con.createStatement();

rs=stmt.executeQuery("select * from userinfo");

//userinfo是mydata中的使用者資訊表(username,password)

while(rs.next()){

System.out.println( rs.getString("username")); //顯示所有使用者

//System.out.println( rs.getString(2));

//System.out.println( rs.getString(3));

}

rs.close(); //這裡一定要記得關閉順序

//先關閉ResultSet,然後關閉Statement(或者PreparedStatement);最後關閉Connection

rs = null;

closeConnection();

}else System.out.println("Error: No active Connection");

}catch(Exception e){

e.printStackTrace();

}

}

private void closeConnection(){

try{

if(con!=null)

con.close();

con=null;

}catch(Exception e){

e.printStackTrace();

}

}

//執行測試。。。

public static void main(String[] args) throws Exception

{

ConnectData myDbTest = new ConnectData();

myDbTest.displayData();

}

}

這裡要強調的是,不同的資料庫除了使用的Driver不同之外,連接配接的方式也是不同的,例如,mysql最好加上字型參數,以免出現亂碼。而oracle就不需要規定資料庫名稱。

2、執行sql語句

  1)用Statement來執行sql語句

  String sql;

  Statement sm = cn.createStatement();

  sm.executeQuery(sql); // 執行資料查詢語句(select)

  sm.executeUpdate(sql); // 執行資料更新語句(delete、update、insert、drop等)statement.close();

  2)用PreparedStatement來執行sql語句

  String sql;

  sql = "insert into user (id,name) values (?,?)";

  PreparedStatement ps = cn.prepareStatement(sql);

  ps.setInt(1,xxx);

  ps.setString(2,xxx);

  ...

  ResultSet rs = ps.executeQuery(); // 查詢

  int c = ps.executeUpdate(); // 更新

  3、處理執行結果

  查詢語句,傳回記錄集ResultSet

  更新語句,傳回數字,表示該更新影響的記錄數

  ResultSet的方法

  1、next(),将遊标往後移動一行,如果成功傳回true;否則傳回false

  2、getInt("id")或getSting("name"),傳回目前遊标下某個字段的值

  4、釋放連接配接

  cn.close();

  一般,先關閉ResultSet,然後關閉Statement(或者PreparedStatement);最後關閉Connection

  可滾動、更新的記錄集

  1、建立可滾動、更新的Statement

  Statement sm = cn.createStatement(ResultSet.TYPE_SCROLL_ENSITIVE,ResultSet.CONCUR_READ_ONLY);

  該Statement取得的ResultSet就是可滾動的

  2、建立PreparedStatement時指定參數

  PreparedStatemet ps = cn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

  ResultSet.absolute(9000);

  ·批量更新,這個非常重要,是多Sql語句結合體的方法。

  1、Statement

  Statement sm = cn.createStatement();

  sm.addBatch(sql1);

  sm.addBatch(sql2);

  ...

  sm.executeBatch()

  一個Statement對象,可以執行多個sql語句以後,批量更新。這多個語句可以是delete、update、insert等或兼有

  2、PreparedStatement

  PreparedStatement ps = cn.preparedStatement(sql);

  {

  ps.setXXX(1,xxx);

  ...

 [color=red] ps.addBatch();[/color]

  }

 [color=red] ps.executeBatch();[/color]  一個PreparedStatement,可以[color=blue]把一個sql語句,變換參數多次執行,一次更新。[/color]  ·事務的處理

  1、關閉Connection的自動送出,這在oracle中效果非常明顯。

  cn.setAutoCommit(false);

  2、執行一系列sql語句

  要點:執行每一個新的sql語句前,上一次執行sql語句的Statement(或者PreparedStatemet)必須先close

  Statement sm ;

  sm = cn.createStatement(insert into user...);

  sm.executeUpdate();

  sm.close();

  sm = cn.createStatement("insert into corp...);

  sm.executeUpdate();

  sm.close();

  3、送出

  cn.commit();

4、如果發生異常,那麼復原。例如發現插入不完整時候,都應該復原保證資料的完整性。

  cn.rollback();