I know a good amount of german history but why didn't a German kingdom or after 1871 the German empire annex Denmark?
There are several reasons for this.
Bismarck was out ot unite the Germans, not annex a lot of non-Germans into the German Empire.
Denmark, while no longer a grand power, still held numerous forts, small colonies and bases all over the world, including Iceland, Greenland, the Danish (later the US) Virgin Islands, Faroe Islands and the Nicobar Islands.
The British, still the premier power in this age, would not want the Germans to gain these colonies, and would most likely support Denmark against Germany.
Denmark also had support from Sweden - while the 1864 war only saw Swedish volunteers in the fight, Sweden would most luley have deployed troops to Denmark to help defend it if Prussia had attempted an annexation.
Russian trade went through the Sound, and German ability to close the Sound would have alarmed Russia.
Germany had gone to war with multiple European powers in a short timeframe 1864-1871, and many of them were itching for a re-match. Britain was also concerned about the potential of the new German Empire to eclipse them. An attempt to annext a sovereign nation such as Denmark could have meant that Germany ended up alone in war with Britain, Sweden, Denmark and Russia, with Austria-Hungary and France as potential enemies wanting revenge for their losses.
It was simply not worth it.
They already did waged war against Denmark before formal unification 1871, well sort of.
Bismarck, during his campaigns, as the Prussian chancellor, in uniting Germany, waged war against Denmark in Schleswig-Holstein war. The root of the war was the fact that Schleswig-Holstein, which has a Germans majority population was under personal unon with Denmark. Bismarck used this as excuse to "liberate" Schleswig-Holstein from Danish rules. And he succeeded in gaining the province, although at first he had to share it with the Austrians as part of loosely-organized German Confederations. In 1860s Bismarck turned against the Austrians, and seized the whole of Schleswig-Holstein for Prussia, and also kicking Austria from German Confederation. The confederation was later turned into a unified Empire following Prussian victory in Franco-Prussian War.
It is important to remember that raison d'etre of Imperial Germany was to unite German people under one nation (with obvious exception the Austrians who did not want to participate as they already have their own empire). Therefore it mostly did not have use for non-Germans area.
Furthermore Imperial Germany managed to build the Kiel Canal, cutting Jutland peninsula and rendering conquest of Denmark unnecessary, especially since such conquest will face heavy oppositions of Britain (King Edward VII married Alexandra of Denmark, daughter of Christian IX of Denmark) and Imperial Russia (Tsar Alexander III married Maria Feodorovna, Alexandra's sister), which both had commercial interest in keeping Denmark independent and Baltic Sea open.
Prussia had invaded Denmark earlier, but only occupied the southern part which had many German speaking inhabitants. This was the last war in which Denmark was a military power, and from this point on they used diplomacy to stay independent.
http://en.wikipedia.org/wiki/Second_Schleswig_War
Denmark had many royal connections. There was a small degree of protection from Britain via Princess Alexandra's marriage to Prince Edward. Prince Edward's older sister Victoria was the Queen of Prussia through marriage to Frederick (father of Kaiser Wilhelm).
To further complicate matters, Princess Alexandra's sister Maria was the mother of Tsar Nicolas II. The Danish royal family had good relations with the British and Russians. Here's Queen Victoria of Britains family tree with all these people in. (The red bits are about a genetic condition in case you were interested)
http://3.bp.blogspot.com/-5G8wDgFU9Ec/TvvGIUNEuHI/AAAAAAAAEhc/inykTqHQ9_k/s1600/ftree.gif
Germany spent the latter half of the 19th century with an uneasy relationship with Britain. The Kaiser had good relations with some royals such as Victoria and Nicolas II, others such as Edward din't like him. Germany saw the British and Russian Empires as rivals, but wanted to keep them sweet in case there was another war with France.
As you will have figured, France also had good relations with Britain and Russia. So Germany was competing with France for their affections. Invading Denmark would have been a diplomatic disaster, and just not worth the bother.
TLDR; Denmark was a small prize that would have upset Germany's relations with other nations.
http://en.wikipedia.org/wiki/Wilhelm_II,_German_Emperor#Foreign_affairs