Oracle uses function functions to query data return cursors

Source: Internet
Author: User
Create or Replace function test111 (ItemNumber in varchar2) return sys_refcursor 
 is
  return_cursor sys_refcursor; 
begin
   OPEN return_cursor for SELECT ' a ' dual WHERE 1 = itemnumber; 
  return return_cursor; 

End test111;

Use the following SQL to return the cursor, in the PL SQL developer can directly point to open the query results

Select test111 (1) from dual;


Applicable conditions: Can be applied when the SQL statement is too long, avoid having too long SQL code in the Java code.


JDBC Call result set

Package com.dahuatech.job;

Import java.sql.CallableStatement;
Import java.sql.Connection;
Import Java.sql.DriverManager;

Import Oracle.jdbc.driver.OracleResultSet;
Import Oracle.jdbc.driver.OracleTypes;

public class Test {public

	static void Main (string[] args) throws Exception {
		Class.forName (" Oracle.jdbc.driver.OracleDriver ");
		String url = "Jdbc:oracle:thin:@10.30.5.106:1521:agile9";

		Connection conn = drivermanager.getconnection (URL, "agile", "* * *");
		
		String sql =  "{? = Call test111 (?)}";
		CallableStatement CST = conn.preparecall (SQL);
		Cst.registeroutparameter (1, oracletypes.cursor);
		Cst.setstring (2, "1");
		Cst.execute ();
		Oracleresultset rs = (Oracleresultset) cst.getobject (1);
		while (Rs.next ()) {
			System.out.println (rs.getstring ("a"));
		}
	}



A function of the jdbctemplete call of the SPRINGMVC frame returned as a string

public string Transf (final String Inmodel) {return
		Jdbctemplate.execute ("{? = Call Transfmodel (?)} ', new Callablestatementcallback<string> () {
			
			@Override public
			String Doincallablestatement (CallableStatement cs)
					throws SQLException, DataAccessException {
				Cs.registeroutparameter (1, Oracletypes.varchar);
				Cs.setstring (2, Inmodel);
				Cs.execute ();
				Return (String) cs.getobject (1);}}
			
		);
	


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.