码迷,mamicode.com
首页 > 数据库 > 详细

java连接oracle

时间:2019-07-03 10:42:18      阅读:547      评论:0      收藏:0      [点我收藏+]

标签:callable   oid   cep   ber   out   public   not   drive   exe   

/**
     * 连接oracle
     */
    @Test
    public void connectTest() {
        String driver="oracle.jdbc.OracleDriver";
        String url="jdbc:oracle:thin:@192.168.43.208:1521:orcl";
        String username="scott";
        String password="tiger";

        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            String sql = "select * from emp";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getObject(1));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 访问存储过程
     */
    @Test
    public void connectTest1() {
        String driver="oracle.jdbc.OracleDriver";
        String url="jdbc:oracle:thin:@192.168.43.208:1521:orcl";
        String username="scott";
        String password="tiger";

        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            CallableStatement callableStatement = connection.prepareCall("{call getYearSal(?,?)}");
            callableStatement.setInt(1,7788);
            callableStatement.registerOutParameter(2, OracleTypes.NUMBER);
            callableStatement.execute();
            Object object = callableStatement.getObject(2);
            System.out.println(object);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 访问存储函数
     */
    @Test
    public void connectTest2() {
        String driver="oracle.jdbc.OracleDriver";
        String url="jdbc:oracle:thin:@192.168.43.208:1521:orcl";
        String username="scott";
        String password="tiger";

        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            CallableStatement callableStatement = connection.prepareCall("{? = call getSals(?)}");
            callableStatement.setInt(2,7788);
            callableStatement.registerOutParameter(1, OracleTypes.NUMBER);
            callableStatement.execute();
            Object object = callableStatement.getObject(1);
            System.out.println(object);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

java连接oracle

标签:callable   oid   cep   ber   out   public   not   drive   exe   

原文地址:https://www.cnblogs.com/zhz-8919/p/11124819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!