Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: connection to a database on another computer(server)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default connection to a database on another computer(server)

    i have been trying to establish a connection to a mysql database on another computing having the same java application running.
    I keep getting a connection timeout error from java.net. I seem not to be getting anywhere, someone please help me!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: connection to a database on another computer(server)

    Welcome to Java Programming Forums. Don't forget to read the rules.
    What have you tried yet? Where actually the problem exists? Post your code here so that we could have a look over this.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    I have setup a peer to peer network and tried establishing a connection to the database with the IP address of the computer with the database.

    try{
    Class.forName(DRIVER);
    connection = DriverManager.getConnection("jdbc:mysql://localhost/db_name",root,password);

    }catch(Exception ex){
    JOptionPane.showMessageDialog(null, ex.getMessage(),"Unable to connect",JOptionPane.ERROR_MESSAGE);
    }

    this code for the client

    try{
    Class.forName(DRIVER);
    connection = DriverManager.getConnection("jdbc:mysql://169.254.214.132",client_name,password);

    }catch(Exception ex){
    JOptionPane.showMessageDialog(null, ex.getMessage(),"Unable to connect",JOptionPane.ERROR_MESSAGE);
    }

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: connection to a database on another computer(server)

    Class.forName(DRIVER);
    What's DRIVER? Do you really think, compiler will let you pass from this statement?
    connection = DriverManager.getConnection("jdbc:mysql://localhost/db_name",root,password);
    I will recommend you to provide IP instead of localhost, though it's not compulsory.
    And i would like to know one more thing, which server are you running?

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    i'm running wampserver 2.1 and the driver i'm using is "com.mysql.jdbc.Driver". And please i need your help so i would appreciate it if you stop being cold

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: connection to a database on another computer(server)

    Class.forName(DRIVER);
    Pass Class.forName the actual driver, not the word driver. Is DRIVER a string? If no, then paste your driver in here and see if it can load the driver. If it gets driver loaded, try creating the connection.

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    DRIVER is a string. And i've tried the connection several times and it works perfectly on the host computer the problem is with using the application on another computer and connecting the database from that particular computer to the host computer. I have tried using an IP address as you suggested and i also used the port 3306. i.e 127.0.0.1:3306 on the host machine and 169.254.236.132:3306 on the computer connecting to the database. the error i keep from computer connecting remotely is java.net.ConnectException, Connection timed out:connect.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: connection to a database on another computer(server)

    The reason is your machine can not communicate with the machine you are actually trying to. This machine do exists else there would be an error saying "Can not find host machine" or whatever like this.. So, i will recommend you to see the firewall settings, if firewall is not blocking the way or you can give a try to communicate to another port. If both of these things didn't work, let me know, how far it takes before spitting this error out. Also, in command prompt, try ping machineIP and see if it's sending and receiving data.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    the port is not blocked and it takes about 20 seconds to report the error when i run it from the netbeans IDE but does not take too long to report when i run the executable jar file.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: connection to a database on another computer(server)

    Can you communicate with the database outside of java? For instance, can you communicate directly to the database remotely using the MySQL command line - this should be your first place to start, as it removes many of the variables that can be the problem since it is a direct communication. If it does not work you have narrowed your problem down to a firewall or MySQL remote host setting. If it does work, this at least tells you a remote client can communicate with the database.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    If you are using web server ,try accessing with your computer name instead of localhost or ip . If you are using only app server then start you server using your IP (IP which your trying to connect) .Normally If server is starting in localhost ,You will not be able to use IP to connect and If you start with IP then you will not be able to use Local host .

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: connection to a database on another computer(server)

    What happened when you tried the ping Mr.777 suggested? Are both these computers on your desk? The IP address you posted is a 'Link Local' IP address:

    Link-local address - Wikipedia, the free encyclopedia

    - so it's not routable.

  13. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: connection to a database on another computer(server)

    the firewall had blocked the port MySQL uses in communicating (Port 3306) by default. so i had to allow through the firewall and my problem is solved now. thank you

Similar Threads

  1. [SOLVED] JDBC Connection from Single Computer
    By Max Avion in forum JDBC & Databases
    Replies: 4
    Last Post: November 17th, 2011, 12:41 PM
  2. [SOLVED] Connection to MySql Database
    By dego89 in forum JDBC & Databases
    Replies: 8
    Last Post: May 26th, 2011, 08:56 AM
  3. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM
  4. Replies: 3
    Last Post: March 1st, 2011, 02:18 PM
  5. Database in a standalone computer
    By Abednego Setyawan in forum JDBC & Databases
    Replies: 1
    Last Post: April 7th, 2010, 09:24 PM