Hisham Ramadan
Hisham Ramadan

Reputation: 323

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

Upvotes: 2

Views: 14157

Answers (3)

MemLeak
MemLeak

Reputation: 4640

  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

Upvotes: 1

blackpanther
blackpanther

Reputation: 11486

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).

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.

Upvotes: 1

ssedano
ssedano

Reputation: 8432

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

Upvotes: 6

Related Questions