Reputation: 1813
Essential parts of the Code:
(Taken from example from Book: Java How to Program 10th ed, By: Paul Deitel, Harvey Deitel (p. 1063)):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DisplayAuthors
{
public static void main(String args[])
{
final String DATABASE_URL = "jdbc:derby:books";
try (
Connection connection =
DriverManager.getConnection(DATABASE_URL, "user", "pass");
)
{
// ...
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
}
I'm on Ubuntu 16.04
Although Java website for Java DB says the db is included in JDK, I have learned after few hours of web searches that openJDK java, the recommended install on Ubuntu, does not come with the java db.
The website that had that helpful information suggested to run:
sudo apt-get install sun-javadb-client sun-javadb-core
Which produced error and did not install.
So I downloaded jdk-8u111-linux-x64.tar.gz, extracted as instructed, and just to be safe(I believe, I could have just set JAVA_HOME, DERBY_HOME to extracted location, and which did "half-worked" upto ij parts[see below]), removed everything from my /usr/lib/jvm/java-8-openjdk-amd64/ and replaced with the content of what I downloaded.
And added these in my .bashrc:
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
DERBY_HOME=/usr/lib/jvm/java-8-openjdk-amd64/db
export DERBY_HOME
Entered in terminal to create database and insert data as instructed in book:
$JAVA_HOME/db/bin/ij
connect 'jdbc:derby:books;create=true;user=usr;password=pass';
run 'book-basic-table-create-and-insertions.sql';
exit;
It produces a file books with db info+/data inside it.
At ./ there's also a file named derby.log that contains these informations:
----------------------------------------------------------------
Wed Dec 14 19:01:58 EST 2016:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.11.1.2 - (1629631): instance a816c00e-0158-ffc9-1471-000006d047c8
on database directory /tmp/deleteme-IuI/books with class loader sun.misc.Launcher$AppClassLoader@6f94fa3e
Loaded from file:/usr/lib/jvm/java-8-openjdk-amd64/db/lib/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_111-b14
user.dir=/tmp/deleteme-IuI
os.name=Linux
os.arch=amd64
os.version=4.4.0-53-generic
derby.system.home=null
Database Class Loader started - derby.database.classpath=''
----------------------------------------------------------------
Wed Dec 14 19:04:25 EST 2016: Shutting down Derby engine
----------------------------------------------------------------
Wed Dec 14 19:04:27 EST 2016:
Shutting down instance a816c00e-0158-ffc9-1471-000006d047c8 on database directory /tmp/deleteme-IuI/books with class loader sun.misc.Launcher$AppClassLoader@6f94fa3e
----------------------------------------------------------------
Compiled and ran:
javac DisplayAuthors.java # compiles without error
java DisplayAuthors
Program outputs:
java.sql.SQLException: No suitable driver found for jdbc:derby:books
at java.sql.DriverManager.getConnection(DriverManagerager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.javaava:247)
at DisplayAuthors.main(DisplayAuthors.java:15)
How do I get the program to work?
Upvotes: 0
Views: 5036
Reputation: 44854
You are missing the load driver section of code
e.g.
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
...
try {
Class.forName(driver);
} catch(java.lang.ClassNotFoundException e) {
...
}
see http://docs.oracle.com/javadb/10.8.3.0/getstart/rwwdactivity3.html for an example
Edit
As a proof I have done the following
My Code
public static void main(String[] args) {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
try {
Class.forName(driver);
} catch(java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
final String DATABASE_URL = "jdbc:derby:myDB;create=true;user=user;password=pass";
try (
Connection connection = DriverManager.getConnection(DATABASE_URL, "user", "pass");
)
{
// ...
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
This runs without Exceptions
Upvotes: 1