Entering content frame

This graphic is explained in the accompanying text Example of a Database Query Locate the document in its SAP Library structure

Use the following example to find out how to connect to the database instance and then execute simple database queries. The individual steps are listed in the comments in the example.

 

import java.sql.*;

 

public class HelloSapDB {

    public static void main (String [] args)

            throws ClassNotFoundException, SQLException

    {

        String dbm_user = "TESTUSER";

        String password = "TEST";

        String database_server = "LOCALSERVER";

        String database_name = "TST";

 

        /*

         * Register the JDBC driver

         */

        Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");

        /*

         * Define the connection URL

         */

        String url = "jdbc:sapdb://" + database_server + "/" + database_name;

        /*

         * Call the java.sql.DriverManager.getConnection method

         */

        Connection connection = DriverManager.getConnection (url, dbm_user, password);

 

        /*

         * Execute a database query; database queries are formulated in Structured Query Language (SQL).

         */

        Statement stmt = connection.createStatement ();

        ResultSet resultSet = stmt.executeQuery ("SELECT 'hello world' FROM dual");

        resultSet.next ();

        String hello = resultSet.getString (1);

        System.out.println (hello);

        /*

         * Close all objects

         */

        resultSet.close ();

        stmt.close ();

        connection.close ()

    }

}

 

Leaving content frame