
How to Connect to remote mysql server using jdbc.

First configure your MySQL server to accept remote connections by enabling external connection for MySQL server. Use the IP / hostname of the remote machine in your database connection string, instead of localhost.
Example :
package roseindia.net;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class RemoteMYSQLServer {
public static void main(String[] args) throws SQLException {
Connection con = null
Statement stmt = null;
ResultSet rs = null;
String conUrl = "jdbc:mysql:// 192.168.10.25:3306/";
String driverName = "com.mysql.jdbc.Driver";
String databaseName = "student";
String usrName = "root";
String usrPass = "root";
try {
// Loading Driver
Class.forName(driverName). newInstance();
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
try {
// Getting Connection
con = DriverManager.getConnection(conUrl + databaseName, usrName,
usrPass);
stmt = con.createStatement();
String query = "SELECT * FROM student'";
rs= stmt.executeUpdate(query);
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
// Closing Connection
con.close();
stmt.close();
}
}
}