Results 1 to 18 of 18
  1. #1
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Question JDBC and mySQL database trouble

    So simply put, I can't figure out how on earth to connect my java app running on my local machine to a mySQL database running on a server on the web.

    Java Code:
    import java.sql.*;
    
       public class Connect
       {
           public static void main (String[] args)
           {
               Connection conn = null;
    
               try
               {
                   String userName = "username";
                   String password = "password";
                   String url = "jdbc:mysql://mysql-lotus.x10hosting.com/dbname";
                   [COLOR="Red"]Class.forName ("com.mysql.jdbc.Driver").newInstance ();[/COLOR]
                   conn = DriverManager.getConnection (url, userName, password);
                   System.out.println ("Database connection established");
               }
               catch (Exception e)
               {
                   System.err.println ("Cannot connect to database server");
               }
               finally
               {
                   if (conn != null)
                   {
                       try
                       {
                           conn.close ();
                           System.out.println ("Database connection terminated");
                       }
                       catch (Exception e) { /* ignore close errors */ }
                   }
               }
           }
       }
    That's my code. The line in red is where the try{} block returns the catch. There's obviously something I'm missing. I've spent hours pouring over all the documentation I can find via Google, but all to no avail.

    Any help is extremely appreciated. :)
    Last edited by ichwar; 10-08-2009 at 08:42 PM.
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    First of all, the newInstance call is completely unneccessary. Take it oout.

    Secondly, an "e.printStackTrace()" to that catch block (or, if you're using a logging framework, log the complete excpetion). Self-made error messages like that are counter-productive if they are the only information provided.

    Thirdly, the only error that line will raise is "ClassNotFound". So, you have either spelled something wrong in that String, or maybe got the package path jumbled or misspelled, or you have not included the mysql driver jarfile on your classpath.

  3. #3
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    Thanks masijade for your help. I think you may have put the finger on my problem. How do I add the mysql driver jarfile to my classpath?
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  4. #4
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    Ok, I changed the code to this:
    Java Code:
    import java.sql.*;
    
       public class Connect
       {
           public static void main (String[] args)
           {
               Connection conn = null;
    
               try
               {
                   String userName = "username";
                   String password = "password";
                   String url = "jdbc:mysql://mysql-lotus.x10hosting.com/dbname";
                   Class.forName ("com.mysql.jdbc.Driver");
                   conn = DriverManager.getConnection (url, userName, password);
                   System.out.println ("Database connection established");
               }
               catch (Exception e)
               {
                   System.err.println ("Cannot connect to database server");
    		e.printStackTrace();
               }
               finally
               {
                   if (conn != null)
                   {
                       try
                       {
                           conn.close ();
                           System.out.println ("Database connection terminated");
                       }
                       catch (Exception e) { /* ignore close errors */ }
                   }
               }
           }
       }
    The output is this:
    Java Code:
    Cannot connect to database server
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:169)
    	at Connect.main(Connect.java:14)
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by masijade View Post
    Thirdly, the only error that line will raise is "ClassNotFound". So, you have either spelled something wrong in that String, or maybe got the package path jumbled or misspelled, or you have not included the mysql driver jarfile on your classpath.
    So, what have done about these suggestions?

  6. #6
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    I'm pretty sure now after some more research that the mysql driver jarfile is my problem. Can you give me some help with getting that working? :)
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    If executing from an IDE, go to the project -> properties -> libraries and add the jarfile as a library (which will automatically include that jar reference in the manifest file when a jarfile is built using the IDE).

    When executing a jarfile from the command line using the "-jar" option. Make sure the driver jarfile is contained in the same directory as the jarfile or a directory "below" that directory, and reference the driver jarfile in the manifest file.

    When executing from the command line without the "-jar" option. The command looks like java -cp /full/path/to/driver/jarfile.jar:/full/path/to/your/classes your.pack.and.Main

  8. #8
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    Ok, I ran this code:
    Java Code:
    java -cp mysql-connector-java-5.1.10-bin.jar: Connect
    And the error I got was this:
    Java Code:
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214)
    	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:781)
    	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
    	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
    	at java.sql.DriverManager.getConnection(DriverManager.java:582)
    	at java.sql.DriverManager.getConnection(DriverManager.java:185)
    	at Connect.main(Connect.java:15)
    Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    	at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
    	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137)
    	... 12 more
    Caused by: java.net.ConnectException: Connection timed out
    	at java.net.PlainSocketImpl.socketConnect(Native Method)
    	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    	at java.net.Socket.connect(Socket.java:525)
    	at java.net.Socket.connect(Socket.java:475)
    	at java.net.Socket.<init>(Socket.java:372)
    	at java.net.Socket.<init>(Socket.java:215)
    	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
    	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
    	... 13 more
    :eek:

    Can you make any sense of that? Thanks.
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Sounds like a firewall, maybe. Make sure you can connect to the DB with the arguments used in your program from some other tool (such as the mysql command line client or GUI client).

  10. #10
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    Yep, I can can use the exact same details from php. Same host. Same username. Same password. Same database name. And it works fine. But when I try to do it from this java class, I always get that error.

    Any other ideas?
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    I can't say anything more about it, bcause I don't have access to the machine, or the network, to get all of the details but this
    java.net.ConnectException: Connection timed out
    is, without a doubt, a network related problem.

    Now, whether that is because a different interface is being used, or the requests are coming from a different machine, or whatever, I can't say.

    Did the "php" try come from the same machine as the "java" try? If no, then try it from there, if yes, then does the db also reside on that machine? If yes, then it is entrely possible that php makes the connection using the domain socket, rather than the network socket.

  12. #12
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    No, the java is coming from my machine. The php tries from the sql machine... I tried with a totally different mysql host and got the same result, so there's still something going fishy. But you've helped me get the driver working at least. A big thanks for that! :D

    I'll see if I can find help with this network problem in another section or maybe on another forum...
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  13. #13
    tawakkol is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    I know this is kind of an old topic but ichwar i was wondering how u were able to solve ur problem. Thanks

  14. #14
    ichwar is offline Member
    Join Date
    Jul 2009
    Location
    Rolesville, North Carolina
    Posts
    16
    Rep Power
    0

    Default

    Sorry Tawakkol, I never did get it working. It is always unsuccessful when it gets to the part of trying to set up a connection with the remote database. I've never solved the problem...

    I figure I'll just have to stick with a localhost JavaDB for now until I find a project that absolutely requires a remote SQL database. At that point, I'll ... umm ... I'll worry about it when it happens. :rolleyes:

    But if anyone here can solve my problem before then, I'll owe him many thanks! :)
    if (ichwar == offline) {
    System.out.println("ichwar is busy");}

  15. #15
    tawakkol is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Default

    ichwar, u got me wrong i am trying to connect to a local database and yet i gt the same error u got.. any help?
    thanks a lot

  16. #16
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    I guess that remote machine is some free hosting company server
    where mysql server resides.

    Why don't simply mail them and ask them what to do - it is a network issue,
    and it is their server on their network.

    Maybe because of security there is some non standard way to connect, after all that MySQL server is not there just for you but for many of users that are sharing it.

    cheers!

  17. #17
    zielin is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default Solution

    Tell me which system are You using. In my Slackware was set option skip-networking. I could connect only to localhost using only unix sockets not tcp/ip sockets. Now when i've removed this option i can connect from local and remote machines using tcp/ip network. PHP scripts use unix sockets during connection to localhost (unix sockets are faster).
    I'm telling about mysql configuration.

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. JDBC data retrieval with MySQL
    By hadesflames in forum JDBC
    Replies: 5
    Last Post: 04-23-2009, 04:38 AM
  2. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  3. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  4. Java JDBC/MySQL appropriate Syntax
    By thelinuxguy in forum Advanced Java
    Replies: 7
    Last Post: 02-10-2009, 07:57 PM
  5. help ... JDBC or PHP/MySQL
    By bluebarca in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •