spring jdbc PreparedStatementCallback使用方式

		JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
		// final String sql = "INSERT INTO t_user(user_name) VALUES (?)";
		Integer count = (Integer) jdbcTemplate.execute(new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
				return conn.prepareStatement("select 1 from dual");
			}
		}, new PreparedStatementCallback() {
			public Integer doInPreparedStatement(PreparedStatement pstmt) throws SQLException, DataAccessException {
				pstmt.execute();
				ResultSet rs = pstmt.getResultSet();
				rs.next();
				return rs.getInt(1);
			}
		});
		
		int vehicleCount = (Integer) jdbcTemplate.execute("select count(*) from vehicle",new PreparedStatementCallback() {
			public Integer doInPreparedStatement(PreparedStatement pstmt) throws SQLException, DataAccessException {
				pstmt.execute();
				ResultSet rs = pstmt.getResultSet();
				rs.next();
				return rs.getInt(1);
			}
		});

猜你喜欢

转载自chwshuang.iteye.com/blog/1933380
今日推荐