0

I am trying to manipulate an Apache Derby DB on NetBeans and I'm having a tough time connecting.

It seems very simple but it just wouldn't connect.

Please help. Thanks in advance!

import java.sql.*;

public class JDBCtutorial {
    private static String tableName = "Diseases";

    private static Connection conn = null;
    private static Statement stmt = null;

    public static void createConnection() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch(ClassNotFoundException cnfe) {
            System.out.println(cnfe);
        }
        try {
            conn = DriverManager.getConnection("jdbc:derby://localhost:1527/DBName", "user", "password");
        } catch (Exception e) {
            System.out.println("Cannot connect. . .");
        }
    }

    public static void main(String[] args) {
        createConnection();
    }
}
danksim
  • 617
  • 3
  • 9
  • 27

2 Answers2

1

You're using the driver for embedded use of Derby (org.apache.derby.jdbc.EmbeddedDriver), yet you try to connect over the network, in which case you should use the network driver, org.apache.derby.jdbc.ClientDriver.

All this is explained in detail in the Derby doc which is quite good.

Also, as probably getConnection is throwing an exception that might give some hints about the cause of the problem, try pinting the stacktrace, it should provide that info:

    } catch (Exception e) {
        System.out.println("Cannot connect:");
        e.printStackTrace();
    }
fvu
  • 32,488
  • 6
  • 61
  • 79
  • fvu, thank you for your input. Actually, I have tried the ClientDriver first but had no luck. I must be doing something wrong here but can't seem to figure out what. – danksim Mar 26 '13 at 00:27
  • @Dinky it might be a good idea to start with [the Netbeans & Derby tutorial](http://netbeans.org/kb/docs/ide/java-db.html). It'll show you the steps needed and it should make it easier for you to spot at what point things went wrong in your program. – fvu Mar 26 '13 at 00:30
  • That is exactly how I created my db but I believe it never mentions how to actually connect using JDBC. – danksim Mar 26 '13 at 00:36
  • @Dinky, indeed, I was too quick, sorry for that. There used to be a tutorial that showed how to make a basic app in Java that connects to Derby, but apparently it's gone. – fvu Mar 26 '13 at 00:54
  • no problem. I know that you are just trying to help. And I appreciate that. I just used the stacktrade and it tells me "SQLException: No suitable driver found for jdbc:derby://localhost:1527/DBName". Do I need to do some sort of driver configuration before all this...? – danksim Mar 26 '13 at 02:32
  • That means that it could not locate the driver class, you have to add the correct jar tot your project's libraries. It's explained [here](http://stackoverflow.com/questions/3816015/sqlexception-no-suitable-driver-found-for-jdbcderby-localhost1527) – fvu Mar 26 '13 at 08:17
0

You Just need to add one library file in your project. Download here

Eclipse : Right Click on project then > Build Path > Configure Build Path > Add External JAR (and select the file you downloaded) > Done

NetBeans : Right Click on project then > Properties > Libraries > ADD JAR/Folder "and select the file you downloaded " > ok (Run)

Add Permanently : Add the file in c:/program files/java/JRE/lib/ folder

Ronak Bokaria
  • 616
  • 6
  • 6