Results 1 to 10 of 10
  1. #1
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Newbie connection blues

    Can someone point me to a simple complete working example of modern MYSQL / Java code ?
    Iíve tried (no kidding) 10 tutorials/demos or so and Iím ashamed to say Iím almost ready to give up. Wasted 2 full days with this !
    I have the Classpath to the MYSQL jar file OK. My code compiles OK
    MYSQL 5.1 on Win XP.
    I have *some* idea of what Iím doing (years of PHP and MYSQL as well as (shudder) years of MS
    SQL Server) I am however just leaning Java and any tips on debugging the issue would be great too.


    Many thanks !

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Newbie connection blues

    I’ve tried (no kidding) 10 tutorials/demos or so and I’m ashamed to say I’m almost ready to give up
    Might be more productive if you post what you've tried in the form of an SSCCE, and post any and all compile time errors, runtime exceptions, or misbehavior. This will give your question a lot more foundation to work from.

  3. #3
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Newbie connection blues

    One example I've tried; code compiles fine but
    I get "Cannot connect to database server"


    import java.sql.*;

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

    try
    {
    String userName = "root";
    String password = "pw";
    String url = "jdbc:mysql://localhost/test";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    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 */ }
    }
    }
    }
    }

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Newbie connection blues

    First, please wrap your code in the code tags - it becomes readable to human eyes as a result. Second:"
    Java Code:
    catch (Exception e)
    {
    System.err.println ("Cannot connect to database server");
    }
    This gives you little information as to what the exception is....add an e.printStackTrace() call and post the full stack trace you get.

  5. #5
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Newbie connection blues

    C:\dev\sockets>j b5

    C:\dev\sockets>javac b5.java

    C:\dev\sockets>java -classpath . b5
    Cannot connect to database server
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at b5.main(b5.java:14)
    (I've played with the classpath , having read various posts suggesting different ideas; most recently putting the file name itself into the path, not just the directory) still no joy)

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Newbie connection blues

    This is one of the most common problems found when trying to use JDBC - search the forums and you will come up with dozens if not hundreds of this very problem. Make sure the JVM can find the driver class by adding it to the classpath as you are (I don't know where your ConnectorJ driver is located, so can't advice what to place in front of the classpath argument).

  7. #7
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Newbie connection blues

    I've tested the MYSQL install; was able to get to it from another PC on my LAN via PHP. so that's not the issue. I"ve been over the classpath syntax....I'll try it from another windows machine and if NG either quit or try on a LINUX box. Any other thoughts, anyone , please feel free...

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Newbie connection blues

    java -classpath . b5
    That there implies that the driver jar file is located in the same directory as your code.
    If it is not (and the error suggests it isn't) then you need to add it to the classpath:
    java -classpath .;<path to jar file, either relative or full> b5

  9. #9
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Newbie connection blues

    There are few places on this machine where that jar file does NOT exist at this point :)
    I tried it both ways, see below.( to be explicit, the jar file is in both my source code directory and in the classpath directory)
    -------------------


    C:\dev\sockets>j b5

    C:\dev\sockets>javac b5.java

    C:\dev\sockets>java -classpath . b5
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at b5.main(b5.java:14)
    C:\dev\sockets>java b5
    Exception in thread "main" java.lang.NoClassDefFoundError: b5
    Caused by: java.lang.ClassNotFoundException: b5
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: b5. Program will exit.

    C:\dev\sockets>

  10. #10
    byterbit is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Newbie connection blues

    Success:
    This batch file works:

    javac %1.java
    java -classpath c:\java\mysql-connector-java-5.1.18-bin.jar;c:\dev\sockets %1

    Note that I have two parts to the classpath:
    1) the FULL path to the jar file including it's name and
    2) a path (after the ;) to the working directory where the source code is.
    Tolls I think you were telling me this ; many thanks ! I was a little deaf.
    Far be it from me, a newbie of the 1st order, to question the Lords of Java , but
    you need an explicit path to the working directory ?
    I suppose there are lots more educational surprises on this road ...

Similar Threads

  1. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  2. newbie
    By neeti in forum Introductions
    Replies: 3
    Last Post: 01-02-2010, 04:09 PM
  3. Replies: 7
    Last Post: 10-20-2009, 10:34 AM
  4. I am newbie
    By Seoplanner in forum Introductions
    Replies: 0
    Last Post: 11-11-2008, 01:22 PM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 08:49 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
  •