jdbc取出表名 名称

Posted funny_coding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc取出表名 名称相关的知识,希望对你有一定的参考价值。

 package com.dataconnect.test.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SchemaDetailsTest {

   private static Logger log = LoggerFactory
       .getLogger(SchemaDetailsTest.class);

   public static void main(String args[]) throws Exception {

     String databaseName = "myDbName";
     String userName = "username";
     String password = "password";
     String mysqlPort = "3306";
     String hostUrl = "127.0.0.1";

     // Setup the connection with the DB
     Class.forName("com.mysql.jdbc.Driver");

     Connection conn = DriverManager.getConnection("jdbc:mysql://" + hostUrl
         + ":" + mySQLPort, userName, password);

     // --- LISTING DATABASE SCHEMA NAMES ---
     ResultSet resultSet = conn.getMetaData().getCatalogs();

     while (resultSet.next()) {
       log.info("Schema Name = " + resultSet.getString("TABLE_CAT"));
     }
     resultSet.close();

     // --- LISTING DATABASE TABLE NAMES ---
     String[] types = { "TABLE" };
     resultSet = conn.getMetaData()
         .getTables(databaseName, null, "%", types);
     String tableName = "";
     while (resultSet.next()) {
       tableName = resultSet.getString(3);
       log.info("Table Name = " + tableName);
     }
     resultSet.close();

     // --- LISTING DATABASE COLUMN NAMES ---
     DatabaseMetaData meta = conn.getMetaData();
     resultSet = meta.getColumns(databaseName, null, tableName, "%");

     while (resultSet.next()) {
       log.info("Column Name of table " + tableName + " = "
           + resultSet.getString(4));
     }
   }
}

 

以上是关于jdbc取出表名 名称的主要内容,如果未能解决你的问题,请参考以下文章

JDBC:获取 SQL 查询中涉及的表名

JDBC - 从 OpenOffice 数据库中获取所有表名

部分代码片段

BigQuery jdbc 驱动程序“缺少数据集名称”

EF6 自定义迁移表名

JDBC预编译语句表名占位异常