日期:2014-05-16 浏览次数:20615 次
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC_Batch_Test {
/**
* @param args
*/
public static void main(String[] args) {
// JDBC批处理 执行批量语句
// 插入emp dept
Connection connection = OracleConnection.getConnection();
Statement statement = null;
String sqlDept = "insert into dept values(55,'批处理部门','福建')";
String sqlEMP = "insert into emp(empno,ename,sal,deptno) values(8001,'JDBC',5000,55)";
try {
// /设置自动提交为false
connection.setAutoCommit(false);
statement = connection.createStatement();
statement.addBatch(sqlDept);
statement.addBatch(sqlEMP);
statement.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 出现异常需要回滚数据
try {
connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
statement.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("OK");
}
}
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
public class TestCallStatement {
static void noReturnValue() {
// create or replace procedure add_dept(id_p number, dname_p
// varchar2,loc_p varchar2) is
// begin
// --不会受父事务的影响
// insert into dept values(id_p,dname_p,loc_p);
// commit;--进行自主提交业务
// dbms_output.put_line('过程提交完成...');
// end add_dept;
Connection connection = OracleConnection.getConnection();
CallableStatement callableStatement = null;
try {
callableStatement = connection
.prepareCall("{call add_dept(?,?,?)}");
// 位置
// callableStatement.setInt(1, 56);
// callableStatement.setString(2, "JDBC过程插入");
// callableStatement.setString(3, "福州");
// 名称
callableStatement.setInt("id_p", 57);
callableStatement.setString("dname_p", "JDBC过程插入57");
callableStatement.setString("loc_p", "福州57");
callableStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
callableStatement.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("OK");
}
static void haveReturnValue_notList() {
// create or replace procedure out_pro(empno_p in number,
// ename_p out varchar2,
// sal_p out number) is
// begin
// select ename, sal into ename_p, sal_p from emp where empno = empno_p;
// exception
// when NO_DATA_FOUND then
// ename_p := 'NULL';
// sal_p := -1;
// end out_pro;
Connection connection = OracleConnection.getConnection();
CallableStatement callableStatement = null;
try {
callableStatement = connection.prepareCall("{call out_pro(?,?,?)}");
callableStatement.setInt(1, 8888);
callableStatement.registerOutParameter(2, Types.VARCHAR);
callableStatement.registerOu