日期:2014-05-16  浏览次数:20419 次

oracl如果存在记录更新否则插入新纪录
DECLARE
vcount number;
begin
select count(*) into vcount from zhang.test where aname='t' and bname='t';

if vcount=0 then
   insert into zhang.test values(3,'t1','t1','t1','t1',50);
else
    update zhang.test set abcount= abcount + 80 where aname='t' and bname='t';
end if;
commit;
end;


--创建存储过程
create or replace procedure record_new_order(
rcount in number
)
is
vcount number;  
begin  
select count(*) into vcount from zhang.test where aname='t' and bname='t';  
  
if vcount=0 then  
   insert into zhang.test values(3,'t1','t1','t1','t1',rcount);  
else  
    update zhang.test set abcount= abcount + rcount where aname='t' and bname='t';  
end if;   
end;
--执行存储过程--
begin 
record_new_order(10);
end;

	
//调用存储过程
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		Class.forName("oracle.jdbc.driver.OracleDriver");

		// DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
		String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
		Connection conn = java.sql.DriverManager.getConnection(dbUrl, "zhang", "password");

		String sql = "{call record_new_order(?)}";
		CallableStatement cs = conn.prepareCall(sql);
		cs.setInt(1,10);
		
//		cs.registerOutParameter(20, Types.INTEGER);
		cs.execute();
		// ResultSet rs = cs.executeQuery();
		// int k= rs.getMetaData().getColumnCount();
		// System.out.println(cs.getObject(2));
		// ResultSet rs = (ResultSet)cs.getObject(2);
		// while (rs.next()){
		// for (int i=1;i<k+1;i++){
		// System.out.print(rs.getObject(i)+"|");
		// }
		// System.out.println();
		//      
		// }
		cs.close();
		conn.close();
	}