#1
  1. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62

    Java.sql.SQLException: No suitable driver


    Hi All,

    I'm going through the Java DB tutorial un Sun's website, and I'm having trouble with the first example script.

    my code is:
    Code:
    import java.sql.*;
    /**
     *
     * @author bkolts
     */
    public class CreateCoffees {
        
        public static void main(String args[]) {
            //jdbc:derby://localhost:1527/COFFEEBREAK
            String url = "jdbc:derby://localhost:1527/COFFEEBREAK";
            Connection con;
            String createString;
            createString = "create table COFFEES " +
                    "(COF_NAME varchar(32), " +
                    "SUP_ID int, " +
                    "PRICE float, " +
                    "SALES int, " +
                    "TOTAL int)";
            Statement stmt;
            
            try {
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
               // Class.forName("org.apache.derby.jdbc.ClientDriver");
                
            } catch(java.lang.ClassNotFoundException e) {
                System.err.print("ClassNotFoundException: ");
                System.err.println(e.getMessage());
            }
            
            try {
                con = DriverManager.getConnection(url,
                        "admin", "admin");
                
                stmt = con.createStatement();
                stmt.executeUpdate(createString);
                
                stmt.close();
                con.close();
                
            } catch(SQLException ex) {
                System.err.println("SQLException: " + ex.toString());
            }
        }
        
    }
    both the emdedded driver and the client driver produce the same resulting exception:

    Code:
    ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    SQLException: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/COFFEEBREAK
    From what I understand, this is a badly formed url issue, but I'm not sure how to fix it. I'm running this on windows xp, using netbeans 5.5 (I've also tried from command line, same result).

    I'm pretty sure I have the classpaths and paths set up correctly for Derby, UI followed the instructions on that.

    I've googled this for 2 days, and I'm at a loss. Any help would be greatly appreciated!

    Cheers
  2. #2
  3. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    You're right about it being a URL problem (good catch!). My bet is that you're using the embedded driver, rather than the network driver. The URL format for the embedded driver is:

    jdbc:derby:databaseName;URLAttributes

    Try that instead, and let us know if it works. More information can be found in the Derby QuickStart Guide.

    Cheers.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  4. #3
  5. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Originally Posted by Yawmark
    You're right about it being a URL problem (good catch!). My bet is that you're using the embedded driver, rather than the network driver. The URL format for the embedded driver is:

    jdbc:derby:databaseName;URLAttributes

    Try that instead, and let us know if it works. More information can be found in the Derby QuickStart Guide.

    Cheers.

    ~
    thanks for pointing me in the right direction Yawmark. From what I read, having the url as jdbc:derby:dbName will work as long as the database is in the same directory as the class. so thats what I did and it worked! Now I'll read up on the url a bit more.
  6. #4
  7. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Ok, I've been googling how to form the url, but I've had no luck.

    As I said, I can get it to work if its in the same directory as the database, but can't seem to form the proper url for the script to look in a different folder structre for the database. Anyone have any insight on this? Its on windows.

    Thanks!
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Are you still getting ClassNotFound?
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    I missed the ClassNotFoundException the first time! You're using a non-embedded URL along with the embedded driver, but the embedded driver isn't visible to the runtime classpath!

    Thanks for pointing that out, bullet!

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  12. #7
  13. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Thanks Bullet and Yawmark,

    I should have stated I changed my code now:

    [code]
    import java.sql.*;

    public class CreateCoffees {

    public static void main(String args[]) {

    String url = "jdbc:derby:COFFEEBREAK";
    Connection con;
    String createString;
    createString = "create table COFFEES " +
    "(COF_NAME varchar(32), " +
    "SUP_ID int, " +
    "PRICE float, " +
    "SALES int, " +
    "TOTAL int)";
    Statement stmt;

    try {
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");


    } catch(java.lang.ClassNotFoundException e) {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    }

    try {
    con = DriverManager.getConnection(url,
    "admin", "admin");

    stmt = con.createStatement();
    stmt.executeUpdate(createString);

    stmt.close();
    con.close();

    } catch(SQLException ex) {
    System.err.println("SQLException: " + ex.toString());
    }
    }
    }
    [\code]

    so, from what I've read, I should have the embedded driver above, and the embedded url format.

    now I have more questions though LOL. where can I find out more infomration on emmbedded vs a client driver. And... if my database is in a seperate directory from my java class, how do I format the url?

    I tried this
    Code:
    String url = "jdbc:derby:c:\\DB\\COFFEEBREAK";
    but I recieve this message when running:
    Code:
    ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    SQLException: java.sql.SQLException: No suitable driver found for jdbc:derby:c:\DB\COFFEEBREAK
    Yawmark, I think you've given me a clue with "the embedded driver isn't visible to the runtime classpath!" but I'm bnot sure what to do about that.

    Sorry for dragging this out, I really appreciate the help!
  14. #8
  15. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    Yawmark, I think you've given me a clue with "the embedded driver isn't visible to the runtime classpath!" but I'm bnot sure what to do about that.
    You'll need to make sure that the JAR file containing that class ("derby.jar", I believe) is in the runtime classpath.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  16. #9
  17. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    I think I have my user environment variables set up correctly, but I'm a rookie there to.

    This is what I have:
    Code:
    CLASSPATH   	%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;.
    DERBY_INSTALL   C:\Apache\db-derby-10.2.2.0-bin
    JAVA_HOME 	C:\Program Files\Java\jdk1.6.0
    PATH 		%PATH%;%JAVA_HOME%\bin
    does this look right?
  18. #10
  19. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    does this look right?
    Looks good to me, but please note that if you're running your application from inside an IDE or other framework, your system classpath won't matter much. That's by design. Applications such as Tomcat or Eclipse won't use the system classpath at runtime and depend on their own classloaders.

    Hope this helps!

    ~

    Comments on this post

    • bkolts agrees : I've almost got it now!!!
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  20. #11
  21. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Originally Posted by Yawmark
    but please note that if you're running your application from inside an IDE or other framework, your system classpath won't matter much. That's by design. Applications such as Tomcat or Eclipse won't use the system classpath at runtime and depend on their own classloaders.
    ~
    I've been using Netbeans. However, I've also been using a text editor on the side, and I compiled and ran the class using the text editor version from the cmd prompt, and everything worked!!

    So, now I will read up on classloaders in Netbeans, and see if I can get Netbeans to do what I want.

    Thanks again!
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5054
    You're welcome. NetBeans will have an option to set runtime options. Check the project properties for starters.

    Good luck!

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  24. #13
  25. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Originally Posted by Yawmark
    You're welcome. NetBeans will have an option to set runtime options. Check the project properties for starters.

    Good luck!

    ~
    Thats where it was! I added the derby.jar file to the libraries in the project, and bingo!

    Thanks again, no I can continue with the tutorial
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0
    Originally Posted by bkolts
    thanks for pointing me in the right direction Yawmark. From what I read, having the url as jdbc:derby:dbName will work as long as the database is in the same directory as the class. so thats what I did and it worked! Now I'll read up on the url a bit more.
    Thank you so much for pointing out that the database in the same directory! I have been grinding on this problem for the last two weeks now, and it's been driving me nuts. Finally got it worked out, and am eternally thankful to you.
  28. #15
  29. strongbad dance now
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bermuda
    Posts
    324
    Rep Power
    62
    Originally Posted by OwlVision
    Thank you so much for pointing out that the database in the same directory! I have been grinding on this problem for the last two weeks now, and it's been driving me nuts. Finally got it worked out, and am eternally thankful to you.
    Glad my 5 year old thread helped you!

IMN logo majestic logo threadwatch logo seochat tools logo