Just 20 miles separate Stanford and San Jose State, but the schools had not met in men’s basketball in 12 years.

If Tuesday night’s game is any indication, they should get together more often.

The underdog Spartans gave the Cardinal all they wanted, especially in the paint, before Stanford pulled out a 78-73 victory at Maples Pavilion.

San Jose State (3-7) led throughout the first half until KZ Okpala, who led Stanford with 25 points, hit a three-point shot with two seconds left for a 34-33 lead at the break.

The Cardinal (6-4) had a 70-59 lead in the second, but the Spartans made it a three-point game (76-73) with 21 seconds left on a basket by Michael Steadman. Stanford made a couple of free throws to clinch the win.

Stanford got a huge lift from sophomore forward Oscar da Silva, who scored a career-high 23 points and grabbed 10 rebounds. Daejon Davis, dealing with an illness, had 14 points and eight assists.

“The good news is we’ve made runs in the last two games,” Stanford head coach Jerod Haase said. “The bad news is we’re not playing very well. I need to find a way to push different buttons or better buttons to get the guys to play at a high level.”

Da Silva entered the game having made just 6 of 35 three-point tries this season. He hit his first eight baskets, including six threes, before missing a long one with 9:33 left.

Stanford made 13 of 34 three-point attempts, but San Jose State head coach Jean Prioleau pointed out the Cardinal eventually cooled off considerably.

“We were basically trying to pack the paint to not let them get easy buckets and easy layups,” he said. “They did make a lot of threes, but they missed mostly all their threes late in the game. It actually worked in our favor. The problem was we could not get the rebound off the threes late in the game.”

The Cardinal were without starting guard and second-leading scorer Cormac Ryan, who injured an ankle in practice Monday. Haase said he didn’t know how long Ryan will be sidelined.

The Spartans rode the inside game of 6-foot-10 Steadman (17 points and 11 rebounds) and 6-11 Oumar Barry (15 and 10). Craig LeCesne had 16 points, and Brae Ivey and Noah Baumann had 11 each.

There was a difference in opinion on the Stanford side as to how good the Spartans were.

“I just think we’re so much better than that team,” Okpala said. “We’ve just got to come out and dominate and show that. Sometimes we underestimate our opponents. That’s maybe the reason for the slow start.”

On the other hand, da Silva said, “I think they’re a really good team. Their record doesn’t really show that, but they’re a tough team to play against.”

Haase was not happy with his team’s “lack of communication and anticipation on the defensive end. Were there bright spots? Absolutely. I just can’t find many right now.”

The Cardinal play USF on Saturday afternoon at Memorial Gym. Haase praised the Dons and said, “If we don’t play exceptionally well, it could get ugly.”

Tom FitzGerald is a San Francisco Chronicle staff writer. Email: tfitzgerald@sfchronicle.com Twitter: @tomgfitzgerald