简体   繁体   中英

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

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?!

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");
  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;

RESTful Web Services API using Java and MySQL

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 (ie your desktop application).

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 article which shows how to do that.

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