一、Oracle数据新建测试表和存储过程
1、新建test表
create table test( name varchar2(5) primary key, password varchar2(5) not null )
2、新建带参数的存储过程pro_add_test用来向test表中插入一条数据
create or replace procedure pro_add_test(m_name in test.name%type,m_password in test.password%type) is begin insert into test values(m_name,m_password); end;
3、测试一下存储过程,插入一条数据
begin pro_add_test('p','1'); end;
二、Java内中调用存储过程
package procedureTransfer; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Procedure { public static Connection getConn(){//获得数据库连接对象 Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } return conn; } public void pro_add(String name,String pass){//调用存储过程 Connection conn = getConn(); try { CallableStatement cal = conn.prepareCall("{call pro_add_test(?,?)}"); cal.setString(1, name); cal.setString(2, pass); cal.execute(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) {//main函数中测试 Procedure p = new Procedure(); p.pro_add("test", "pass"); } }
数据库中查看就会发现多了一天“test”-“pass”的数据。