老女,恶魔羽翼下的天使,手工毛衣
储存过程只在创建时编译一次存储在数据库服务端中,以后调用无需再编译,可以重复使用,比一般的sql语句执行效率快,并且可以设定某用户才可以使用该存储过程,提高了安全性。
create or replace procedure [存储过程名称]
(
[参数1] (in/out) [参数类型],---in为传入参数,out为输出参数
[参数2] (in/out) [参数类型],
)is
begin
[sql体];
(select name into [参数1] from test where id=[参数2];)
end
/**
*
*/
package com.test.procedure;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;
public class TestProcedure {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
Connection conn = getConn();
// String sql = "SELECT FULLNAME FROM USERS";
// PreparedStatement pstm = conn.prepareStatement(sql);
// ResultSet rs = pstm.executeQuery();
// while(rs.next()){
// System.out.println(rs.getString(1));
// }
CallableStatement cs = conn.prepareCall("{call test_procedure(?,?)}");//调用存储过程
cs.setInt(1, 11694);
cs.registerOutParameter(2, OracleTypes.VARCHAR);
cs.execute();
System.out.println(cs.getString(2));
closeConn(conn);
}
public static Connection getConn(){
Connection conn = null;
String name = "lims_data";
String pwd = "lims";
String url = "jdbc:oracle:thin:@10.80.36.91:1521/orcl";
String driver = "oracle.jdbc.driver.OracleDriver";
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,name,pwd);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn) throws SQLException{
if(conn!=null){
conn.close();
}
}
}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Oracle字符串函数length、upper、lower、initcap、 concat、instr、replace
安装jdk,tomcat,oracle,PL/SQL的一些问题
网友评论