Oracle(20)—— Oracle JDBC进阶

PreparedStatement

1、灵活指定SQL语句中的变量。PreparedStatement是一种预编译,SQL表达式形式,效率较高。一般推荐使用,并且应用非常灵活。也可以防止SQL注入

1.1】使用示例如下:

 

package com.sinosoft.test_01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import utitys.DBConn;

public class TestPreparedStatement {

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            connection = DBConn.getConnection();
            String sql = "select ename, empno from emp where empno = ? and sal > ?";
            String sql2 = "select ename from emp where hiredate < ?";
            pst = connection.prepareStatement(sql2);
            
            //参数索引是从 1 开始计算的,而不是0
            //pst.setInt(1, 7369);
            //pst.setFloat(2, 2000.0f);
            
            pst.setDate(1, new java.sql.Date(new Date().getTime()));
            
            rs = pst.executeQuery();
            
            while (rs.next()) {
                System.out.println(rs.getString(1));
                System.out.println(rs.getString("ename"));
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            DBConn.close(rs, pst, connection);
        }
    }
    
}

 

CallableStatement

对存储过程的调用

2.1】使用示例如下:

  

create or replace procedure pro2(empnos in varchar2,enames out varchar2)
as 
begin 
select ename into enames from emp where empno=empnos;
end;

 

package com.xushuyi;

import java.sql.CallableStatement;
import java.sql.Connection;

import com.sinosoft.utility.DBConnPool;

public class TestProcedure {
    private static Connection _conn = null;
    private static CallableStatement _cstmt = null;

    public static void main(String[] args) {
        // 测试存储过程不包含参数
        // testProcedureNoArgs();

        // 测试存储过程包含参数
//        testProcedureWithArgs();
        
        //测试存储过程包含参数
        testProcedureWithArgs_01();

    }

    /**
     * 
     */
    private static void testProcedureWithArgs_01() {
        try {
            _conn = DBConnPool.getConnection();
            // 创建存储过程的对象
            _cstmt = _conn.prepareCall("{call pro3(?, ?, ?)}");

            // 给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
            _cstmt.setInt(1, 1600);
            _cstmt.setString(3, "SALESMAN");

            // 注册存储过程的第二个参数
            _cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);

            // 执行Oracle存储过程
            _cstmt.execute();

            // 得到存储过程的输出参数值并打印出来
            System.out.println("存储过程返回值输出:" + _cstmt.getString(2));

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                if (_cstmt != null) {
                    _cstmt.close();
                    _cstmt = null;
                }
                if (_conn != null) {
                    _conn.close();
                    _conn = null;
                }
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
        
    }

    /**
     * 测试用例01
     * 测试存储过程包含参数 
     */
    private static void testProcedureWithArgs() {
        try {
            _conn = DBConnPool.getConnection();
            // 创建存储过程的对象
            _cstmt = _conn.prepareCall("{call pro2(?,?)}");

            // 给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
            _cstmt.setInt(1, 7499);

            // 注册存储过程的第二个参数
            _cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);

            // 执行Oracle存储过程
            _cstmt.execute();

            // 得到存储过程的输出参数值并打印出来
            System.out.println("存储过程返回值输出:" + _cstmt.getString(2));

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                if (_cstmt != null) {
                    _cstmt.close();
                    _cstmt = null;
                }
                if (_conn != null) {
                    _conn.close();
                    _conn = null;
                }
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }
    }

    /**
     * 测试存储函数不包含参数
     */
    private static void testProcedureNoArgs() {
        try {
            _conn = DBConnPool.getConnection();
            // 创建存储过程的对象
            _cstmt = _conn.prepareCall("{call pro1(?)}");

            // 给Oracle存储过程的参数设置值 ,将第一个参数的值设置成188
            _cstmt.setInt(1, 188);

            // 执行Oracle存储过程
            _cstmt.execute();

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                if (_cstmt != null) {
                    _cstmt.close();
                    _cstmt = null;
                }
                if (_conn != null) {
                    _conn.close();
                    _conn = null;
                }
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
        }

    }
}

Batch

Batch 批处理使用,成批调用SQL语句,效率较高

 

package com.sinosoft.test_01;

import java.sql.Connection;
import java.sql.PreparedStatement;

import utitys.DBConn;

public class TestBatch {

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement pst = null;
        try {
            connection = DBConn.getConnection();
            String sql = "insert into dept values (?, ?, ?)";
            pst = connection.prepareStatement(sql);
            
            pst.setInt(1, 10);
            pst.setString(2, "hahha");
            pst.setString(3, "b");
            pst.addBatch();
            
            pst.setInt(1, 10);
            pst.setString(2, "hahha");
            pst.setString(3, "b");
            pst.addBatch();
            
            pst.setInt(1, 10);
            pst.setString(2, "hahha");
            pst.setString(3, "b");
            pst.addBatch();
            
            pst.executeBatch();
            
            pst.close();
            
            connection.close();
            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

 

 

 

 

  

 

posted @ 2016-06-28 10:16  xu_shuyi  阅读(165)  评论(0)    收藏  举报