Java调用Oracle存储过程(无返回值)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kalision/article/details/7173503

无返回值:


1:创建存储过程

此存储过程向表中插入一条数据


create or replace procedure demo_procedure(id varchar2,name varchar2,age varchar2,sex varchar2,address varchar2)
as
begin
insert into system.demo values(id,name,age,sex,address);
end;


2:java代码


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class TestProcedure {
	static Connection conn = null;
	static CallableStatement cstmt = null;
	PreparedStatement pstmt = null;
	static String url ="jdbc:oracle:thin:@localhost:1521:myoracle";
	static String driver = "oracle.jdbc.driver.OracleDriver";
	String name="";
	public static void main(String args[]){
		System.out.println("hello kalsiion");
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,"system","admin");
			cstmt = conn.prepareCall("{call demo_procedure(?,?,?,?,?)}");
			cstmt.setString(1, "12");
			cstmt.setString(2, "kalision");
			cstmt.setString(3, "23");
			cstmt.setString(4, "男");
			cstmt.setString(5, "塞浦路斯");
			cstmt.executeUpdate();
			System.out.println("存储过程已经成功执行...");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				cstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


OK!

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页