【发布时间】:2017-05-22 16:22:47
【问题描述】:
我正在尝试使用 Java 获取数据库中所有表的名称。
这是我正在使用的方法。但是,在运行它时,它还会列出其他实际上不属于我的数据库的表,或者可能是一些我不感兴趣的系统表。我怎样才能只获取我创建的表?
/* connecting to database using supplied credentials, and printing out the SQL queries for inserting data to local db */
public void connectToAzure(String connectionString, String databaseName, String username, String password) throws ClassNotFoundException, SQLException
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
final Connection m_Connection = DriverManager.getConnection( connectionString+";DatabaseName="+databaseName, username, password );
final ArrayList<String> tables = getTableNames(m_Connection);
}
/* helper method to get table names */
public ArrayList<String> getTableNames(final Connection m_Connection)
{
final ArrayList<String> tables = new ArrayList<>();
try
{
DatabaseMetaData dbmd = m_Connection.getMetaData();
String[] types = {"TABLE"};
ResultSet rs = dbmd.getTables(null, null, "%", types);
while (rs.next())
{
String tableName = rs.getString("TABLE_NAME");
tables.add( tableName );
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return tables;
}
上面运行的输出
table1, table2, table3, table4, table5, table6, trace_xe_action_map, trace_xe_event_map
其中,
trace_xe_action_map, trace_xe_event_map
不是我创建的表。
【问题讨论】:
标签: java sql-server database jdbc