Connection conn =NULL; String URL= "Jdbc:mysql://xxx.xx.xx.xx:3306/connection"; Try{class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("Load Successfully"); String User= "Root"; String PassWord= "Root"; Conn=drivermanager.getconnection (Url,user,password);System.out.println (conn.tostring ()); Statement stmt=conn.createstatement (); String SQL= "SELECT * from Users"; ResultSet RS=stmt.executequery (SQL); while(Rs.next ()) {System.out.println (rs.getstring (1) + "\ T" + rs.getstring (2)); }
XX in the code is the remote server IP address.
After confirming that my remote server MySQL can be connected remotely, and port 3306 can be accessed normally, it is still not possible to access through Java code.
Inadvertently add:
if (! conn.isclosed ()) { System.out.println ("succeeded!" ); }
Success.
Guess the reason is: because the computer has been using the MySQL, the connection is not delayed, and remote access, may be in the absence of a connection when the following code, resulting in an error.
The conn.isclosed () method should be waiting for feedback, which actually increases the time period to give the connection. ----is a complete conjecture.
Java Remote Access linux server MySQL cannot connect the problem I