Only for reference.
import java.sql.*; public class TestCallPrecedure { public static void main(String[] args) { Connection connection = null; try { connection = getConnection(); // Creates a CallableStatement for executing the // stored procedure String query = "call GET_DETAIL_BY_PRODUCT(?, ?, ?, ?)"; CallableStatement cb = connection.prepareCall(query); // Sets the input parameter cb.setString(1, "bag"); // Registers the out parameters cb.registerOutParameter(2, Types.VARCHAR); cb.registerOutParameter(3, Types.DECIMAL); cb.registerOutParameter(4, Types.INTEGER); // Executes the query cb.executeQuery(); // Gets the query result output System.out.println("Code : " + cb.getString(2)); System.out.println("Price : " + cb.getBigDecimal(3)); System.out.println("Quantity: " + cb.getInt(4)); } catch (Exception e) { e.printStackTrace(); } finally { try { closeConnection(connection); } catch (SQLException e) { e.printStackTrace(); } } } /** * Get a connection to database. * @return a connection to database. * @throws Exception when an exception occurs. */ private static Connection getConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/kodejavadb"; return DriverManager.getConnection(url, "root", ""); } /** * Close a connection to database. * @param connection a connection to be closed. * @throws SQLException when an exception occurs. */ private static void closeConnection(Connection connection) throws SQLException { if (connection != null && !connection.isClosed()) { connection.close(); } } }
Mysql Stored Procedure:
DROP PROCEDURE IF EXISTS kodejavadb.GET_DETAIL_BY_PRODUCT; CREATE PROCEDURE kodejavadb.`GET_DETAIL_BY_PRODUCT`(IN vproduct varchar(30), OUT vcode varchar(5), OUT vprice decimal, OUT vqty int) BEGIN SELECT code INTO vcode FROM orcl.products WHERE name = vproduct; SELECT price INTO vprice FROM orcl.products WHERE name = vproduct; SELECT qty INTO vqty FROM orcl.products WHERE name = vproduct; END;
相关推荐
java数据库连接CallableStatement
CallableStatement的用法
这是一个很好的存储过程的应用例子!赶快下载吧!
CallableStatement调用Oracle存储过程返回结果集(ResultSet).doc
JDBC基础教程之CallableStatement.doc )
CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, "xxxxxxxx"); callableStatement...
在Java中调用: package cn.bl.v2; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Types; import org.junit.Test; import cn.bl.DBUtil;
一个学员管理系统的实例,演示了JDBC一些拓展的特性,例如PrepareStatement、CallableStatement、ResultSetMetaData、大对象、DataSource等。
CallableStatement 对象为所有的 DBMS 提供了一种以标准形式调用已储存过程的方法...两种形式都可带有数量可变的输入(IN 参数)、输出(OUT 参数)或输入和输出(INOUT 参数)的参数。问号将用作参数的占位符。
7 – CallableStatement本概述是从《JDBCTM Database Access from JavaTM: A Tutorial and Annotated Reference 》这本书中摘引来的。JavaSoft 目前正在准备这本书。这本书是一本教程,同时也是 JDBC 的重要参考手册...
。。。
。。。
主要介绍了Java的JDBC中Statement与CallableStatement对象实例,JDBC是Java编程中用于操作数据库的API,需要的朋友可以参考下
2.得到CallableStatement对象, CallableStatement cst=conn.prepareCall("{call 过程名(?,?... )}"); //每个?代表过程中的一个参数 3.给输入参数赋值 4.注册输出参数,比如: cst.registerOutParameter(2, oracle....
JDBC学习的简单教程,包括如何连接数据库,使用Statement,PreparedStatement,CallableStatement,元数据等。 主要用于教学和自学
System.out.println("I am print in Test"); } void showme() { print(); } } class TestDemo { public static void main(String[] args) { Test t=new Test() ______________ } } 下面关于类描述...
4.2.2 IN、OUT及INOUT参数的使用 4.2.3 执行CallableStatement Object对象 4.2.4 CallableStatement对象使用实例 4.2.5 SQL Server存储过程编程经验技巧 4.3 成批更新(BatchedUpdate) 4.3.1 成批更新所使用的...
CallableStatement proc = null; try { Class.forName(driver); conn = DriverManager.getConnection(strUrl, "oshr", "q1w2e3r4"); proc = conn.prepareCall("{ call dev.P_GetEmpInfo(?,?,?,?)}"); ...