java连接oracle

Posted zhz-8919

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java连接oracle相关的知识,希望对你有一定的参考价值。

/**
     * 连接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的主要内容,如果未能解决你的问题,请参考以下文章

ArcGis连接oracleoracle配置

ArcGis连接oracleoracle配置

OracleOracle错误 ora-12514 检查以及解决方法

OracleOracle Database 12c Release 2安装多图详解

ORACLEoracle 日志文件管理

OracleOracle中使用转义字符