The Artima Developer Community
Sponsored Link

Java Buzz Forum
JDBC - CallableStatement In Out Parameters (Oracle)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
JDBC - CallableStatement In Out Parameters (Oracle) Posted: Jul 13, 2015 9:35 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: JDBC - CallableStatement In Out Parameters (Oracle)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=JWNbxg17Rso&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
JDBC - CallableStatement In Out Parameters (Oracle) 
JDBC - CallableStatement In Out Parameters (Oracle) 
JDBC - CallableStatement In Out Parameters (Oracle) 

JDBCCallableStatementDemo.java

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCCallableStatementDemo
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:xe";

// Database credentials
static final String USERNAME = "hr";
static final String PASSWORD = "hr";

public static void main(String[] args)
{
JDBCCallableStatementDemo jdbcCallableStatementDemo = new JDBCCallableStatementDemo();

Scanner scanner = new Scanner(System.in);
while (true)
{
System.out.print("Enter the CountryId :");
String countryId = scanner.nextLine();

if (countryId.equals("exit"))
{
break;
}

jdbcCallableStatementDemo.getCountryName(countryId);

}
scanner.close();
}

private void getCountryName(String countryId)
{
Connection connection = null;
CallableStatement callableStatement = null;
try
{
/*
* Register the JDBC driver in DriverManager
*/


Class.forName(JDBC_DRIVER);

/*
* Establish connection to the Database using DriverManager
*/


connection = DriverManager
.getConnection(DB_URL, USERNAME, PASSWORD);

String plSql = "{call GET_COUNTRY_NAME(?,?)}";

callableStatement = connection.prepareCall(plSql);

/*
* Bind IN parameter first, then bind OUT parameters
*/


callableStatement.setString(1, countryId);

/*
* Register OUT Parameters
*/

callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);

/*
* Use execute method to run the stored procedure.
*/


callableStatement.execute();

/*
* Retrieve countryName with getXXX method
*/

String countryName = callableStatement.getString(2);

System.out.println("countryName : " + countryName);

}
catch (SQLException se)
{
se.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
/*
* finally block used to close resources
*/

try
{
if (callableStatement != null)
{
callableStatement.close();
}
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}

}
}
Output
Enter the CountryId :AR
countryName : Argentina
Enter the CountryId :IN
countryName : India
Enter the CountryId :exit
To Download JDBCCallableStatementDemoOracleINOUTApp Project Click the below link
https://sites.google.com/site/javaee4321/jdbc/JDBCCallableStatementDemoOracleINOUTApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: JDBC - CallableStatement In Out Parameters (Oracle)

    Topic: Oracle : Stored Procedure with Input and Output Parameters Previous Topic   Next Topic Topic: Throttle methods with Spring AOP and Guava rate limiter

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use