I am working with JDBC and I have created a simple table using postgresql:
create table bank (
bank_id int,
bank_name varchar(40),
bank_address varchar(80),
user_id int,
user_first_name varchar(40),
user_surname varchar(40),
user_phone varchar(12),
primary key(bank_id, user_id)
);
The problem is that when I check whether bank_id is unique, I get true (when I was expecting false, ofc). Here is the code for my function that checks whether a column in a table is unique:
private static boolean checkIfUnique(Connection conn, String tableName,
String columnName) {
try {
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getIndexInfo(null, null, tableName, true, true);
while(rs.next()) {
if (rs.getString(9).equals(columnName)) {
return true;
}
}
return false;
} catch (Exception e) {
System.err.println("Exception: " + e + "\n" + e.getMessage());
}
return false;
}
I'm not really sure what I did wrong. Any suggestion would help. Thank you.