package com.oracle.tutorial.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types; public class StoredProcedureMySQLSample { private String dbName;
private Connection con;
private String dbms; public StoredProcedureMySQLSample(Connection connArg, String dbName,
String dbmsArg) {
super();
this.con = connArg;
this.dbName = dbName;
this.dbms = dbmsArg;
} public void createProcedureRaisePrice() throws SQLException {//新建存储过程 String createProcedure = null; String queryDrop = "DROP PROCEDURE IF EXISTS RAISE_PRICE"; createProcedure =
"create procedure RAISE_PRICE(IN coffeeName varchar(32), IN maximumPercentage float, INOUT newPrice numeric(10,2)) " +
"begin " +
"main: BEGIN " +
"declare maximumNewPrice numeric(10,2); " +
"declare oldPrice numeric(10,2); " +
"select COFFEES.PRICE into oldPrice " +//读取原价格
"from COFFEES " +
"where COFFEES.COF_NAME = coffeeName; " +
"set maximumNewPrice = oldPrice * (1 + maximumPercentage); " +
"if (newPrice > maximumNewPrice) " +
"then set newPrice = maximumNewPrice; " +
"end if; " +
"if (newPrice <= oldPrice) " +
"then set newPrice = oldPrice;" +
"leave main; " +
"end if; " +
"update COFFEES " +
"set COFFEES.PRICE = newPrice " +//写入新价格
"where COFFEES.COF_NAME = coffeeName; " +
"select newPrice; " +
"END main; " +
"end"; Statement stmt = null;
Statement stmtDrop = null; try {
System.out.println("Calling DROP PROCEDURE");
stmtDrop = con.createStatement();
stmtDrop.execute(queryDrop);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmtDrop != null) { stmtDrop.close(); }
} try {
stmt = con.createStatement();
stmt.executeUpdate(createProcedure);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
} } public void createProcedureGetSupplierOfCoffee() throws SQLException { String createProcedure = null; String queryDrop = "DROP PROCEDURE IF EXISTS GET_SUPPLIER_OF_COFFEE"; createProcedure =
"create procedure GET_SUPPLIER_OF_COFFEE(IN coffeeName varchar(32), OUT supplierName varchar(40)) " +
"begin " +
"select SUPPLIERS.SUP_NAME into supplierName " +
"from SUPPLIERS, COFFEES " +
"where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " +
"and coffeeName = COFFEES.COF_NAME; " +
"select supplierName; " +
"end";
Statement stmt = null;
Statement stmtDrop = null; try {
System.out.println("Calling DROP PROCEDURE");
stmtDrop = con.createStatement();
stmtDrop.execute(queryDrop);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmtDrop != null) { stmtDrop.close(); }
} try {
stmt = con.createStatement();
stmt.executeUpdate(createProcedure);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
} public void createProcedureShowSuppliers() throws SQLException {
String createProcedure = null; String queryDrop = "DROP PROCEDURE IF EXISTS SHOW_SUPPLIERS"; createProcedure =
"create procedure SHOW_SUPPLIERS() " +
"begin " +
"select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " +
"from SUPPLIERS, COFFEES " +
"where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " +
"order by SUP_NAME; " +
"end";
Statement stmt = null;
Statement stmtDrop = null; try {
System.out.println("Calling DROP PROCEDURE");
stmtDrop = con.createStatement();
stmtDrop.execute(queryDrop);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmtDrop != null) { stmtDrop.close(); }
} try {
stmt = con.createStatement();
stmt.executeUpdate(createProcedure);
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
} public void runStoredProcedures(String coffeeNameArg, float maximumPercentageArg, float newPriceArg) throws SQLException {
CallableStatement cs = null; try { System.out.println("\nCalling the procedure GET_SUPPLIER_OF_COFFEE");
cs = this.con.prepareCall("{call GET_SUPPLIER_OF_COFFEE(?, ?)}");//存储过程语句
cs.setString(1, coffeeNameArg);//in的跟以前一样赋值
cs.registerOutParameter(2, Types.VARCHAR);//执行前先注册out参数
cs.executeQuery();//执行 String supplierName = cs.getString(2);//获取结果,out参数为第二个 if (supplierName != null) {
System.out.println("\nSupplier of the coffee " + coffeeNameArg + ": " + supplierName);
} else {
System.out.println("\nUnable to find the coffee " + coffeeNameArg);
} System.out.println("\nCalling the procedure SHOW_SUPPLIERS");
cs = this.con.prepareCall("{call SHOW_SUPPLIERS}");
ResultSet rs = cs.executeQuery(); while (rs.next()) {
String supplier = rs.getString("SUP_NAME");
String coffee = rs.getString("COF_NAME");
System.out.println(supplier + ": " + coffee);
} System.out.println("\nContents of COFFEES table before calling RAISE_PRICE:");
CoffeesTable.viewTable(this.con); System.out.println("\nCalling the procedure RAISE_PRICE");
cs = this.con.prepareCall("{call RAISE_PRICE(?,?,?)}");
cs.setString(1, coffeeNameArg);
cs.setFloat(2, maximumPercentageArg);
cs.registerOutParameter(3, Types.NUMERIC);//inout类型的也要注册
cs.setFloat(3, newPriceArg); cs.execute();//如果不确定会返回几个ResultSet就用这个 System.out.println("\nValue of newPrice after calling RAISE_PRICE: " + cs.getFloat(3)); System.out.println("\nContents of COFFEES table after calling RAISE_PRICE:");
CoffeesTable.viewTable(this.con); } catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (cs != null) { cs.close(); }
}
}

 

