views:

272

answers:

3

I am trying to create a JSF application using the Eclipse IDE. I am using a remote mySQL server as my database. How do I connect to this remote database for creating tables and accessing them?

+5  A: 

Just supply the IP / hostname of the remote machine in your database connection string, instead of localhost. For example:

jdbc:mysql://192.168.15.25:3306/yourdatabase

Make sure there is no firewall blocking the access to port 3306

Also, make sure the user you are connecting with is allowed to connect from this particular hostname. For development environments it is safe to do this by 'username'@'%'. Check the user creation manual and the GRANT manual.

Bozho
Also, be sure to GRANT appropriate permissions to that host and user in your MySQL schema.
duffymo
A: 

You need to pass IP/hostname of the rempote machine in the connection string.

import java.sql.*;
import javax.sql.*;

public class Connect
{
   public static void main (String[] args)
   {
       Connection conn = null;

       try
       {

           String url = "jdbc:mysql://localhost:3306/mydb";
           Class.forName ("com.mysql.jdbc.Driver");
           conn = DriverManager.getConnection (url,"root"," ");
           System.out.println ("Database connection established");
       }
       catch (Exception e)
       {
           e.printStackTrace();

       }
       finally
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
                   System.out.println ("Database connection terminated");
               }
               catch (Exception e) { /* ignore close errors */ }
           }
       }
   }
}
giri
Instead of local host i replaced my remote machine address. this is the code: `String url = "jdbc:mysql://172.18.227.237/struts2"; conn = DriverManager.getConnection(url,"root","admin")`;. But Now I get the error, `com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Access denied for user 'root'@'172.20.169.174' to database 'struts2'. I have obtained remote access to the mysql server.
Angeline Aarthi
+1  A: 

Plus, you should make sure the MySQL server's config (/etc/mysql/my.cnf, /etc/default/mysql on Debian) doesn't have "skip-networking" activated and is not binded exclusively to the loopback interface (127.0.0.1) but also to the interface/IP address you want connect to.