简体   繁体   中英

Why does JDBC code work in Java but return null in Android?

fellow stackers. I'm using android studios. As the title indicates; I've managed to get JDBC to work in java, but not on android. I made a class dedicated for everything database related. Both my Java code and my android code are in the same folder and have the EXACT same code.

A function in my database class returns an ArrayList of results, fetching this list works perfectly fine in java, but in android, it returns null, which results in a NullPointerException .

I've done some research on the matter and I am using the INTERNET persmission as well as calling the database class from an async class.

Any suggestions?

Edit: My database is hosted on a webserver.

Thanks, everyone, for your help - I've discovered the root of the issue. In the try/catch, I returned null instead of logging the error when caught. The issue lies in the driver: "java.sql.SQLException: No suitable driver". I ought to be able to take it from here.

It works in Java because you're most likely connected to a database in your very own machine (I'm guessing you're trying to access localhost).

In Android, it doesn't work that way. Localhost in android would be your smartphone and you probably don't have any database in it.

My suggestion: create some sort of webservice that connects to your database and returns the results to your android application. It acts as a bridge between your DB and your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM