Can anyone provide me the Java SE coding to connect to the MySQL database in a Server (not in the localhost)? what should I put instead of "localhost" and port number?
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql :// " + serverName + "/" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
}
}
localhostor your ip address in connection string with the ip address or domain name of remote computer where you want to connect. In your code replacelocalhostas a value ofserverNamewith ip of remote machine. Also username/password you are using should be valid on remote machine's mySqlorg.gjt.mm.mysql.Driverdriver is obsolete, use thecom.mysql.jdbc.Driverdriver from now on. Also, just change yourserverNameto your server IP address. That's it! :-)