0

I am retrieving tables from database. I am using the following code:

HashSet<String> hash = new HashSet<String>();
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {   
    hash.add(rs.getString(3));
}

I am getting each table twice as the output of this code. I am retrieving through hashset, but still I am getting each table twice. I want each table only one time.

Secondly, I want to use the tables stored in hashset to retrieve data from database. Which I am doing like this:

for (int i = 0; i < hash.size(); i++) {
    sql="Select distinct a from hash.get(i)";
    //now passing this to prepared statement and as usual taking resultset
}

But I run my application it shows that this does not exist, even when table is present in database. Please help me with both these scenarios.

  • This cannot be your actual code as this won't even compile. Post some actual code as there's no reason to invesigate the fake code you came up with. – lexicore Oct 03 '17 at 19:06
  • I have added the part of code in which I am getting problem and this is not a fake one.. – user8548920 Oct 03 '17 at 19:07
  • 1
    `hash.add(rs,getString(3))` - This. Will. Not. Compile. This cannot be real code. – lexicore Oct 03 '17 at 19:09
  • I have edited it please have a look – user8548920 Oct 03 '17 at 19:14
  • Show an example of which values does your set contain. – lexicore Oct 03 '17 at 19:15
  • Are you passing this string to the prepared statement: "Select distinct a from hash.get(i)" o it's just a typo? – Dominik Kunicki Oct 03 '17 at 19:16
  • a_employee_raw, a_employee_raw..this should come only once..this table and other tables re coming twice – user8548920 Oct 03 '17 at 19:18
  • Did you try to display each column of your result, it could help, there must be something different. – Dominik Kunicki Oct 03 '17 at 19:20
  • You set can't hold duplicate values. Somehow these values are different. Find out how. – lexicore Oct 03 '17 at 19:21
  • 1
    Perhaps the connection has access to multiple schemas and the tables are defined in multiple schemas? Also, HashSet.get() doesn't exist. – Andrew S Oct 03 '17 at 19:21
  • @andrew I think you are right,,,how I can get tables from one schema and how to retreive data from tables present in hashset – user8548920 Oct 03 '17 at 19:27
  • 1
    Add the schema parameter to narrow the search: `md.getTables(null, "{schemaOfInterest}", "%", null)` or change the user of the connection to only have access to the schema. And [here](https://stackoverflow.com/questions/12455737/how-to-iterate-over-a-set-hashset-without-an-iterator) to iterate over a Set. – Andrew S Oct 03 '17 at 19:37
  • One more doubt how to add schema.. Could you please show with example to add user defined tables from schema – user8548920 Oct 04 '17 at 03:59

0 Answers0