Connecting to a Derby database

To connect to a database, you need to perform the following steps:

Start the JVM Load the appropriate driver. Create a connection by providing a valid database connection URL.

When using ij interactively to connect to a database connection information is generally supplied on the full database connection URL. ij automatically loads the appropriate driver based on the syntax of the URL. The following example shows how to connect in this manner by using the Connect command and the embedded driver: D:>java org.apache.derby.tools.ij ij version ij> connect 'jdbc:derby:sample'; ij>If the URL entered contains Network Client information the Connect command loads the Network Client driver:D:>java org.apache.derby.tools.ij ij version ij> connect 'jdbc:derby://localhost:1527/sample'; ij>In these and subsequent examples the databases were created in the derby.system.home directory. For more information on the System Directory see the .

ij provides alternate methods of specifying part or all of a connection URL (e.g. the ij.protocol, ij.database, or ij.connection.connectionName properties). These properties are often used when a script is being used and the path to the database or the driver name is not known until runtime. The properties can also to used to shorten the amount of information that must be provided with the connection URL. The following are some examples of different ways to supply the connection information:

  • Supplying full connection information on the command line

    Specifying one of the following properties along with a valid connection URL on the ij command line starts ij with the connection already active. This is often used when running a SQL script so the database name or path can be specified at runtime.

    • ij.database - opens a connection using the URL provided
    • ij.connection.connectionName - Used to open one or more connections. The property can appear multiple times on the command line with different connectionNames and the same or different URLs.
    This example shows how to create the database myTours and run the script ToursDB_schema.sql by specifying the database URL using the ij.database property.C:\>java -Dij.database=jdbc:derby:myTours;create=true org.apache.derby.tools.ij %DERBY_HOME%\demo\programs\toursdb\ToursDB_schema.sql ij version CONNECTION0* - jdbc:derby:myTours * = current connection ij> -- Licensed to the Apache Software Foundation (ASF) under one or more -- contributor license agreements. See the NOTICE file distributed with ...output removed... ij> CREATE TRIGGER TRIG2 AFTER DELETE ON FLIGHTS FOR EACH STATEMENT MODE DB2SQL INSERT INTO FLIGHTS_HISTORY (STATUS) VALUES ('INSERTED FROM TRIG2'); 0 rows inserted/updated/deleted ij>

  • Defining a Protocol and using a "short form" URL

    A default URL protocol and subprotocol can be specified by setting the property ij.protocol or using the ij Protocol command. This allows a connection to be made by specifying only the database name. This "short form" of the database connection URL defaults the protocol (For more information, see ).

    This example uses the ij Protocol command and a "short form" connection URL:

    D:>java org.apache.derby.tools.ij ij version ij> protocol 'jdbc:derby:'; ij> connect 'sample'; ij>
  • Specifying an alternate Driver

    If you are using the drivers supplied by , you can specify the driver names listed in . However, the drivers are implicitly loaded when a supported protocol is used so specifying them is probably redundant. Specifying a driver is required when ij is used with other JDBC drivers to connect to non- databases. To use drivers supplied by other vendors explicitly specify the driver one of three ways

    • with an ij property ij.Driver
    • using the JVM system property jdbc.drivers
    • using the ij Driver command
    This example specifies the driver using the ij Driver commandD:>java org.apache.derby.tools.ij ij version ij> driver 'sun.jdbc.odbc.JdbcOdbcDriver'; ij> connect 'jdbc:odbc:myOdbcDataSource'; ij>