Entering content frame

 Example for a Database Query 

Use the following example to find out how to connect to the database instance and then make 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 method java.sql.DriverManager.getConnection

         */

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

 

        /*

         * Make a database query

         */

        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