-1

I wanted to retrive all the table name from a specific database. I tried the follwowing code

try {
        DatabaseMetaData dbmd = connection.getMetaData();
        String[] types = {"TABLE"};
        ResultSet rs = dbmd.getTables(null, null, "%", types);
        while (rs.next()) {
            System.out.println(rs.getString("TABLE_NAME"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

but it is giving nullpointer exception.

Nikhil Kadam
  • 159
  • 1
  • 8
  • 23
  • Precisely **WHERE** is it giving the nullpointer exception? – Mike Nakis Feb 05 '15 at 12:45
  • Precisely **WHICH** RDBMS are you using? – Mike Nakis Feb 05 '15 at 12:46
  • Uhm, I am not sure if I asked this before: Precisely WHERE is it giving the nullpointer exception? – Mike Nakis Feb 05 '15 at 13:02
  • on button click in swing I was creating the table in database. While in constructor I was trying to get the table name which was added in database. After executing I was getting null pointer exception , but after adding DriverManager.getConnection("jdbc:sqlite:testdata.db"); in constructor I am getting the output. – Nikhil Kadam Feb 05 '15 at 13:21
  • Okay, next time you will save yourself and everyone else from a lot of wasted time by explaining exactly where the problem is. "WHERE" in this case means "on which line". So, I suppose in your case the exception was being thrown by `connection.getMetaData()`. Who would have thought of this. – Mike Nakis Feb 05 '15 at 13:25

2 Answers2

1

Following example shows that the method conn.getMetaData() would return the expected data.

public class Main {

    public static void main(String[] args) throws SQLException {
        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:testdata.db");
                Statement stmt = conn.createStatement()) {
            String sql = "CREATE TABLE IF NOT EXISTS TEST_TABLE"
                    + " (ID INT PRIMARY KEY     NOT NULL,"
                    + " NAME           TEXT    NOT NULL, "
                    + " ADDRESS        CHAR(50))";
            stmt.executeUpdate(sql);

            DatabaseMetaData md = conn.getMetaData();
            ResultSet rs = md.getTables(null, null, "%", null);
            while (rs.next()) {
                System.out.printf("table name: %s%n", rs.getString("TABLE_NAME"));
            }
        }
    }
}

produced output:

table name: TEST_TABLE
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
0

Try the following:

DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}

(taken from here: https://stackoverflow.com/a/2780375/1843508)

Community
  • 1
  • 1
Ofer Lando
  • 814
  • 7
  • 12
  • I have tried the code , It doesn't give any output. I am calling the the following code in constructor and I am using Sqlite database . – Nikhil Kadam Feb 05 '15 at 12:46
  • Try running the following query (via JDBC) and see if it gives you results: SELECT name FROM sqlite_master WHERE type='table'; – Ofer Lando Feb 05 '15 at 12:53