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);
			}
		});

 

你可能感兴趣的:(spring)