Reputation: 1595
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
Upvotes: 0
Views: 7629
Reputation: 4602
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?
Upvotes: 0
Reputation: 10396
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).
Upvotes: 1