如何将 Java 桌面应用程序连接到在线 mysql 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19362693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:30:19  来源:igfitidea点击:

How to connect Java desktop application to an online mysql database?

javamysql

提问by Hisham Ramadan

As titled, I want to make my desktop java application to connect to an online mysql db ?! How can I reach it such connection to be able to add and retrieve data?!

如题,我想让我的桌面 Java 应用程序连接到在线 mysql 数据库?!我怎样才能达到这样的连接才能添加和检索数据?!

采纳答案by ssedano

Download the connector from here. Add it to your classpath and in the code:

这里下载连接器。将它添加到您的类路径和代码中:

// This will load the MySQL driver, each DB has its own driver
  Class.forName("com.mysql.jdbc.Driver");
  // Setup the connection with the DB
  connect = DriverManager
      .getConnection("jdbc:mysql://remoteUri/database-name?"
          + "user=user&password=userpw");

回答by MemLeak

  1. You could directly connect and let the queries run over the network (via JDBC)example: vogella.com
  2. You build an RESTful Service, there are lots of tutorials as Example;
  1. 您可以直接连接并让查询通过网络(通过 JDBC)运行,例如:vogella.com
  2. 你构建了一个 RESTful 服务,有很多教程作为例子;

RESTful Web Services API using Java and MySQL

使用 Java 和 MySQL 的 RESTful Web 服务 API

回答by blackpanther

By all means, a JDBC connection is the way to go. But, you must make sure that the MySQL users have access to the database from another IP Address (i.e. your desktop application).

无论如何,JDBC 连接是要走的路。但是,您必须确保 MySQL 用户可以从另一个 IP 地址(即您的桌面应用程序)访问数据库。

MySQL users are set up so that they can access the database on the local machine. Therefore, you have to allow access to the MySQL database from any host. Please see the following articlewhich shows how to do that.

MySQL 用户设置为可以访问本地计算机上的数据库。因此,您必须允许从任何主机访问 MySQL 数据库。请参阅以下文章,其中显示了如何做到这一点。