IDEA连接Oracle数据库并调用存储过程

public class jdbcTest {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@192.168.238.130:1521:orcl";
    String username = "scott";
    String password = "tiger";
    /**
     *
     * jdbc连接Oracle数据库,并对emp表进行查询操作
     */
    @Test
    public void  findAll() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName(driver);
        //2.获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        //3.执行SQL语句,获取预编译对象
        PreparedStatement preparedStatement = connection.prepareStatement("select * from emp");
        //4.执行查询
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getInt(1)+" "+resultSet.getString("ename"));
        }
        //5.释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }

/**
     *
     * jdbc连接oracle数据库并调用存储过程,(需要在控制台输出结果)
     *
     */
    @Test
    public void callProcedure2() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName(driver);
        //2.获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        //3.调用存储过程,获取调用语句
        CallableStatement callableStatement = connection.prepareCall("{call pro_total_sal(?,?)}");
        //4.设置参数1,注册参数2
        callableStatement.setInt(1,7499);
        callableStatement.registerOutParameter(2,OracleTypes.INTEGER);
        //5.执行查询,
        callableStatement.execute();
        System.out.println(callableStatement.getInt(2));
        //5.释放资源
        callableStatement.close();
        connection.close();
    }
}    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值