Code example for PreparedStatement

Methods: executeUpdatesetIntsetNullsetString

0
 
		try { 
			updateStmt = connection.prepareStatement(QUERY_UPDATE);
			if(entity.getName()!=null)
			{ 
				updateStmt.setString(1, entity.getName());
			}else{ 
				updateStmt.setNull(1, Types.VARCHAR);
			} 
 
			if(entity.getType()!=null)
			{ 
				updateStmt.setString(2, entity.getType().toString().toUpperCase());
			}else{ 
				updateStmt.setNull(2, Types.VARCHAR);
			} 
			 
			if(entity.getDescription()!=null)
			{ 
				updateStmt.setString(3, entity.getDescription());
			}else{ 
				updateStmt.setNull(3, Types.VARCHAR);
			}	 
			updateStmt.setInt(4, entity.getId());
			recordsUpdated = updateStmt.executeUpdate();
			// this should affect at most one record 
			if (recordsUpdated == 0){
				throw new NoSuchEntityException(); 
			}else if(recordsUpdated == 1) {
				success = true;
Stop searching for code, let great code find you!  Add Codota to your java IDE