简体   繁体   English

SAP HANA无效的表名

[英]Sap hana invalid table name

I am trying to connect hana database using java code as follow 我正在尝试使用以下Java代码连接hana数据库

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. 当我使用hana studio连接时,Live2架构中存在连接表。

Any suggestion what's going wrong? 任何建议出什么事了吗?

Thanks 谢谢

The SAP documentation has a working sample, and there are databases in the URL for multidatabase environments. SAP文档有一个有效的示例,URL中有用于多数据库环境的数据库。

http://help.sap.com/saphelp_hanaplatform/helpdata/en/ff/15928cf5594d78b841fbbe649f04b4/content.htm http://help.sap.com/saphelp_hanaplatform/helpdata/zh-CN/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网址应如下所示

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? 更新:表列表可以通过jdbc getTables()调用获得,如此处所述,例如, 如何从数据库获取所有表名?

My guess here is: you have created the table with the name "Connections" (upper and lower case characters. 我的猜测是:您创建的表的名称为“ Connections”(大小写字符。

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. 现在,在您的代码中,您无需在名称两边加上引号,这使SAP HANA自动执行对象名称线性化:它会自动使所有字符变为大写。

That way your query looks for CONNECTIONS while the table is called Connections. 这样,当表称为“连接”时,查询将查找CONNECTIONS。 Just put it in quotation marks to have it find your table (or rename the table to all upper case letters). 只需将其放在引号中即可找到表(或将表重命名为所有大写字母)。

  • Lars 拉尔斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM