如何将Android连接到数据库服务器

发布于 2024-09-12 17:49:35 字数 586 浏览 1 评论 0原文

有样品吗?我有我的 Android 应用程序,我需要连接到我的机器上的 mysql 服务器,最好的方法是什么?

我不应该使用 jdbc,这里有解释

永远不要在任何地方、任何数据库、任何平台、任何客户端通过 Internet 连接使用数据库驱动程序。对于移动设备来说,这一数字翻倍。数据库驱动程序专为 LAN 操作而设计,不适用于不稳定/间歇性连接或高延迟。

并且应该去:

DefaultHttpClient httpclient = new DefaultHttpClient(); 

但是没有关于如何打开连接或执行简单的sql语句的示例。 有人可以帮助我吗?

Is there any sample? I have my android application and I need to connect to mysql server on my machine, what is the best way?

I should not use jdbc, explanation here

Never never use a database driver across an Internet connection, for any database, for any platform, for any client, anywhere. That goes double for mobile. Database drivers are designed for LAN operations and are not designed for flaky/intermittent connections or high latency.

And should go for:

DefaultHttpClient httpclient = new DefaultHttpClient(); 

But there is no example in how to open a connection or execute a simple sql statement.
anyone could help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空城缀染半城烟沙 2024-09-19 17:49:35

您应该使用 Web 服务或实现 HTTP 处理程序并以 RESTful 方式进行传输。

You should either use web services or implement an HTTP handler and transfer in a RESTful manner.

浪漫之都 2024-09-19 17:49:35

为了连接到 MySQL 服务器,您需要一个 MySQL 客户端。 Android 不附带任何 MySQL 库。您也许可以采用通用的 Java MySQL 库并伪造它以与 Android 一起使用,但这将是一项艰巨的任务并且浪费时间。

您指向的链接已经告诉您,您尝试做的事情一开始就是错误的。不要通过互联网连接到数据库!您的服务器上需要有一些东西可以响应 HTTP 请求、在数据库中查找数据并通过 HTTP 将它们发送回。该链接已经提到了一些选项。您甚至可以自己编写一些东西,尽管使用现有的解决方案很可能比尝试使自己的方法安全且防黑客更容易。

In order to connect to a MySQL server, you need a MySQL client. Android does not come with any MySQL libraries. You may be able to take a generic Java MySQL library and fudge it to work with Android, but that would be a big undertaking and wasted time.

The link you pointed to already told you that what you're trying to do is wrong in the first place. Don't connect to a database across the internet! You will need something on your server that responds to HTTP requests, looks up data in the database, and sends them back via HTTP. The link already mentioned a few options. You could even write something yourself, although it's most likely easier to use an existing solution than trying to make your own approach safe and hack-proof.

说谎友 2024-09-19 17:49:35

我是离线优先的粉丝,这意味着您的应用程序应该可以在没有连接的情况下使用。

为了实现这一点,我建议使用 SQLite 本地数据库,并在联机时同步到远程数据库。

您可以使用 SymmetricDSDaffodil Replicator 通过 HTTP(S) 同步本地和远程数据库。

I'm a fan of Offline First, meaning your app should be usable without a connection.

To facilitate this, I would recommend using a SQLite local database, and syncing to a remote database when online.

You can use a tool like SymmetricDS or Daffodil Replicator to sync your local and remote databases over HTTP(S).

走过海棠暮 2024-09-19 17:49:35

我尝试从 Android 应用程序连接 DatabaseServer,最初我在使用 jtds(用于数据库驱动程序支持的 jar 包)时遇到了一些问题,而不是使用 jtds jar 文件,而是使用 mysql-Connector jar 文件来支持数据库驱动程序。
mysql-connector jar 文件“mysql-connector-java-5.1.24-bin.jar”。
将此 jar 文件放入项目的 libs 文件夹中。

一点代码片段:

`String url="jdbc:mysql://(IP-of databaseServer):3306/DBNAME";`
`String driver="org.gjt.mm.mysql.Driver";`
`String username="XYZ";//user must have read-write permission to Database
`String password= "*********";`//user password

`try{
    Class.forName(driver).newInstance();//loading driver
    Connection con = DriverManager.getConnection(url,username,password);
    /*once we get connection we can execute ths SQL command to Remote Database Server with the help mysql-connector driver over Internet*/

}`    
`catch(Exception e){
    e.printStackTrace();
}`

我希望它能起作用。

干杯
拉杰什·P·亚达夫.

I tried to connect DatabaseServer from an Android Application,initially i faced some issue while i was using jtds, jar package for database Driver Support, instead of using jtds jar file use mysql-Connector jar file for Database Driver Support.
mysql-connector jar file "mysql-connector-java-5.1.24-bin.jar".
put this jar file in projects libs folder.

a little bit code snippet :

`String url="jdbc:mysql://(IP-of databaseServer):3306/DBNAME";`
`String driver="org.gjt.mm.mysql.Driver";`
`String username="XYZ";//user must have read-write permission to Database
`String password= "*********";`//user password

`try{
    Class.forName(driver).newInstance();//loading driver
    Connection con = DriverManager.getConnection(url,username,password);
    /*once we get connection we can execute ths SQL command to Remote Database Server with the help mysql-connector driver over Internet*/

}`    
`catch(Exception e){
    e.printStackTrace();
}`

I hope it could work.

Cheers
Rajesh P Yadav.

带刺的爱情 2024-09-19 17:49:35

您将无法使用 HttpClient 直接连接到 MySQL 数据库,因为 MySQL 数据库不按该协议运行。

您链接的问题的答案的第二部分建议使用 Web 服务并使用这些服务与数据库服务器进行通信。这正是您可以使用 HttpClient 执行的操作。

You won't be able to connect directly to a MySQL database with the HttpClient, as the MySQL database doesn't operate on that protocol.

The second part of the answer to the question you linked recommends going with web services and consuming those to communicate with the database server. Which is exactly what you would be able to do with the HttpClient.

十二 2024-09-19 17:49:35

如果您需要在多部手机之间共享数据,我建议将其公开为网络服务。如果数据只需要在特定的手机上访问(并且您想保持相关性),您可以使用 sqlite。

If you need to share data between multiple phones, I would recommend exposing it as a web service. If the data only needs to be accessible on that particular phone (and you want to remain relational) you can use sqlite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文