The following example shows how to establish a connection to the database instance and how to 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 database_user = "MONA";
String password = "RED";
String database_computer = "GENUA";
String database_name = "DEMODB";
/*
* Registering the JDBC Driver in the JDBC Driver Manager
*/
Class.forName ("com.sap.dbtech.jdbc.DriverSapDB");
/*
*/
String url = "jdbc:sapdb://" + database_computer + "/" + database_name;
/*
* Calling the java.sql.DriverManager.getConnection method
*/
Connection connection = DriverManager.getConnection (url, database_user, password);
/*
* Executing 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);
/*
* Closing all objects
*/
resultSet.close ();
stmt.close();
connection.close ()
}
}