This discussion is archived
5 Replies Latest reply: Mar 21, 2010 10:48 PM by 807580 RSS

connect to a database

807580 Newbie
Currently Being Moderated
hello,
I am trying to connect to a database.
Netbeans says that the url to the database is (Database URL column in properties):
jdbc:derby://localhost:1527/Queue
but if I try this:
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/Queue","root","password"); //this are the credentials

I get the result:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/Queue

what is wrong here?
  • 1. Re: connect to a database
    807580 Newbie
    Currently Being Moderated
    what type of database is it ?
    if it is derby db, did you import the jars needed to communicate with the db ?

    for connecting to any db, you need connection classes, thats the error reason, missing the connection driver (classes)

    Edited by: progruma on Mar 21, 2010 5:10 AM

    Edited by: progruma on Mar 21, 2010 5:11 AM
  • 2. Re: connect to a database
    807580 Newbie
    Currently Being Moderated
    yes, it's derby.
    here is the code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class Main {
    
        public static void main(String[] args) throws SQLException, ClassNotFoundException {
            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/Queue","root","password");
        }
    
    }
    it results in:
    Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/Queue
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Main.main(Main.java:9)
    Java Result: 1
  • 3. Re: connect to a database
    DrClap Expert
    Currently Being Moderated
    Right. So presumably Derby comes with a jar file which contains the JDBC driver, and you didn't include that jar file in your classpath.
  • 4. Re: connect to a database
    807580 Newbie
    Currently Being Moderated
    Make sure the derby library is in your class path and put Class.forName("org.apache.derby.jdbc.ClientDriver"); (Or embedded driver if you want) before you call DriverManager.getConnection(). The driver needs to register itself with the driver manager which you can do by calling Class.forName()
  • 5. Re: connect to a database
    807580 Newbie
    Currently Being Moderated
    yeah, add Embedded jar file of the derby driver in the class path for console applicatios,
    otherwise the application will unable to access the classes needed to access the database from the application