Last active
December 19, 2015 14:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} | |
} |
Related HANA JDBC issue:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'");
}