1.1.1 Preparations
create a table
--- create test table School
the CREATE TABLE School (
ID Number The, - Schools the above mentioned id
NAME VARCHAR2 (30) --- school name
);
--- add data
INSERT into school values ( 1, 'Peking');
INSERT INTO School values (2, 'Nanjing University');
INSERT INTO School values (1, 'tokyo University');
COMMIT;
· write java code to connect to a database, get a connection object connection
public class OracleUtil {
// 加载Oracle驱动
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@iP:1521:orcl", "system",
"密码");
return connection;
}
}
1.1 --- --- storage function
*
1. What is the storage function
·
storage function, also known as a custom function. You may receive one or more parameters and returns a result. In the function, we can use PL / SQL processing logic.
·
Storing function syntax structure
CREATE [OR REPLACE] FUNCTION function name
(parameter name [in | out | in out] parameter type, ...)
the RETURN
outcome variable data type
IS
declaration section;
the BEGIN
logic portion;
the RETURN
outcome variables;
[ eXCEPTION
exception processing section
the END;
. 1.1.1 Create function
· here simply returns a data value may be returned varchar2 Cursor, Table..
--- create a function name address queries the address ID.
the CREATE OR fn_getName the REPLACE the fUNCTION (SID NUMBER)
the rETURN return type VARCHAR2 ---
AS
r_name VARCHAR2 (30); --- declare variables
the BEGIN
the SELECT name r_name the INTO the FROM School the WHERE ID = SID; --- queries the name stored in the r_name
RETURN r_name; - Returns r_name
the END;
1.1.2 PL / the SQL. oracle function call
--- PL / SQL calls the procedure using a function, parameter passing 2 = ID
SELECT fn_getName (2) Dual schools from;
Java code calls the function oracle
`
/ *
invoke the function oRACLE
@param SID
* /
public void static getenamefun (int sid) {
String sname;
java.sql.Connection conn = null;
java.sql.PreparedStatement stmt = null;
String SQL = "? call fn_getName = {} (?)"; // Called
CallableStatement fun = null;
{the try
Conn OracleUtil.getConnection = ();
Fun = conn.prepareCall (SQL);
fun.setInt (2, SID);
fun.registerOutParameter (. 1, Types.VARCHAR); // Parameter Type outgoing register
fun.execute ( );
sname = fun.getString (. 1); // get the values returned by the result
System.out.println (sname);
} the catch (SQLException E) {
e.printStackTrace ();
}
}
1.2. ——-存储过程——-
· 1.什么是存储过程
存储过程是被命名的PL/SQL块,存储于数据库中,是数据库对象的一种。应用程序可以调用存储过程,执行相应的逻辑。
存储过程与存储函数都可以封装一定的业务逻辑并返回结果,存在区别如
下:
1.存储函数中有返回值,且必须返回;而存储过程没有返回值,可以通过
传出参数返回多个值。
2.存储函数可以在select语句中直接使用,而存储过程不能。过程多数是
被应用程序所调用。
3.存储函数一般都是封装一个查询结果,而存储过程一般都封装一段事务代码
存储函数语法结构
CREATE [ OR REPLACE ] PROCEDURE 存储过程名称(参数名 类型,
参数名 类型,参数名 类型...)
IS|AS
变量声明部分;
BEGIN
逻辑部分
[EXCEPTION
异常处理部分
END;
注意:
1.参数只指定类型,不指定长度
2.过程参数的三种模式:
IN :传入参数(默认)
OUT :传出参数主要用于返回程序运行结果
IN OUT :传入传出参数
1.3. 一:不带传出参数
OR PROCEDURE pro_insert the REPLACE the CREATE (
ID NUMBER,
NAME VARCHAR2 ---- Note that without a length, can not be written (VARCHAR (30))
)
the AS
the BEGIN
the INSERT the INTO School values (ID, NAME);
the END;
1.3.1. PL/SQL调用过程
CALL pro_insert(4,'郑州大学');
· 1
1.3.2. java代码调用oracle函数
static void callPr public () {
the java.sql.Connection Conn = null;
java.sql.PreparedStatement stmt = null;
the try {
Conn OracleUtil.getConnection = ();
stmt = conn.prepareCall ( "Call pro_insert {(,)?? } "); // call the stored procedure is provided for the keyword.
stmt.setInt (. 1,. 4); // set the first input parameter
stmt.setInt (2," Zhengzhou University "); // set the second input parameter
stmt.execute (); // execute
} the catch (SQLException E) {
e.printStackTrace ();
}
}
1.4. 二:带传出参数
---. 带传出参数
```
CREATE OR REPLACE PROCEDURE pro_getCount(s_name VARCHAR2,s_count OUT NUMBER)
AS
BEGIN
SELECT COUNT(*) INTO s_count FROM school
WHERE name LIKE '%'||s_name||'%';
END;
. 1.4.1 PL / SQL call the procedure
--- PL / SQL calls
`` `
DECLARE
s_count Number The; - define the parameters of variables came
the begin
pro_getCount ( 'university', s_count); --- execution
DBMS_OUTPUT.put_line ( 'query result, the number:' || s_count); --- print the results
end;
1.4.2. java代码调用oracle函数
static void pro_Gount public (String name) {
the java.sql.Connection Conn = null;
java.sql.CallableStatement Sets stmt = null;
the try {
Conn DaoUtil.getConnection = ();
stmt = conn.prepareCall ( "Call pro_getCount {(?, ?)} "); // set the stored procedure call as a keyword.
stmt.setString (1," the University "); // set the first input parameter
stmt.registerOutParameter (2, OracleTypes.NUMBER); // set the first two input parameters
stmt.execute (); // performed.
// after machining, the placeholder column corresponding to it
int COUNT = stmt.getInt (2);
System.out.println ( "query results "+ COUNT); //. 4
} the catch (SQLException E) {
e.printStackTrace ();
}
}
1.5. 二:带传出参数
---- 返回值为游标
OR PROCEDURE pro_cursor the REPLACE the CREATE (s_cur a SYS_REFCURSOR OUT)
the AS
the BEGIN
the OPEN s_cur the FOR School the SELECT * the FROM;
the END;
1.5.1 PL / calls the SQL procedure.
DECLARE
s_cur a SYS_REFCURSOR; - Outgoing defined variable parameter
s_row school% ROWTYPE; - the outgoing parameters assigned to s_row
the begin
pro_cursor (s_cur); - execution
loop - loop result
FETCH s_cur INTO s_row;
EXIT the WHEN s_cur% NOTFOUND;
dbms_output.put_line (s_row.name);
End loop;
End;
1.5.2. java代码调用oracle函数
/ *
Return result Cursor
* /
public static void pro_cur () {
the java.sql.Connection Conn = null;
java.sql.CallableStatement Sets stmt = null;
the try {
Conn DaoUtil.getConnection = ();
stmt = conn.prepareCall ( " {call pro_cursor (?)} " ); // call the stored procedure is provided for the keyword
stmt.registerOutParameter (1, OracleTypes.CURSOR); // set the first input parameter
stmt.execute (); // performed.
// after machining, the placeholder column corresponding to it
the ResultSet the resultSet = (the ResultSet) stmt.getObject (. 1);
the while (ResultSet.next ()) {
String ID = resultSet.getString ( "ID");
String name = resultSet.getString ( "name" );
System.out.println ( "ID" + id + "school name" + name);
}
} the catch (SQLException E) {
e.printStackTrace ();
}
}