1

I am trying to connect to MySQL database using Java using the code:

 try{
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost/bd_Test","root", "root");

    }catch(ClassNotFoundException e) {
        throw new SQLException(e.getMessage());
    }

However, I want this connection to be made ​​without being over localhost. Is there any way for this?

I want the connection isn't localhost, because the objective is to create an installer application and that later can be accessed by other computers.

Thank you all for reply

strange_098
  • 1,261
  • 6
  • 24
  • 44
  • You can, of course, connect to any database you want (and have access to). Just change your DSN. – Carsten Mar 20 '14 at 14:25
  • 3
    What does "without being over localhost" mean? Are you asking how to change `localhost` to a different hostname? – SLaks Mar 20 '14 at 14:26
  • Thanks for reply...Yes. I want to change localhost to a different hostname, because I need to run application in other computers. – strange_098 Mar 20 '14 at 14:27
  • So, umm, just change it?! – Alnitak Mar 20 '14 at 14:27
  • @user3320956 so change "localhost" to the host name of your mysql server.. – Cruncher Mar 20 '14 at 14:27
  • Have you tried just changing "localhost" to "myotherhost"? What happens when you do? If it's an exception, please edit your post with the *full trace* of the exception. – kdgregory Mar 20 '14 at 14:28
  • I have a suspicion that the mysql database is behind a nat. You may need to port forward. – Cruncher Mar 20 '14 at 14:29
  • You need to always specify the host of the DB there. If you don't want to hard code this, have these in a properties file, so that you could change it without touching your code. – Jay Mar 20 '14 at 14:29
  • Or, you can see if [this answer](http://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql) solves your problem (whatever it might be). – kdgregory Mar 20 '14 at 14:29
  • Unfortunately I'm not sure if this is enough to then run the application on other computers, but the only way to find out is by trying. – strange_098 Mar 20 '14 at 14:31
  • @user3320956 You didn't even try?.... – Cruncher Mar 20 '14 at 14:31
  • 1
    There doesn't appear to be a real Question here. – Stephen C Mar 20 '14 at 14:40

2 Answers2

1
try {
    Class.forName("com.mysql.jdbc.Driver");
    return DriverManager.getConnection("jdbc:mysql://TheHostName/bd_Test","root", "root");

} catch(ClassNotFoundException e) {
    throw new SQLException(e.getMessage());
}
Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • TheHostName can be an IP, ou something? – strange_098 Mar 20 '14 at 14:35
  • 1
    @user3320956 The HostName can be an IP, if it's under the same switch it can be a computer name(not actually sure the specifics here or when/how the computer name gets resolved), if it's over the internet it can be a domain name(assuming a registered DNS). There's many things a host name can be – Cruncher Mar 20 '14 at 14:35
1

Let's clean this up a bit.

It sounds like your question is: Can I connect to a database that is on another machine than my own?

The answer is YES.

In your code snippet, simply replace 'localhost' with the IP address of the remote machine that is hosting the MYSQL database. In in interest of first testing this on your own, first replace 'localhost' with the 'loop-back' address of your own computer: 127.0.0.1 Then test your existing code - you'll find that your application will still be able to query your local database because 'localhost' and '127.0.0.1' are equivalent. Then when you're ready, simply replace the 127.0.0.1 IP address with the remote machine's.

Edit: As others have mentioned, the IP address may also instead be a public DNS (like www.google.com) or a machine name (although this is probably not your best option).

TheCatParty
  • 1,449
  • 12
  • 19
  • For a complete answer(as there's very little info in the question) you may want to consider the possibility that the sql database is behind a nat, in which case you'd have to port forward. Also if the machine with the database is assigned its IP through DHCP, this may not be reliable. – Cruncher Mar 20 '14 at 14:41
  • Sure - there are loads of additional factors here, but a general question can only receive an answer that is as specific as the question is itself. – TheCatParty Mar 20 '14 at 14:43
  • 1
    I agree, but you've given a specific answer, to a very unspecific question – Cruncher Mar 20 '14 at 14:43
  • Thank you all for the answers. I'll try all the possibilities that spoke and when you have a conclusion, put the answer here. And sorry if the question was not well formulated. – strange_098 Mar 20 '14 at 15:05