JDBC调用存储过程的简单案列

DataBase:

    

    create table person(Id int primary key, name char(20),age int);

    create or replace procedure myPerson(pid in int,name in char,age in int)

    as

   begin

   insert into person values(pid,name,age);

   end;

CallStat.java

    public class CallStat{

   public static void main(String[]args){

   Connection conn=null;

   CallableStatement stmt=null;

   try{

            Class.forName("oracle.jdbc.driver.OracleDriver");

            String url="jdbc:orcle:thin:@127.0.0.1:orcl";

            conn=DriverManager.getConnection(url,"scott","tiger");

            stmt=conn.prepareCall("{call myPerson(?,?,?)}")

            stmt.setInt(100);

            stmt.setString(2,"test");

            stmt.setInt(3,26);

            stmt.execute();

            stmt.close();

        }catch(Exception e){

            System.out.println(e);

        }finally{

           try{

                  if(conn!=null)

                      conn.close();

             }catch(Exception e){

            System.out.println(e);

              }

          }

      }

}

猜你喜欢

转载自zhangzhifang.iteye.com/blog/1675316