Displaying onlty tables from MSSQL Server 2005 using java
I have a problem with fetching table names from SQL Server 2005. I have succeeded in fetching the table names but the problem is along with the table names VIEWS are also displaying. I need to display only t开发者_运维技巧he table names in a dropdown.
My code is:
...
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));
}
Here System.out.println(rrs.getString(3));
statement prints all the table names, but along with view names. I need to avoid printing view names. How can I do it?
you can query to meta data tables on MSSQL server. e.g. select * from sysobjects where xtype = 'u' ;here xtype is type of object and 'u' refers to the table type objects.. see MSSQL server documentation for syntax details
there are two areas:
1/ you not restricted on Db side for view the Metadata
2/ by using aliases return AliasName f.e. Select SomeColumNane as Count returns 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
精彩评论