# DB2 Driver for JDBC examples

The examples show how to specify the server name, database, and the URL attributes. You must specify the user name and password attributes when you use the IBM® DB2® Driver for JDBC.

#### Example 1

The following example connects to the default server name localhost on the default port, 1527, and to the database sample. It specifies the URL attributes user, password, and retrieveMessagesFromServerOnGetMessage.

    jdbc:derby:net://localhost:1527/sample:user=judy;password=no12see;
    retrieveMessagesFromServerOnGetMessage=true;

#### Example 2

The following example specifies the attributes for both Derby and the DB2 Driver for JDBC:

    jdbc:derby:net://localhost:1527/sample;create=true:user=judy;
    password=no12see;retrieveMessagesFromServerOnGetMessage=true;

#### Example 3

This example connects to the default server name localhost on the default port, 1527, and includes the path in the database name portion of the URL. The database name must be delimited by double quotation marks and you cannot specify Derby attributes on the URL.

    jdbc:derby:net://localhost:1527/"c:/my-db-dir/my-db-name":user=judy;
    password=no12see;retrieveMessagesFromServerOnGetMessage=true;

#### Example 4

The following is a sample program fragment that connects to the Network Server using the DB2 Driver for JDBC:

    String databaseURL = "jdbc:derby:net://localhost:1527/sample";
    // Load DB2 Driver for JDBC class
    Class.forName("com.ibm.db2.jcc.DB2Driver");
    // Set user and password properties
    Properties properties = new Properties();
    properties.put("user", "APP");
    properties.put("password", "APP");
    properties.put("retreiveMessagesFromServerOnGetMessage", "true");
    // Get a connection
    Connection conn = DriverManager.getConnection(databaseURL, properties);

Parent topic: Accessing the Network Server by using the DB2 Driver for JDBC