如何列出计算机上已知实例中的所有SQL Server数据库?
答案 0 :(得分:3)
您可以尝试使用JDBC中的DatabaseMetaData#getCatalog()
(与MS SQLServerDriver和jTDS驱动程序一起使用):
import java.sql.*;
public class ListDatabases
{
public static void main(String [] args)
{
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1269;"
+ "user=sa;password=HerongYang");
DatabaseMetaData meta = con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of databases: ");
while (res.next()) {
System.out.println(res.getString("TABLE_CAT"));
}
res.close();
} catch (Exception e) {
System.err.println("Exception: " + e);
} finally {
con.close();
}
}
}