Mary is linked directly to David:
Mat 1:15 And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob;
Mat 1:16 And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
Luk 3:23 And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the son of Heli,
Joseph's father was Jacob, not Heli. This means Luke is actually giving the genealogy of Mary not Joseph. Being the son of Joseph there means a son through marriage. "as was supposed" means he entered the family through marriage as a step-son calling Mary's father his own father. Heli is Mary's father.
Take note that Matthew uses begat for the birth of Joseph but Luke uses "as was supposed" when calling him a son of Heli. That's not a birth father. Jacob begat Joseph. That is his birth father.
Barnes:
As was supposed - As was commonly thought, or perhaps being legally reckoned as his son.
Clarke:
As the Hebrews never permitted women to enter into their genealogical tables, whenever a family happened to end with a daughter, instead of naming her in the genealogy, they inserted her husband, as the son of him who was, in reality, but his father-in-law.
Gill:
Which was the son of Eli; meaning, not that Joseph was the son of Eli; for he was the son of Jacob, according to Mat_1:16, but Jesus was the son of Eli; and which must be understood, and carried through the whole genealogy, as thus; Jesus the son of Matthat, Jesus the son of Levi, Jesus the son of Melchi, &c. till you come to Jesus the son of Adam, and Jesus the Son of God; though it is true indeed that Joseph was the son of Eli, having married his daughter; Mary was the daughter of Eli: and so the Jews speak of one Mary, the daughter of Eli, by whom they seem to design the mother of our Lord:
Luk 3:23 And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the son of Heli,
Luk 3:24 Which was the son of Matthat, which was the son of Levi, which was the son of Melchi, which was the son of Janna, which was the son of Joseph,
Luk 3:25 Which was the son of Mattathias, which was the son of Amos, which was the son of Naum, which was the son of Esli, which was the son of Nagge,
Luk 3:26 Which was the son of Maath, which was the son of Mattathias, which was the son of Semei, which was the son of Joseph, which was the son of Juda,
Luk 3:27 Which was the son of Joanna, which was the son of Rhesa, which was the son of Zorobabel, which was the son of Salathiel, which was the son of Neri,
Luk 3:28 Which was the son of Melchi, which was the son of Addi, which was the son of Cosam, which was the son of Elmodam, which was the son of Er,
Luk 3:29 Which was the son of Jose, which was the son of Eliezer, which was the son of Jorim, which was the son of Matthat, which was the son of Levi,
Luk 3:30 Which was the son of Simeon, which was the son of Juda, which was the son of Joseph, which was the son of Jonan, which was the son of Eliakim,
Luk 3:31 Which was the son of Melea, which was the son of Menan, which was the son of Mattatha, which was the son of Nathan, which was the son of David,
Luk 3:32 Which was the son of Jesse, which was the son of Obed, which was the son of Booz, which was the son of Salmon, which was the son of Naasson,
Luk 3:33 Which was the son of Aminadab, which was the son of Aram, which was the son of Esrom, which was the son of Phares, which was the son of Juda,
Luk 3:34 Which was the son of Jacob, which was the son of Isaac, which was the son of Abraham, which was the son of Thara, which was the son of Nachor,
Luk 3:35 Which was the son of Saruch, which was the son of Ragau, which was the son of Phalec, which was the son of Heber, which was the son of Sala,
Luk 3:36 Which was the son of Cainan, which was the son of Arphaxad, which was the son of Sem, which was the son of Noe, which was the son of Lamech,
Luk 3:37 Which was the son of Mathusala, which was the son of Enoch, which was the son of Jared, which was the son of Maleleel, which was the son of Cainan,
Luk 3:38 Which was the son of Enos, which was the son of Seth, which was the son of Adam, which was the son of God.
Mary is related to David so that is a link through his mother. He also has a link through his Father, God:
Luk 1:26 And in the sixth month the angel Gabriel was sent from God unto a city of Galilee, named Nazareth,
Luk 1:27 To a virgin espoused to a man whose name was Joseph, of the house of David; and the virgin's name was Mary.
Luk 1:28 And the angel came in unto her, and said, Hail, thou that art highly favoured, the Lord is with thee: blessed art thou among women.
Luk 1:29 And when she saw him, she was troubled at his saying, and cast in her mind what manner of salutation this should be.
Luk 1:30 And the angel said unto her, Fear not, Mary: for thou hast found favour with God.
Luk 1:31 And, behold, thou shalt conceive in thy womb, and bring forth a son, and shalt call his name JESUS.
Luk 1:32 He shall be great, and shall be called the Son of the Highest: and the Lord God shall give unto him the throne of his father David:
Luk 1:33 And he shall reign over the house of Jacob for ever; and of his kingdom there shall be no end.
David being Jesus' father here just means an ancestor, like great great great great grandfather or something similar. The DNA God sent via the Holy Spirit was what directly tied Jesus to David through his Father.
How Jesus is related to David through his mother:
Jesus, Joseph (Mary), Heli, Matthat, Levi, Melchi, Janna, Joseph, Mattathias, Amos, Naum, Esli, Nagge, Maath, Mattathias, Semei, Joseph, Juda, Joanna, Rhesa, Zorobabel, Salathiel, Neri, Melchi, Addi, Cosam, Elmodam, Er, Jose Eliezer, Jorim, Matthat, Levi, Simeon, Juda, Joseph, Jonan, Eliakim, Melea, Menan, Mattatha, Nathan, David, Jesse, Obed, Booz, Salmon, Naason, Aminadab, Aram, Esrom, Phares, Juda, Jacob, Isaac, Abraham, Thara, Nachor, Saruch, Ragau, Phalec, Heber, Sala, Cainan, Arphaxad, Sem, Noe, Lamech, Mathusala, Enoch, Jared, Maleleel, Cainan, Enos, Seth, Adam.
Look at that list which includes David, Juda (Judah), Jacob, Abraham, Noe (Noah), Mathusala, Enoch, Seth and Adam. That's an impressive genealogy! No wonder God chose Mary to birth his Son!
Luk 3:23 And Jesus himself began to be about thirty years of age, being (as was supposed) the son of Joseph, which was the son of Heli,
Luk 3:24 Which was the son of Matthat, which was the son of Levi, which was the son of Melchi, which was the son of Janna, which was the son of Joseph,
Luk 3:25 Which was the son of Mattathias, which was the son of Amos, which was the son of Naum, which was the son of Esli, which was the son of Nagge,
Luk 3:26 Which was the son of Maath, which was the son of Mattathias, which was the son of Semei, which was the son of Joseph, which was the son of Juda,
Luk 3:27 Which was the son of Joanna, which was the son of Rhesa, which was the son of Zorobabel, which was the son of Salathiel, which was the son of Neri,
Luk 3:28 Which was the son of Melchi, which was the son of Addi, which was the son of Cosam, which was the son of Elmodam, which was the son of Er,
Luk 3:29 Which was the son of Jose, which was the son of Eliezer, which was the son of Jorim, which was the son of Matthat, which was the son of Levi,
Luk 3:30 Which was the son of Simeon, which was the son of Juda, which was the son of Joseph, which was the son of Jonan, which was the son of Eliakim,
Luk 3:31 Which was the son of Melea, which was the son of Menan, which was the son of Mattatha, which was the son of Nathan, which was the son of David,
Luk 3:32 Which was the son of Jesse, which was the son of Obed, which was the son of Booz, which was the son of Salmon, which was the son of Naasson,
Luk 3:33 Which was the son of Aminadab, which was the son of Aram, which was the son of Esrom, which was the son of Phares, which was the son of Juda,
Luk 3:34 Which was the son of Jacob, which was the son of Isaac, which was the son of Abraham, which was the son of Thara, which was the son of Nachor,
Luk 3:35 Which was the son of Saruch, which was the son of Ragau, which was the son of Phalec, which was the son of Heber, which was the son of Sala,
Luk 3:36 Which was the son of Cainan, which was the son of Arphaxad, which was the son of Sem, which was the son of Noe, which was the son of Lamech,
Luk 3:37 Which was the son of Mathusala, which was the son of Enoch, which was the son of Jared, which was the son of Maleleel, which was the son of Cainan,
Luk 3:38 Which was the son of Enos, which was the son of Seth, which was the son of Adam, which was the son of God.