码迷,mamicode.com
首页 > 数据库 > 详细

jdbc取出表名 名称

时间:2018-06-28 12:08:30      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:tail   取出   public   jdb   inf   etc   result   log   names   

 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取出表名 名称

标签:tail   取出   public   jdb   inf   etc   result   log   names   

原文地址:https://www.cnblogs.com/yszzu/p/9237848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!