这段时间开始学习写存储过程,主要原因还是因为工作需要吧,本来以为很简单的,但几经挫折,豪气消磨殆尽,但总算搞通了,为了避免后来者少走弯路,特记述与此,同时亦对自己进行鼓励。
一:无返回值的存储过程
存储过程为:
create or replace procedure testa(para1 in varchar2,para2 in varchar2) as
begin
insert into hyq.b_id (i_id,i_name) values (para1, para2);
end testa;
然后呢,在java里调用时就用下面的代码:
package com.hyq.src;
import java.sql.*;
import java.sql.resultset;
public class testprocedureone {
public testprocedureone() {
}
public static void main(string[] args ){
string driver = "oracle.jdbc.driver.oracledriver";
string strurl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq ";
statement stmt = null;
resultset rs = null;
connection conn = null;
callablestatement cstmt = null;
try {
class.forname(driver);
conn = drivermanager.getconnection(strurl, " hyq ", " hyq ");
callablestatement proc = null;
proc = conn.preparecall("{ call hyq.testa(?,?) }");
proc.setstring(1, "100");
proc.setstring(2, "testone");
proc.execute();
}
catch (sqlexception ex2) {
ex2.printstacktrace();
}
catch (exception ex2) {
ex2.printstacktrace();
}
finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (sqlexception ex1) {
}
}
}
}
当然了,这就先要求要建张表testtb,里面两个字段(i_id,i_name)。
二:有返回值的存储过程(非列表)
存储过程为:
create or replace procedure testb(para1 in varchar2,para2 out varchar2) as
begin
select into para2 from testtb where i_id= para1;
end testb;
在java里调用时就用下面的代码:
package com.hyq.src;
public class testproceduretwo {
public testproceduretwo() {
}
public static void main(string[] args ){
string driver = "oracle.jdbc.driver.oracledriver";
string strurl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
statement stmt = null;
resultset rs = null;
connection conn = null;
try {
class.forname(driver);
conn = drivermanager.getconnection(strurl, " hyq ", " hyq ");
callablestatement proc = null;
proc = conn.preparecall("{ call hyq.testb(?,?) }");
proc.setstring(1, "100");
proc.registeroutparameter(2, types.varchar);
proc.execute();
string testprint = proc.getstrin
闽公网安备 35060202000074号