0

I am trying to connect hana database using java code as follow

try {
            Class.forName("com.sap.db.jdbc.Driver");

            String url = "jdbc:sap://host:30015/?";
            String user = "myuser";
            String password = "password";
            System.out.println("try to connect to HANA !");
            Connection cn = java.sql.DriverManager.getConnection(url, user, password);
            System.out.println("Connection to HANA successful!");
            ResultSet rs = cn.createStatement().executeQuery("select * from LIVE2.Connections");
            rs.next();
            System.out.println(rs.getInt(1));
        } catch (Exception e) {
            e.printStackTrace();
        }

Connection is established successfully. And following exception occurs

com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [259] (at 20): invalid table name:  Could not find table/view CONNECTIONS in schema LIVE2: line 1 col 21 (at pos 20)
    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.createException(SQLExceptionSapDB.java:345)
    at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.generateDatabaseException(SQLExceptionSapDB.java:176)
    at com.sap.db.jdbc.packet.ReplyPacket.buildExceptionChain(ReplyPacket.java:102)
    at com.sap.db.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:1033)
    at com.sap.db.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:823)

While when i connect using hana studio, Connections table exist in Live2 schema.

Any suggestion what's going wrong?

Thanks

3
  • does HANA have any means of database shares? You give no database name in the jdbc string, that means you are on the server itself.
    – thst
    Apr 30, 2015 at 17:56
  • Shoud I need to give schema name in url? As i m giving in my query. I just modified connection string and replaced ? mark with LIVE2. But same issue Apr 30, 2015 at 17:58
  • Maybe you created the table using double quotes which makes names case-sensitive in SQL: Try quoting the names in your SQL query: "select * from \"LIVE2\".\"Connections\""
    – user330315
    May 1, 2015 at 6:57

2 Answers 2

1

My guess here is: you have created the table with the name "Connections" (upper and lower case characters.

Now in your code you don't put quotation marks around the name, which makes SAP HANA perform its automatic object name linearization: it automatically makes all characters upper case.

That way your query looks for CONNECTIONS while the table is called Connections. Just put it in quotation marks to have it find your table (or rename the table to all upper case letters).

  • Lars
2
  • Lars If I put quotation marks around the name so following exception occurs sql syntax error: incorrect syntax near "Connections": line 1 col 15 Apr 30, 2015 at 20:49
  • As the quotation marks need to be a part of the actual SQL string you need to escape them. Something like ResultSet rs = cn.createStatement().executeQuery("select * from \"LIVE2\".\"Connections\""); should work.
    – Lars Br.
    May 2, 2015 at 16:32
0

The SAP documentation has a working sample, and there are databases in the URL for multidatabase environments.

http://help.sap.com/saphelp_hanaplatform/helpdata/en/ff/15928cf5594d78b841fbbe649f04b4/content.htm

The sample code from the page is inserted here to make it short.

import java.sql.*;
public class jdemo {
   public static void main(String[] argv) {
      Connection connection = null;
      try {                  
         connection = DriverManager.getConnection(
            "jdbc:sap://myhdb:30715/?autocommit=false",myname,mysecret);                  
      } catch (SQLException e) {
         System.err.println("Connection Failed. User/Passwd Error?");
         return;
      }
      if (connection != null) {
         try {
            System.out.println("Connection to HANA successful!");
            Statement stmt = connection.createStatement();
            ResultSet resultSet = stmt.executeQuery("Select 'hello world' from dummy");
            resultSet.next();
            String hello = resultSet.getString(1);
            System.out.println(hello);
       } catch (SQLException e) {
          System.err.println("Query failed!");
       }
     }
   }
}

If you have a multidatabase environment the JDBC url should look like this

jdbc:sap://localhost:30013/?databaseName=tdb1&user=SYSTEM&password=manager

UPDATE: The list of tables can be obtained through the jdbc getTables() call, as described here for example How to get all table names from a database?

1
  • thanks for response. Dear "Select * from dummy works fine for me as well " and I just changed url as followjdbc:sap://54.69.200.113:30015/?databaseName=LIVE2 But same issue Apr 30, 2015 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.