关于RowSet和几个不常见类型(略) (mysql没有,oracle有)

 

用Swing界面展现结果(略)

 

 

Trail: JDBC(TM) Database Access(2)的更多相关文章

  1. Trail: JDBC(TM) Database Access(1)

    package com.oracle.tutorial.jdbc; import java.sql.BatchUpdateException; import java.sql.Connection; ...

  2. Trail: JDBC(TM) Database Access(3)

    java.sql,javax.sql,javax.naming包    默认TYPE_FORWARD_ONLY:结果集只能向前滚动,只能调用next(),不能重定位游标 TYPE_SCROLL_INS ...

  3. JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language

    JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language,结构化查询语言)数据库访问接口,它使数据 ...

  4. [19/05/06-星期一] JDBC(Java DataBase Connectivity,java数据库连接)_基本知识

    一.概念 JDBC(Java Database Connectivity)为java开发者使用数据库提供了统一的编程接口,它由一组java类和接口组成.是java程序与数据库系统通信的标准API. J ...

  5. JDBC (Java DataBase Connectivity)数据库连接池原理解析与实现

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  6. [19/05/07-星期二] JDBC(Java DataBase Connectivity)_CLOB(存储大量的文本数据)与BLOB(存储大量的二进制数据)

    一. CLOB(Character Large Object ) – 用于存储大量的文本数据 – 大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的.而非一般的字段,一次 ...

  7. [19/05/05-星期日] JDBC(Java DataBase Connectivity,java数据库连接)_mysql基本知识

    一.概念 (1).是一种开放源代码的关系型数据库管理系统(RDBMS,Relational Database Management System):目前有很多大公司(新浪.京东.阿里)使用: (2). ...

  8. 通过Oracle数据库访问控制功能的方法(Database access control)

    修改sqlnet.ora文件中的IP列表后都需要重启监听才能生效.(原文是: Any changes to the values requires the TNS listener to be sto ...

  9. [19/05/08-星期三] JDBC(Java DataBase Connectivity)_ORM(Object Relationship Mapping, 对象关系映射)

    一.概念 基本思想: – 表结构跟类对应: 表中字段和类的属性对应:表中记录和对象对应: – 让javabean的属性名和类型尽量和数据库保持一致! – 一条记录对应一个对象.将这些查询到的对象放到容 ...

随机推荐

  1. SendMessage、PostMessage原理

    SendMessage.PostMessage原理 本文讲解SendMessage.PostMessage两个函数的实现原理,分为三个步骤进行讲解,分别适合初级.中级.高级程序员进行理解,三个步骤分别 ...

  2. AC题目简解-数论

    反素数: HDU2521定义对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x ...

  3. 未能加载文件或程序集“Interop.jmail”或它的某一个依赖项

    未能加载文件或程序集“Interop.jmail”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中 ...

  4. [Codeforces677C]Vanya and Label(组合数学,快速幂)

    题目链接:http://codeforces.com/contest/677/problem/C 题意:给一个字符和数字的映射关系,然后再给一个字符串.问有多少个其他的字符串,使得那些字符串之间相互操 ...

  5. git push --no-thin

    有时候我们执行 git push 将一个 new branch 推送到远程仓库的时候,会被远程仓库阻止. 可能是我们没有相应的权限吧.然而,我在 git push 的时候加上 --no-thin 参数 ...

  6. Parallel WebDriver executions using TestNG

    In this post, we will see how does one make use of TestNG to kick off parallel UI tests using WebDri ...

  7. 函数buf_page_get_gen

    /********************************************************************//** This is the general functi ...

  8. Qt之自定义界面(窗体缩放-跨平台终极版)

    简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...

  9. LA 3266 (贪心) Tian Ji -- The Horse Racing

    题意: 田忌和齐王各有n匹马,如果马的速度比齐王的快就赢200,慢则输200,相等不赔不赚. 已知两人每匹马的速度(为整数)和齐王所排出的马的顺序,问田忌该如何应对才能使收益最大. 分析: 本以为是一 ...

  10. ASP.NET MVC实现多个按钮提交事件

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...