Entering content frame

This graphic is explained in the accompanying text Example: Connecting to the Database Instance Locate the document in the library structure

The following examples show different options as to how you can connect the database instance to your Java application.

The following example data are used:

Description

Property

Example

Name of the database user

user

MONA

Password of the database user

password

RED

Name of database instance

-

DEMODB

Name of the computer on which the database instance is running

server

PARMA

SQL mode

sqlmode

ORACLE

Creating a Connection Using the java.sql.DriverManager.getConnection Method

You have the following options for transferring the connection options:

     You transfer the connection options as part of the Connection URL.

The connection URL, url has the following content: "jdbc:sapdb://PARMA/DEMODB?sqlmode=ORACLE"

To connect to the database instance, call the getConnection method as follows:

java.sql.Connection conn = java.sql.DriverManager.getConnection(url, "MONA", "RED");

     You transfer the connection options as well as the database user’s name and the password as part of the connection URL.

The connection URL, url has the following content:
"jdbc:sapdb://PARMA/DEMODB?sqlmode=ORACLE&user=MONA&password=RED"

To connect to the database instance, call the getConnection method as follows:

java.sql.Connection conn = java.sql.DriverManager.getConnection (url);

     The connection options, name and password of the database user are transferred in an object of the java.util.Properties class.

The connection URL, url has the following content:

"jdbc:sapdb://PARMA/DEMODB"

...

                            a.      You create an object of the class java.util.Properties:

java.util.Properties properties = new java.util.Properties ();

                            b.      You define the connection options:

properties.setProperty ("user", "MONA");

properties.setProperty ("password", "RED");

properties.setProperty ("sqlmode", "ORACLE");

                            c.      To connect to the database instance, call the getConnection method as follows:

java.sql.Connection conn = java.sql.DriverManager.getConnection (url, properties);

You can find more information under java.sql.DriverManager.getConnection Method.

Connecting with the com.sap.dbtech.jdbcext.DataSourceSapDB Class

import java.sql.Connection;

import java.sql.Statement;

import java.sql.SQLException;

import javax.sql.DataSource;

import java.sql.ResultSet;

import com.sap.dbtech.jdbcext.DataSourceSapDB;

 

public final class HelloDataSource

{

    public static void main (String[] args) throws SQLException

     {

        String  dbUser   = "MONA";

        String password = "RED";

        String  dbName   = "DEMODB";

        String  dbServer = "PARMA";

        DataSourceSapDB ds = zero;

 

        ds = new DataSourceSapDB();

        ds.setUser(dbUser);

        ds.setPassword(password);

        ds.setDatabaseName(dbName);

        ds.setServer(dbServer);

 

        Connection c = ds.getConnection();

        Statement  s = c.createStatement();

        ResultSet rs = s.executeQuery("SELECT * FROM 

HOTEL.CITY");

        rs.next();

        System.out.println(rs.getString(1));

        return;

      }

}

You can find more information under Class com.sap.dbtech.jdbcext.DataSourceSapDB.

 

Leaving content frame