Java – in the case of MSSQL, metadata Gettables result set is empty

I tried to select all table names from the database Use the following code line

DatabaseMetaData databaseMetaData =  connection.getMetaData();
            resultSet = databaseMetaData.getTables(null,dbName,null,null);
            while(resultSet.next()){
                        //doing operations - take table name and all that
                        }

The problem here is that it works normally in the case of MySQL, which means that the underlying database is MySQL - no problem But when I try the same code with MSSQL, the result is empty Are there other alternatives to getting table details from the MSSQL database

If it's Please provides the difference of obtaining table details under Diff database, such as mysql, MSSQL and Oracle; Because in my case, my database may be any of the above Anyone can ask for help

Solution

Get code, for example:

Connection con = null;
    try {

      Class.forName(
        "com.microsoft.sqlserver.jdbc.sqlServerDriver");
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");

      DatabaseMetaData Meta = con.getMetaData();
      ResultSet res = Meta.getTables(null,new String[] {"TABLE"});
      System.out.println("List of tables: "); 
      while (res.next()) {
         System.out.println(
            "   "+res.getString("TABLE_CAT") 
           + ","+res.getString("TABLE_SCHEM")
           + ","+res.getString("TABLE_NAME")
           + ","+res.getString("TABLE_TYPE")
           + ","+res.getString("REMARKS")); 
      }
      res.close();

      con.close();

You can try this link on this page:

Listing All Tables – getTables()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>