Code example for PreparedStatement

Methods: executeUpdate, getGeneratedKeys, setBoolean, setInt, setString

0
		String sql =
		             "INSERT INTO UM_USER_PERMISSION (UM_PERMISSION_ID, UM_USER_NAME, UM_IS_ALLOWED, UM_TENANT_ID) VALUES(?,?,?,?) "; 
 
		stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
		byte count = 0;
		stmt.setInt(++count, getPermissionId());
		stmt.setString(++count, getUserName());
		stmt.setBoolean(++count, isAuthorized());
		stmt.setInt(++count, getTenantId());
 
		int resCount = stmt.executeUpdate();
		if (resCount == 0) {
			String error = "Insertion faild for the permission";
			log.error(error);
			throw new UserStoreException(error);
		} 
		res = stmt.getGeneratedKeys();
		if (res.next()) {
			setId(res.getInt(1));
		} 
 
	}