Recently working on a project that uses the remote MySQL database.
I want to say what I want to accomplish:
1/**2 * is such a feature: My machine is a, I'm going to use a JDBC method I wrote myself <br>3 * Call the database installed on remote machine B <br>4 * And then took me to the CRUD operation database on machine B <br>5 * or simply, I think I have some data on machine a now <br>6 * I'm going to plug this data into the MySQL database of the remote machine B. <br>7 * This is the operation <br>8 */
As follows:
Here is the test Class I wrote on Machine A:
1/** 2 * 3 */4 package edu.gzucm.thms.utils; 5 6 Import Java.sql.Connection; 7 Import Java.sql.DriverManager; 8 Import java.sql.PreparedStatement; 9 Import java.sql.SQLException; Ten import Java.util.Calendar; /** * @author hongten</br> * @date 2012-10-18 * * * * * * * public class SendData { Lic static int senddate2server (string IP, string port, String databaseName, String userName, String Passwro D,string receiver,string content) {int flag = 0; try {23//load MySQL driver class 24 Class.forName ("Com.mysql.jdbc.Driver"); (ClassNotFoundException e) {System.out.println ("Driver class not found, load driver failed! "); E.printstacktrace (); 28} 29//Connect MySQL database, username and password are root String url = "jdbc:mysql://" +ip+ ":" +port+ "/" +databasename; System.out.println (URL); String username = "root"; Password String = "root"; String sql = getsql (receiver, content); Connection conn = null; PreparedStatement pstmt = null; Notoginseng try {conn = drivermanager.getconnection (URL, username, password); pstmt = CONN.P Reparestatement (SQL); The flag = pstmt.executeupdate (SQL); (SQLException se) {System.out.println ("Database connection failed! "); Se.printstacktrace (); (pstmt! = null) {//close declaration, try {pstmt.close (); 48 } catch (SQLException e) {e.printstacktrace (); \ n} (conn! =) ULL) {//Close the Connection object at try {conn.close (); (SQLException e) {56 E.printstacktrace (); + + +} (+)-return flag; GetSQL, public static string (string receiver,string content) {63/** 64 * Here are some constant settings: retrytimes=0 pri=1 inpool=0 sendmode=1, Sendtime and inserttime format: 2011-05-03 18:55:20 66 * * * the String now = Getnow (); "INSERT into sms_boxsending (Sender,receiver,content,sendtime,inserttime,retrytimes,pri,inpool,sendmode) v Alues (' System ', ' +receiver+ ' ', ' "+content+" ', ' "+now+" ', ' "+now+" ', ' 0 ', ' 1 ', ' 0 ', ' 1 ') "; 69} 70 71/** 72 * Get the current time and format the time * * * * * @return Format the current time * */public static String Getnow () {year Calendar calendar = Calendar.getinstance (); int. int year = Calendar.get (calendar.year) ; + int month = Calendar.get (calendar.month); int day = Calendar.get (calendar.day_of_month); Bayi int hour = Calendar.get (Calendar.hour_of_day); calendar.get int min = n/A (calendar.minute); The Calendar.get int sec = n (calendar.second); Monthstring Object = (Month < 10)? "0" + month:month; Daystring Object = (dAy < 10)? "0" + day:day; Hourstring Object = (Hour < 10)? "0" + hour:hour; Minstring Object = (min < 10)? "0" + min:min; Secstring Object = (sec < 10)? "0" + sec:sec; "+ year +"-"+ monthstring +"-"+ daystring +" "+ hourstring +": "+ minstr ing + ":" + secstring; 94 (string[] args) {96//This is the MySQL database that calls remote machine B SYSTEM.OUT.PR Intln (Senddate2server ("210.38.111.34", "3306", "Messagecat", NULL, NULL, "1342360****", "This is a Test text message")); 98//This is the test local System.out.println (Senddate2server ("localhost", "3308", "Messagecat", NULL, NULL, "1342360* "," This is a Test SMS ")); 100}101}
The result is this:
So how should we solve this problem?
I also checked the information, before the problem to fix ....
In fact, the reason is very simple, that is, the remote machine B does not allow machine A to access his database. In other words, we have to solve this problem by allowing Machine B's database to allow
Machine A access, it is done;
The procedure is also very simple:
One: Open the MySQL console and enter:
1 Use mysql;2 3 show tables;
Two: Input:
1 Select host from user;2 3 update user set host = '% ' where user = ' root ';
Three: Access to the computer's service interface, restart the MySQL service is done.
Four: The following is run:
The return value is 1, indicating that the program is working properly .... Also wish you good luck ....
There are many ways: http://hi.baidu.com/soutnila/item/5356f2c0002781bf0d0a7bae
Excerpt from: http://www.cnblogs.com/hongten/archive/2012/10/25/mysql.html
Remote Mysql_java.sql.SQLException:null, message from server: "Host ' xxx ' isn't allowed to connect