Pergunta

I am working on an android app that connects to any given MSSQL database. For that to work properly, I have to read database metadata to get the table names within it. I tested it back at the start of developing, and it worked fine. This is the code that worked:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(url, login, password);
DatabaseMetaData dbmd = conn.getMetaData();
String[] tables = {"TABLE"};
ResultSet rs = dbmd.getTables(null, null, "%", tables);
while (rs.next()) {
     String tableName = rs.getString(3);
     t.setText(t.getText()+" "+tableName+";");
}
conn.close();

As I said, it worked perfectly, and it still works in that place if I uncomment it. Later on, I did something like that in my Connection class:

public ArrayList<String> getTableNames() throws SQLException {
ArrayList<String> namelist = new ArrayList<String>();
DatabaseMetaData dbmd = conn.getMetaData();
String[] tables = {"TABLES"};
ResultSet rs = dbmd.getTables(null, null, "%", tables);
while (rs.next()) {
    String name = rs.getString(3);
    namelist.add(name);
}
return namelist;
}

conn is the Connection object, which is created in the same way as above within class constructor.

The problem is it returns empty list, and after some tests I did I realized it's because the ResultSet is empty (rs.next() returns false). I'm kinda lost, because it's basically the same thing done as before, only it's working outside the class, but not inside. Any ideas?

Foi útil?

Solução

In your first Code-Snippet there you put "TABLE" as a parameter and in the second snippet you put "TABLES" as a parameter.

I think you have mistyped.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top