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的属性名和类型尽量和数据库保持一致! – 一条记录对应一个对象.将这些查询到的对象放到容 ...
随机推荐
- maven项目配置Jetty服务器
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin ...
- Android Studio:libpng warning: iCCP: Not recognizing known sRGB profile that has been edited解决办法
把以前的eclipse的项目导入Android Studio中,Build项目的时候,出现了一堆错误. 如下: AAPT err(Facade for 1944774242): ERROR: 9-pa ...
- 进程控制的一些api
转自:http://blog.chinaunix.net/uid-26833883-id-3222794.html 1.fork() ,vfork() 创建进程 2‘ exec()类 在进程中执行其他 ...
- servlet基础讲解
基本知识一.Web结构1.两种应用程序 ①桌面应用程序:QQ.CS.MyEclipse.Office.DW.360.浏览器等必须下载.安装.桌面快捷方式.注册表信息.操作系统后台服务.占用操作系统端口 ...
- linux的chmod与chown命令详解
使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案存取权限分为三级 : 档案拥有者.群组.其他.利用 ...
- iOS开发:在Swift中调用oc库
先列举这个工程中用到的oc源码库: MBProgressHUD:半透明提示器,Loading动画等 SDWebImage:图片下载和缓存的库 MJRefresh: 下拉刷新,上拉加载 Alamofir ...
- 可视化PK纯代码
简述 其实今天说的内容不仅仅局限于Qt,在很多其它语言或者框架中也适用,那就是 - 用可视化工具or文本编辑器?拖or不拖? 如果有人问我喜欢脱or不脱?我会毫不犹豫地说不脱,因为我比较矜持O(∩_∩ ...
- Asp.Net多线程用法1
Asp.Net多线程简单用法 一个web页面 default.aspx 里面有两个控件GridView1,GridView2,通过两个线程分别加载绑定数据. protected void Page_L ...
- 图解VS2010打包全过程
原文转自:http://blog.csdn.net/shan9liang/article/details/6957308 最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家 ...
- error LNK2019: 无法解析的外部符号 __imp___CrtDbgReportW
error LNK2005 and error LNK2019 error LNK2019: unresolved external symbol __imp___CrtDbgReportW refe ...