Remote mysql_java. SQL .SQLException: null, messagefromserver: _ MySQL

Source: Internet
Author: User
Remote mysql_java. SQL .SQLException: null, messagefromserver: amp; quot; Hostamp; #39; xxxamp; #39; isno bitsCN.com

Recently, I am working on a project that uses a remote mysql database.

Let me explain the functions I want to implement:

1/** 2 * is the function: my machine is A. now I will use A jdbc method written by myself.
3 * call the database installed on remote machine B
4 * then I want CRUD to operate the database on Machine B.
5 * or simply, you can think that I have some data on machine.
6 * I want to insert the data into the mysql database of remote machine B.
7 *.
8 */

As follows:

The following is the test class I wrote on machine:

1/** 2*3 */4 package edu.gz ucm. thms. utils; 5 6 import java. SQL. connection; 7 import java. SQL. driverManager; 8 import java. SQL. preparedStatement; 9 import java. SQL. SQLException; 10 import java. util. calendar; 11 12/** 13 * @ author Hongten
14 * @ date 2012-10-18 15*16 */17 public class SendData {18 19 public static int sendDate2Server (String ip, String port, 20 String databaseName, String userName, String passwrod, string cycler, String content) {21 int flag = 0; 22 try {23 // load the MySql driver Class 24 Class. forName ("com. mysql. jdbc. driver "); 25} catch (ClassNotFoundException e) {26 System. out. println ("The driver class cannot be found. failed to load the driver! "); 27 e. printStackTrace (); 28} 29 // connect to the MySql database. the username and password are both root 30 String url = "jdbc: mysql: //" + ip + ": "+ port +"/"+ databaseName; 31 System. out. println (url); 32 String username = "root"; 33 String password = "root"; 34 String SQL = getSQL (Explorer, content); 35 Connection conn = null; 36 PreparedStatement pstmt = null; 37 try {38 conn = DriverManager. getConnection (url, username, password); 39 pstmt = c Onn. prepareStatement (SQL); 40 flag = pstmt.exe cuteUpdate (SQL); 41} catch (SQLException se) {42 System. out. println ("database connection failed! "); 43 se. printStackTrace (); 44} 45 if (pstmt! = Null) {// Close the declaration 46 try {47 pstmt. close (); 48} catch (SQLException e) {49 e. printStackTrace (); 50} 51} 52 if (conn! = Null) {// close the connection object 53 try {54 conn. close (); 55} catch (SQLException e) {56 e. printStackTrace (); 57} 58} 59 return flag; 60} 61 62 public static String getSQL (String handler er, String content) {63/** 64 * below are some constant settings: retrytimes = 0 pri = 1 inpool = 0 sendmode = 1 65 * the format of sendtime and inserttime is: 18:55:20 66 */67 String now = getNow (); 68 return "insert into sms_boxsending (sender, aggreger, conten T, sendtime, inserttime, retrytimes, pri, inpool, sendmode) values ('system', '"+ cycler +"', '"+ content + "', '"+ now +"', '"+ now +"', '0', '1', '0', '1 ')"; 69} 70 71/** 72 * get the current time and format the time 73*74 * @ return format the current time 75 */76 public static String getNow () {77 Calendar calendar = Calendar. getInstance (); 78 int year = calendar. get (Calendar. YEAR); 79 int month = calendar. get (Calendar. MONTH); 80 int day = calendar. get (Calendar. DAY_OF_MONTH); 81 int hour = calendar. get (Calendar. HOUR_OF_DAY); 82 int min = calendar. get (Calendar. MINUTE); 83 int sec = calendar. get (Calendar. SECOND); 84 85 Object monthString = (month <10 )? "0" + month: month; 86 Object dayString = (day <10 )? "0" + day: day; 87 Object hourString = (hour <10 )? "0" + hour: hour; 88 Object minString = (min <10 )? "0" + min: min; 89 Object secString = (sec <10 )? "0" + sec: sec; 90 91 return "" + year + "-" + monthString + "-" + dayString + "" 92 + hourString + ": "+ minString +": "+ secString; 93} 94 95 public static void main (String [] args) {96 // This is the mysql database 97 System that calls remote machine B. out. println (sendDate2Server ("210.38.111.34", "3306", "messagecat", null, null, "1342360 ***", "this is a test text message ")); 98 // This is the local 99 System. out. println (sendDate2Server ("localhost", "3308", "messagecat", null, null, "1342360 ***", "this is a test text message ")); 100} 101}

The result is as follows:

So how should we solve this problem?

I also checked the information to solve this problem ....

In fact, the principle is very simple, that is, remote machine B does not allow machine A to access his database. That is to say, to solve this problem, we need to allow the database of Machine B

Machine A's access is done;

The procedure is also very simple:

1. open the mysql console and enter:

1 use mysql;2 3 show tables;

II. input:

1 select host from user;2 3 update user set host ='%' where user ='root';

3. enter the computer service interface and restart the mysql service ..

4. run the following command:

The return value is 1, indicating that the program runs normally... and wish you good luck ....

There is also a multi-method: http://hi.baidu.com/soutnila/item/5356f2c0002781bf0d0a7bae

BitsCN.com

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.