oracle java api

一、项目结构

二、代码

import oracle.jdbc.OracleType;
import org.junit.Test;

import java.sql.*;

public class myApplication {
    /**
     * 测试java连接oracle
     * @throws Exception
     */
    @Test
    public void test01() throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "c##linlong", "123456");

        PreparedStatement statement = connection.prepareStatement("select * from person where pid = ?");
        statement.setObject(1, 4);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            System.out.println(resultSet.getString("pname"));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }

    /**
     * 调用存储过程
     * @throws Exception
     */
    @Test
    public void test02() throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "c##linlong", "123456");

        CallableStatement statement = connection.prepareCall("{call protestout(?,?)}");
        statement.setObject(1, "孟美岐");
        statement.registerOutParameter(2, OracleType.VARCHAR2);
        statement.execute();
        System.out.println(statement.getObject(2));

        statement.close();
        connection.close();
    }

    /**
     * 调用存储函数
     * @throws Exception
     */
    @Test
    public void test03() throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "c##linlong", "123456");

        CallableStatement statement = connection.prepareCall("{?=call sumpersonpidfun(?)}");
        statement.setObject(2, 4);
        statement.registerOutParameter(1, OracleType.NUMBER);
        statement.execute();
        System.out.println(statement.getObject(1));

        statement.close();
        connection.close();
    }
}
原文地址:https://www.cnblogs.com/linding/p/13839501.html