How can I implement remote communication between the server and MySQL?

Source: Internet
Author: User

I would like to access the database directly through the Android program to get data, helpless in the course of testing Android has been shown unable to find the driver. The internet to find a lot of information, still did not find a solution. Therefore determined to build a mediation server. Relay through a mediation server to enable MySQL manipulation of third-party computers and data communication with Android.

The final test was successful.

The steps are as follows:

Service side:

1. First, the MySQL driver is introduced under the Lib directory of the Web-inf folder on the server side:

2. After the introduction, right-click the project and create a new Java file:

3. Write the Code:


Import com.mysql.jdbc.*;
Import com.mysql.jdbc.Connection;
Import Com.mysql.jdbc.ResultSet;
Import com.mysql.jdbc.Statement;

Import java.sql.*;
Import java.util.ArrayList;
Import java.util.List;
public class Conn {

static list List = null;

public static void Main (string[] args) throws sqlexception{
Conn con = new Conn ();
Con.getdata ();
}

public void GetData () throws sqlexception{
Drive Database
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ();
Get information

write the appropriate information here
String dbName = "";
String tableName = "";
String userName = "";
String password = "";



String URL = "JDBC:MYSQL://<IP address >:< port number, set by the other >/" + DbName + "? user="
+username + "&password=" + password;


Connection conn = (Connection) drivermanager.getconnection (URL);
System.out.print ("Database connection succeeded! ");


}

}

The steps here are finished, and then you start configuring the other computer (this test is Linux):

1.

Comment out the bind-address in/ETC/MYSQL/MY.CNF

2. Change the host of the corresponding user in the database to%

This test successfully, after successful running Java program, will print out the database connection success!

Another: If an error occurs, you may also want to set the other reason for the firewall.

This test failed many times the last unexpected success, do not rule out one of the reasons for success is because the other computer built the Java environment, but this is still not very certain. Hope the great God guidance!

How can I implement remote communication between the server and MySQL?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.