Oracle's JDBC Approach

Source: Internet
Author: User
Tags value store

 Packagecom.swift.jdbc_oracle;Importjava.sql.CallableStatement;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;Importoracle.jdbc.OracleTypes;Importoracle.jdbc.driver.OracleCallableStatement; Public classORACLE_JDBC {String driver= "Oracle.jdbc.driver.OracleDriver"; String URL= "JDBC:ORACLE:THIN:@192.168.189.101:1521:ORCL"; String username= "Scott"; String Password= "Tiger"; Connection Conn=NULL; PreparedStatement Statement=NULL; ResultSet RS=NULL; CallableStatement Call=NULL; Oraclecallablestatement Oraclecall=NULL; @Before Public voidInit ()throwsClassNotFoundException, SQLException {class.forname (driver); Conn=drivermanager.getconnection (Url,username,password); }    //General Query     Public voidTest ()throwsClassNotFoundException, SQLException {String SQL= "SELECT * from emp"; Statement=conn.preparestatement (SQL); RS=Statement.executequery ();  while(Rs.next ()) {System.out.println (Rs.getint ("Empno") + "~" +rs.getstring ("ename") + "~" +rs.getstring ("sal") + "~" +rs.getstring ("comm") + "~" +rs.getstring ("Deptno")); } System.out.println ("Test~~~~~~~~~"); }    //Access single return value stored procedure Create or Replace procedure Getyearsal (Eno in number,yearsal out number)//{? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} //SQL syntax Format {call <procedure-name>[(<ARG1>,<ARG2>, ...)]}     Public voidProcedure ()throwsSQLException {String SQL= "{call Getyearsal (?,?)}"; Pager=conn.preparecall (SQL); //the input is the same as before PreparestatementCall.setint (1, 7788); //what type of output is the parameter to register?Call.registeroutparameter (2, Oracletypes.number);        Call.execute (); //to query the stored procedure before outputting the result        DoubleYearsal = call.getdouble (2);            System.out.println (yearsal); }    //access cursor return value stored procedure Create or Replace procedure Getemps (DNO in Number,emps out sys_refcursor)         Public voidProcedure2 ()throwsSQLException {String SQL= "{call Getemps (?,?)}"; Pager=conn.preparecall (SQL); //the input is the same as before PreparestatementCall.setint (1, 20); //what type of output is the parameter to register?Call.registeroutparameter (2, Oracletypes.cursor);        Call.execute (); //to query the stored procedure before outputting the resultObject o = Call.getobject (2); RS=(ResultSet) o;  while(Rs.next ()) {System.out.println (Rs.getint ("Empno") + "~" +rs.getstring ("ename") + "~" +rs.getstring ("sal") + "~" +rs.getstring ("comm") + "~" +rs.getstring ("Deptno")); }    }    //access cursor return value stored procedure (oraclecallablestatement) Create or replace procedure Getemps (DNO in Number,emps out sys_refcursor)         Public voidProcedure3 ()throwsSQLException {String SQL= "{call Getemps (?,?)}"; Oraclecall=(oraclecallablestatement) conn.preparecall (SQL); //the input is the same as before PreparestatementOraclecall.setint (1, 20); //what type of output is the parameter to register?Oraclecall.registeroutparameter (2, Oracletypes.cursor);        Oraclecall.execute (); //to query the stored procedure before outputting the resultrs = Oraclecall.getcursor (2);  while(Rs.next ()) {System.out.println (Rs.getint ("Empno") + "~" +rs.getstring ("ename") + "~" +rs.getstring ("sal") + "~" +rs.getstring ("comm") + "~" +rs.getstring (" Deptno)); }    }    //Access single return value store function create or Replace function X (Eno in number) return number//{? = call <procedure-name>[(<ARG1>,<ARG2>,. ...)]}         Public voidfunction ()throwsSQLException {String SQL= "{? = Call X (?)}"; Pager=conn.preparecall (SQL); //the input is the same as before PreparestatementCall.setint (2, 7788); //what type of output is the parameter to register?Call.registeroutparameter (1, Oracletypes.number);        Call.execute (); //to query the stored procedure before outputting the result        DoubleYearsal = call.getdouble (1);             System.out.println (yearsal); }    //Access single return value store function create or Replace function X (Eno in number) return number//Do not use this format {? = Call <procedure-name>[(<ARG1>,<ARG2>,. ...)]} Using the Storage function Select Method@Test Public voidFunction2 ()throwssqlexception{String SQL= "Select X (7788) yearsal from dual";//cannot write one more, write, or report invalid character exceptionsStatement=conn.preparestatement (SQL); RS=Statement.executequery ();  while(Rs.next ()) {System.out.println (Rs.getint ("Yearsal")); }} @After Public voidClose ()throwsSQLException {if(rs!=NULL) {rs.close (); }        if(statement!=NULL) {statement.close (); }        if(conn!=NULL) {conn.close (); }            }}

The Oracle package needs to be poured into the project first Ojdbc14-10.2.0.1.0.jar

Oracle's JDBC Approach

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.