使用java仅显示MS SQL Server 2005中的表

时间:2011-05-07 02:33:00

标签: java sql-server-2005 jdbc

我在从SQL Server 2005获取表名时遇到问题。我已经成功获取了表名,但问题是表名VIEWS也在显示。我只需要在下拉列表中显示表名。

我的代码是:

...
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
               "databaseName="somedb";username=sa;password=1234";
Connection con = null;
Statement st = null;
ResultSet rslt = null;

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

con = DriverManager.getConnection(connectionUrl);

DatabaseMetaData md = con.getMetaData();

ResultSet rrs = md.getTables(null, null, "%", null);

while (rrs.next()) 
{
     System.out.println(rrs.getString(3));
}

此处System.out.println(rrs.getString(3));语句打印所有表名,但同时显示视图名称。我需要避免打印视图名称。我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以在MSSQL服务器上查询元数据表。例如select * from sysobjects,其中xtype ='u';这里xtype是对象的类型,'u'是指表类型对象..有关语法详细信息,请参阅MSSQL服务器文档

答案 1 :(得分:0)

有两个方面:

1 /您不限制在Db方面查看元数据

2 /使用别名返回AliasName f.e.选择SomeColumNane作为Count返回columnName Count

private ResultSetMetaData metaData; //variable
//intialize rslt("Select .....") and thenarter you can call from rstl
metaData = rslt.getMetaData();
//get Column Class (Varchar, Date, Double....)
String className = metaData.getColumnClassName(column + 1);// packed into try catch finally block
// get column count
int columnCount = metaData.getColumnCount();// packed into try catch finally block
//get Column Name
String columnName = metaData.getColumnName(column + 1);// packed into try catch finally block