1

I'm trying to compile this small piece of code, to help me connect to my db and retrieve some information to test it. I am using Netbeans on a Windows 7 x64 machine. This is the code:

package passwordprotector;
import java.sql.*;

public class PasswordProtector {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String host = "jdbc:derby://localhost:1527/PasswordProtector DB";
    String dbUsername = "john"; 
    String dbPassword = "arsenal";

    /*try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    }catch(ClassNotFoundException e){
        System.out.println(e);
    }*/

    try{
        Connection con = DriverManager.getConnection(host, dbUsername, dbPassword);
        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM APP.PERSON");

        while (rs.next()) {
            String uName = rs.getString("uname");
            String uPass = rs.getString("upass");
            System.out.println("Username: " + uName + "/n" + "Password: " + uPass);
        }
    }catch(SQLException e){
        System.err.println(e);
    }
}
}

When I compile and run I receive this error:

java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/PasswordProtector DB
BUILD SUCCESSFUL (total time: 0 seconds)

When I right click on my db and select properties I can see it's location, like so:

Database URL: jdbc:derby://localhost:1527/PasswordProtector

I've checked with others who have posted about this and it seems they had an incorrect URL as the issue, but I can't see any other URL which I can use apart from the one posted.

I've tried with and without the ending ' DB' for the String host, neither works.

I've also already read from here and still couldn't figure out why the URl is incorrect:

3
  • 1
    Why did you comment the loading driver? Commented Jan 27, 2013 at 12:07
  • I read that it wasn't needed for Java 6 upwards as stated here (top answer) stackoverflow.com/questions/8111534/… I've tried both with and without. Neither work. Commented Jan 27, 2013 at 12:11
  • This is what I get: run: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver Commented Jan 27, 2013 at 12:31

3 Answers 3

2

Not sure the problem with the database URL connection, but in the usage of the correct driver. If the database is embedded you should use the driver commented in your post and example from my answer, there's also tutorial embedded derby.

if not then use

Class.forName("org.apache.derby.jdbc.ClientDriver");

It's a different driver to connect to the database running standalone. In this case see derby network client documentation how to configure and run derby network client.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm not sure if the driver is embedded or not if I'm being honest. I've tried using your answer and I get: run: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver but I've also used ClientDriver instead of EmbeddedDriver in my code and neither works.
How do you run, I need to know if are you running embedded or not?
Do you start the derby database separately when you run your app or not?
I'm not sure how I would check but I think this answers your question - when opening Netbeans for the first time I must right click the db and select 'Connect...'. This is a new project and I have no actual runnable classes yet.
@JohnVasiliou can i have acces to your system/DB ?
|
2

Make sure derbyrun.jar is in your classpath. It resides in the db/lib directory of your JDK.

1 Comment

I have done this, I have added derbyrun.jar into my classpath. Within Netbeans I right clicked on my project, properties, library, add jar and added the correct file - no luck.
0

Doing a quick search on Maven and Derby, included the following in my pom:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.10.2.0</version>
</dependency>

and everything worked afterwards, so it may be a library reference issue if the previous solution did not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.