Skip to content

Instantly share code, notes, and snippets.

@cwdesautels
Last active December 19, 2015 14:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwdesautels/5968797 to your computer and use it in GitHub Desktop.
Save cwdesautels/5968797 to your computer and use it in GitHub Desktop.
Demonstrates a problem in the jdbc api for HANA when using meta data to get primary keys.
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class HANAGetPrimaryKeysTest
{
public static void main(String[] args)
{
Connection conn = null;
Statement stmnt = null;
try
{
// Database product version: 1.00.62.380773
// JDBC driver version: 1.00.60.Build 0379371-1510
conn = com.sap.db.jdbc.Driver.connect("user", "pw", "url", "test", null);
stmnt = conn.createStatement();
stmnt.execute("create table test.a(id integer primary key)");
stmnt.execute("create table test.b(id integer not null)");
stmnt.execute("alter table test.b add constraint b_PK primary key(id)");
if (conn.getMetaData().getPrimaryKeys(null, "TEST", "A").next() == true)
{
// Expected Result
System.out.println("Found primary key(s) for table 'test.a'");
}
else
{
// Unexpected Result
System.out.println("Failed to find primary key(s) for table 'test.a'");
}
if (conn.getMetaData().getPrimaryKeys(null, "TEST", "B").next() == true)
{
// Expected Result
System.out.println("Found primary key(s) for table 'test.b'");
}
else
{
// Unexpected Result
System.out.println("Failed to find primary key(s) for table 'test.b'");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
if (stmnt != null)
{
try
{
stmnt.execute("drop table test.a");
stmnt.execute("drop table test.b");
stmnt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
@gbatumbya
Copy link

if (conn.getMetaData().getPrimaryKeys("catalog", "schema", "test.a").next() == true) ->

if (conn.getMetaData().getPrimaryKeys("catalog", "schema", "test.a").next())
{
System.out.println("Found primary key(s) for table 'test.a'");
}
else
{
System.out.println("No primary key(s) for table 'test.a'");
}

@cwdesautels
Copy link
Author

Related HANA JDBC issue:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment