I'm having pain while I have sex, is it because my husband penis is too long? How can I prevent it from hurting during intercourse?

Painful intercourse. Use a position where you control the depth, such as woman on top.
Dyspareunia. Dyspareunia is the medical term for painful intercourse, and there are many causes other than size or position; you should be checked for issues such as vaginal infections, endometriosis, pelvic infections, ovarian cysts, or other, more uncommon conditions.
Here are some... Coitus is like a pair playing duet piano, who need to understand the features of the piano, understand the melody, and adjusting the pace & strength of playing at right time in right way so, at the end, they can extol to clap & celebrate to bravo for what they have accomplished because they can play with excellent coordination. Likewise, understand and act in coitus.