Trail: JDBC(TM) Database Access(2)
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)的更多相关文章
- Trail: JDBC(TM) Database Access(1)
package com.oracle.tutorial.jdbc; import java.sql.BatchUpdateException; import java.sql.Connection; ...
- Trail: JDBC(TM) Database Access(3)
java.sql,javax.sql,javax.naming包 默认TYPE_FORWARD_ONLY:结果集只能向前滚动,只能调用next(),不能重定位游标 TYPE_SCROLL_INS ...
- JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language
JDBC(Java Database Connectivity,Java数据库连接)API是一个标准SQL(Structured Query Language,结构化查询语言)数据库访问接口,它使数据 ...
- [19/05/06-星期一] JDBC(Java DataBase Connectivity,java数据库连接)_基本知识
一.概念 JDBC(Java Database Connectivity)为java开发者使用数据库提供了统一的编程接口,它由一组java类和接口组成.是java程序与数据库系统通信的标准API. J ...
- JDBC (Java DataBase Connectivity)数据库连接池原理解析与实现
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...
- [19/05/07-星期二] JDBC(Java DataBase Connectivity)_CLOB(存储大量的文本数据)与BLOB(存储大量的二进制数据)
一. CLOB(Character Large Object ) – 用于存储大量的文本数据 – 大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的.而非一般的字段,一次 ...
- [19/05/05-星期日] JDBC(Java DataBase Connectivity,java数据库连接)_mysql基本知识
一.概念 (1).是一种开放源代码的关系型数据库管理系统(RDBMS,Relational Database Management System):目前有很多大公司(新浪.京东.阿里)使用: (2). ...
- 通过Oracle数据库访问控制功能的方法(Database access control)
修改sqlnet.ora文件中的IP列表后都需要重启监听才能生效.(原文是: Any changes to the values requires the TNS listener to be sto ...
- [19/05/08-星期三] JDBC(Java DataBase Connectivity)_ORM(Object Relationship Mapping, 对象关系映射)
一.概念 基本思想: – 表结构跟类对应: 表中字段和类的属性对应:表中记录和对象对应: – 让javabean的属性名和类型尽量和数据库保持一致! – 一条记录对应一个对象.将这些查询到的对象放到容 ...
随机推荐
- App应用与思考
我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...
- javascript基础之数组对象
一.定义数组的方法: 定义了一个空数组: var myArray =new Array(); 指定有n个空元素的数组: var myArray=new Array(n); 定义数组并赋值: var m ...
- redhat 新装后不能联网
1.ifconfig查看是否有ip地址,如果没有就配置,命令如下: [root@redhat ~]# system-config-network 设置DHCP 为 [*] [ok]后,重新ifconf ...
- hadoop拾遗(一)---- 避免切分map文件
有些程序可能不希望文件被切分,而是用一个mapper完整处理每一个输入文件.例如,检查一个文件中所有记录是否有序,一个简单的方法是顺序扫描第一条记录并并比较后一条记录是否比前一条要小.如果将它实现为一 ...
- 1002: A+B for Input-Output Practice (II)
问题描述: http://acm.wust.edu.cn/problem.php?id=1002&soj=0 代码实现: import java.util.Scanner; public cl ...
- bzoj4025
首先我们要知道,怎么去维护一个是否是二分图 二分图的充要条件:点数>=2且无奇环 重点就是不存在奇环,怎么做呢 考虑随便维护一个图的生成树,不难发现,如果一条边加入后,形成奇环的话就不是二分图 ...
- C# 实现对接电信交费易自动缴费
他有这样一个JS PassGuardCtrl.js 部分代码 1 defaults:{ 2 obj:null, 3 random:null,/ ...
- UVa 12186 Another Crisis
题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...
- UVA 10765 Doves and bombs(双连通分量)
题意:在一个无向连通图上,求任意删除一个点,余下连通块的个数. 对于一个非割顶的点,删除之后,原图仍连通,即余下连通块个数为1:对于割顶,余下连通块个数>=2. 由于是用dfs查找双连通分量,树 ...
- C# winform 窗体从右下角向上弹出窗口效果
参考自 http://blog.csdn.net/yilan8002/article/details/7197981 /// <summary> /// 窗体动画函数 注意:要引用Syst ...