package testcase;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 调用存储过程插入表数据
* @author study_monkey
* * 数据库新建表
* create table Test_tbl
(
i_id INTEGER,
i_name VARCHAR2(20)
)
*新建存储过程
* CREATE OR REPLACE PROCEDURE TEST_Produce(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS
BEGIN
INSERT INTO TEST_PRO (I_ID,I_NAME) VALUES (PARA1, PARA2);
END TEST_Produce;
* @param args
*/
public class TestCallProduce {
private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
private static final String URL="jdbc:oracle:thin:@127.0.0.1:1521:testdb";
private String name="username";
private static String pwd="password";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
CallableStatement proc = null;
public void callProduce(){
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL,name,pwd);
proc = conn.prepareCall("{ call Test_Produce(?,?) }"); //调用存储过程
proc.setString(1, "1"); //第一个输入参数
proc.setString(2, "one");//第二个输入参数
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) {
}
}
}
public static void main(String[] args) {
TestCallProduce test = new TestCallProduce();
test.callProduce();
}
}