oracle學習筆記(二十三)——JDBC調用存儲過程以及批量操作


jdbc調用存儲過程

使用並獲得out模式的參數返回值

//存儲過程為sum_sal(deptno department.deptno%type,sum in out number)
CallableStatement cs =conn.prepareCall("{call sum_sal(?,?)}"); 
cs.setInteger(1,7879);
cs.setDouble(2,0.0);//第二個傳什么都無所謂,因為第二個參數是in out模式,是作為輸出的
cs.registerOutParameter(2,java.sql.Types.Double,2);//最后那個參數是保留小數點2位
cs.excute();//執行會返回一個boolean結果

//獲得結果,獲取第二個參數
double result = cs.getDouble(2);

獲得oracle返回的結果集

//存儲過程為list(result_set out sys_refcursor, which in number)
CallableStatement cs =conn.prepareCall("{call list(?,?)}"); 
cs.setInteger(2,1);
cs.registerOutParameter(1,racleTypes.CURSOR);
cs.execute();
//獲得結果集
ResultSet rs = (ResultSet)cs.getObject(1);

批量操作

批量插入

People表中只有兩列,id和name ,還有對應的一個實體類People
批量操作應該放到事務里進行,因為它會存在某條語句執行失敗的情況。

public int[] insetBatch(List<People> list) {
	try (Connection connection = JdbcUtil.getConnection();
		 PreparedStatement ps = connection.prepareStatement("insert into PEOPLE values (?,?)");
	) {
		// 關閉事務自動提交,手動提交
		connection.setAutoCommit(false);
		//從list中取出數據
		for (People people : list) {
			ps.setInt(1, people.getId());
			ps.setString(2, people.getName());
			//加入到指語句組中
			ps.addBatch();
		}
		int[] recordsEffect = ps.executeBatch();
		// 提交事務
		connection.commit();
		return recordsEffect;

	} catch (SQLException e) {
		e.printStackTrace();
	}
	return null;
}

批量插入測試

public static void main(String[] args) {
	List<People> list = new ArrayList<>();
	int id = 1;
	list.add(new People(id++, "james"));
	list.add(new People(id++, "andy"));
	list.add(new People(id++, "jack"));
	list.add(new People(id++, "john"));
	list.add(new People(id++, "scott"));
	list.add(new People(id++, "jassica"));
	list.add(new People(id++, "jerry"));
	list.add(new People(id++, "marry"));
	list.add(new People(id++, "alex"));

	int[] ints = new BatchTest().insetBatch(list);

	System.out.println(Arrays.toString(ints));
}

批量更新

public int[] updateBatch(List<People> list) {
	try (Connection connection = JdbcUtil.getConnection();
		 PreparedStatement ps = connection.prepareStatement("undate people set name=? where id=?");
	) {
		// 關閉事務自動提交,手動提交
		connection.setAutoCommit(false);
		//從list中取出數據
		for (People people : list) {
			ps.setInt(1, people.getId());
			ps.setString(2, people.getName());
			//加入到指語句組中
			ps.addBatch();
		}
		int[] recordsEffect = ps.executeBatch();
		// 提交事務
		connection.commit();
		return recordsEffect;

	} catch (SQLException e) {
		e.printStackTrace();
	}
	return null;
}

批量刪除

public int[] updateBatch(List<People> list) {
	try (Connection connection = JdbcUtil.getConnection();
		 PreparedStatement ps = connection.prepareStatement("delete people where id=?");
	) {
		// 關閉事務自動提交,手動提交
		connection.setAutoCommit(false);
		//從list中取出數據
		for (People people : list) {
			ps.setInt(1, people.getId());
			//加入到指語句組中
			ps.addBatch();
		}
		int[] recordsEffect = ps.executeBatch();
		// 提交事務
		connection.commit();
		return recordsEffect;

	} catch (SQLException e) {
		e.printStackTrace();
	}
	return null;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM