JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Write a program to execute SQL function using CallableStatement.


Description:

A function is a named PL/SQL Block which is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value. Below example shows how to execute a SQL function using CallableStatement. Below example shows how to call a SQL function using CallableStatement. The query syntax should be {?= call myfunction(?,?)}


Code:
package com.java2novice.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

public class MyFunctionCallableStmt {

	public static void main(String a[]){
		
		Connection con = null;
		CallableStatement callSt = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.
					getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
						,"user","password");
			callSt = con.prepareCall("{?= call myfunction(?,?)}");
			callSt.setInt(1,200);
			//below method used to register data type of the out parameter
			callSt.registerOutParameter(2, Types.DOUBLE);
			callSt.execute();
			Double output = callSt.getDouble(2);
			System.out.println("The output returned from sql function: "+output);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try{
				if(callSt != null) callSt.close();
				if(con != null) con.close();
			} catch(Exception ex){}
		}
	}
}
<< Previous Program | Next Program >>
blog comments powered by Disqus

List Of All JDBC Programs:

  1. How to create JDBC Connection?
  2. What is Statement & ResultSet in JDBC?
  3. How to execute and read select queries using JDBC?
  4. How to update a record in the database using JDBC?
  5. How to execute any type of query in JDBC?
  6. What are the types of JDBC Statements available?
  7. Write an example code for JDBC prepared statement.
  8. Write an example for JDBC prepared statement with ResultSet.
  9. How to get primary key value (auto-generated keys) from inserted queries using JDBC?
  10. Write a simple program for CallableStatement statement to execute stored procedure.
  11. Write a program for CallableStatement statement with stored procedure returns OUT parameters.
  12. Write a program for CallableStatement statement with batch execution.
  13. Write a program to execute SQL function using CallableStatement.
  14. Write a program to execute SQL cursors using CallableStatement.
  15. How to get column properties from ResultSet using ResultSetMetaData?
  16. Write an example for batch update using Statement.
  17. Write an example for batch update using PreparedStatement.
  18. What are the types of ResultSets in JDBC?
  19. Write an example for scrollable result set with read only mode.
  20. Write an example for updatable result set.
  21. How to insert an image into database table? or Write an example for inserting BLOB into table.
  22. How to read an image from database table? or Write an example for reading BLOB from table.
  23. What is DatabaseMetaData? Write an example code.
  24. How to get JDBC Connection object using properties file?
Knowledge Centre
Prevent a method from being overridden
By specifying final keyword to the method you can avoid overriding in a subcalss. Similarlly one can use final at class level to prevent creating subclasses.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.