我应该在Android中使用Java JDBC进行MySQL连接吗?一般而言,这是如何完成的?

huangapple go评论60阅读模式
英文:

Should I use Java JDBC for Mysql connection in Android ? How is it done generally?

问题

我是新手安卓开发者,正在尝试学习如何将我的应用连接到MySQL数据库。

经过一些研究,我发现在安卓中不推荐使用JDBC进行数据库连接。

另外,我找到了一些替代方法,但它们似乎相当奇怪,它们使用一个PHP文件来处理应用内的连接和查询部分。

现在我感到困惑了,你能否解释一下我应该如何进行操作?

以及一般推荐使用什么方法?

英文:

I am new to android & I am trying to learn how to connect my app to a Mysql database.

With some research I found that its not recommended to use JDBC for Database connection in Android.

Also some alternatives I found seem pretty weird, they were using a PHP file to handle the connection and query part inside the App.

I am confused now, Can you please explain how I should co about doing it ?

and also how its generally recommended ?

答案1

得分: 1

JDBC在Android应用中很少被使用,有很多原因。MySQL原则上可以在应用中使用,也有其他在SO上的帖子解释了使其工作所需的步骤。

但是...

最终,JDBC是数据库连接协议的接口,这些协议很少适用于移动计算中使用的网络类型。在Android应用中执行数据库操作的通常方式是将它们作为网络服务暴露在某个具有与数据库强连接的服务器上。该网络服务会进行低层数据操作,可能使用JDBC,同时以适合应用程序处理的形式接受和呈现数据。有许多Java库可简化充当网络服务客户端的工作。

实际上,这种方法在所有情况下用于对数据库的广域网络访问,不仅限于Android应用。大多数组织不会将数据库连接协议暴露给公共互联网,因为这些协议难以路由,并且很难确保安全性。

简而言之,除非您的数据库服务器可以通过WiFi连接从移动设备访问,否则直接的JDBC连接很可能无法很好地工作,即使可以克服Android特定的问题。尝试解决这些问题几乎没有太大价值,因为即使实际工作,JDBC很可能不会稳健、安全或带宽高效。

英文:

JDBC is rarely used in Android apps, for many reasons. MySQL can, in principle, be made to work in an app, and there are other posts on SO that explain the steps needed to make it work.

But...

In the end, JDBC is an interface to database wire protocols, that are rarely suitable for the kind of networking used in mobile computing. The usual way to do database operations in an Android app is to expose them as webservices on some server that has a strong network connection to the database. The webservice would do the low-level data manipulation, perhaps using JDBC, while accepting and presenting data in a form that is suitable for handling in an app. There are many Java libraries for simplifying the work of acting as a webservice client.

In fact, this approach is almost always used for wide-area access to databases in all contexts, not just Android apps. Most organizations will not expose database wire protocols to the public Internet, because these protocols are hard to route, and it's hard to do it securely.

In short, unless your database server is accessible to your mobile device over a WiFi connection, direct JDBC connection is unlikely to work well, even if the Android-specific problems can be overcome. It's almost not worth trying to overcome these problems, because JDBC is unlikely to be robust, secure, or bandwidth-efficient even if it actually works.

huangapple
  • 本文由 发表于 2020年10月8日 21:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64263319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定