2

I need to do some simple experiments using JDBC in Java, and thought that running up a derby database should be a simple route to that.

I have installed derby, and used the "ij" tool to create a table and get some data into it. I can read that data back using ij too.

However, when I try to connect to this using actual Java code, things go badly. This is one of those situations where I've tried several things, and each fails differently, so I hope you'll all forgive this getting a bit fuzzy edged. But these are the key points of what I've tried/failed so far.

  • This is a maven project, and there is a dependency in place for artifactId "derbyclient" version 10.15.2.0
  • The ij tool connects to my database (which is on the localhost, but not "embedded") successfully using the url jdbc:derby:firstdb
  • The docs that I found (finally!) that talked somewhat about non-embedded mode said the driver should be org.apache.derby.jdbc.ClientDriver, but that class is not found. I found notes in the gihub for the liquibase project that said this is now org.apache.derby.client.ClientAutoloadedDriver. Using that classname, doesn't throw the class not found error, which seems like a step forward.
  • Although the above driver loads, the jdbc url shown above fails with "No suitable driver for jdbc:derby:firstdb"
  • I found other notes that suggested the url should be of the form jdbc:derby://localhost/firstdb however, that says "The connection was refused because the database firstdb was not found". I've tried a couple of variations of naming the db, but they resulted in "no suitable driver" again.
  • At the prompting of g00se in the comments, I went looking for the ClientDriver that I noted above was not found. I discovered this in the main derby distribution in a file derbytools.jar, but not in any of the jars that maven loaded. I tried executing this using a hand-built command line so I could put the derbytools on the classpath. This allowed that to be loaded as a driver, but it connects to an in-JVM database (which works, I can do stuff with this). However, it does not connect to the running network server which is what I want to achieve. When I try to force its hand by using a URL of the form jdbc:derby://localhost:1527/firstdb, it again fails telling me the database was not found.
  • I've also discovered that there seems to be no need to do the "Class.forName" thing anyway, nor to be concerned about having ClientDriver on the classpath. Somehow a driver is loaded and available when I do DriverManager.getConnection. However, with the URLs I've tried, I have utterly failed to make it connect to the network server, it always connects to an in-JVM server (which works, but isn't where I want to connect.)

Can anyone tell me how to get this to work?

halfer
  • 19,824
  • 17
  • 99
  • 186
Toby Eggitt
  • 1,806
  • 19
  • 24
  • Yes, `org.apache.derby.jdbc.ClientDriver`. We need to see some code – g00se Aug 03 '21 at 22:24
  • Well, that throws class not found. On poking around, I see this class in the derbytools.jar file in the derby distribution directory, but what maven dependency will load that? I currently have dependencies for "derby", "derbyclient", and "derbyshared" but there's nothing in the three jarfiles that loads into .m2/repository that has the FQCN we're looking for :( – Toby Eggitt Aug 03 '21 at 22:42
  • Looks like the driver should be loaded using the [DriverManager](https://docs.oracle.com/javadb/10.8.3.0/ref/rrefjdbc32052.html) class – g00se Aug 03 '21 at 22:51
  • I'm loading the driver using Class.forName, and aside from ClientDriver not being in any of the maven artifacts I can find, it does load. But as noted in my edits above, it connects to the in-JVM db, not the network server. Is my URL wrong? – Toby Eggitt Aug 03 '21 at 23:13
  • I think the manual i linked to should tell you. I'm not experienced much in Derby myself. If you've got it in client server mode, check with netstat that it's listening – g00se Aug 03 '21 at 23:23
  • That's the problem, the server is in client server mode, but these drivers are connecting to (and indeed creating) an embedded server. Anyway, thanks for trying. – Toby Eggitt Aug 03 '21 at 23:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235603/discussion-between-g00se-and-toby-eggitt). – g00se Aug 03 '21 at 23:30
  • Does this link help? https://stackoverflow.com/a/57221011/193453 – Bryan Pendleton Aug 04 '21 at 20:47
  • @BryanPendleton it's likely helpful for some who might find themselves looking at this question, thank you. Not in my case though. I've found the real problem, and it was elsewhere. I'm planning to write it up for clarification, but not sure how best to do so. – Toby Eggitt Aug 06 '21 at 15:27
  • 1
    Rather than squeeze in a solution by editing your Question, you should post and accept an Answer to your own Question. Answering your own Question is perfectly acceptable on Stack Overflow. – Basil Bourque Aug 16 '21 at 16:11
  • Hi Toby - a comment has been left for you under the CW answer. – halfer Oct 19 '21 at 20:35

1 Answers1

0

(Posted on behalf of the question author to move the solution to the answer space).

I now understand this, which effectively solves my question:

  • Derby doesn't require you to load any driver explicitly, it uses the services mechanism to handle this (services pre-date JPMS, so yes, it works without modules). Of course, out of date documenation that still describes loading a driver doesn't really help.
  • My actual problem was my failure to understand a couple of things, first, the format of the derby jdbc url, second that the ij tool can create an embedded database.
  • In particular the url I used in ij (jdbc:derby:firstdb noted below) is an embedded url. This meant that trying to connect to it using a network url from the client code was bogus. The client created a new db of its own, but didn't interact with the ij one.

Fingers crossed someone can find time to update the Derby docs, and in particular the getting started guide, to make these critical concepts a bit clearer from the outset.

halfer
  • 19,824
  • 17
  • 99
  • 186