# Connecting to a Derby database

To connect to a Derby database, you must perform the following steps.

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

When you use `ij` interactively to connect to a Derby database, you generally supply connection information 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 way by using the `Connect` command and the embedded driver:

    java org.apache.derby.tools.ij
    ij version 10.17
    ij> connect 'jdbc:derby:sample';
    ij>

If the URL entered contains Network Client information, the `Connect` command loads the Network Client driver:

    java org.apache.derby.tools.ij
    ij version 10.17
    ij> connect 'jdbc:derby://localhost:1527/sample';
    ij>

Note: In these and subsequent examples, the databases are created in the derby.system.home directory. For more information on the System Directory, see the Derby Developer's Guide.

`ij` provides alternate methods of specifying part or all of a connection URL: the `ij.protocol`, `ij.database`, and `ij.connection.connectionName` properties. These properties are often used when a script is being used and the database path name 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 mechanism is often used when running a SQL script, so that the database path name 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.

      java -Dij.database=jdbc:derby:myTours;create=true \
          org.apache.derby.tools.ij \
          %DERBY_HOME%\demo\programs\toursdb\ToursDB_schema.sql
      ij version 10.17
      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

  You can specify a default URL protocol and subprotocol by setting the property `ij.protocol` or using the `Protocol` command. This allows you to make a connection by specifying only the database name. This "short form" of the database connection URL defaults the protocol (For more information, see About Protocols).

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

      java org.apache.derby.tools.ij
      ij version 10.17
      ij> protocol 'jdbc:derby:';
      ij> connect 'sample';
      ij>

- Specifying an alternate driver

  If you are using the drivers supplied by Derby, you can specify the driver names listed in JDBC drivers overview. However, the Derby drivers are implicitly loaded when a supported protocol is used, so specifying them is usually redundant. You may find it useful to specify the driver if you need to reload it after you have shut down the Derby engine and deregistered the driver.

  To load a driver explicitly, use the `ij` command `Driver` or specify the JVM system property `jdbc.drivers`.

- The ij Driver name and connection URL
  The following table summarizes the different ways to specify the driver name and database connection URL.

Parent topic: Getting started with ij