文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

使用Java显示MySQL数据库中的所有表?

2023-10-22 10:45

关注

我们将在这里看到如何使用 Java 显示 MySQL 数据库中的所有表。您可以使用 MySQL 中的 show 命令来获取 MySQL 数据库中的所有表。

假设我们的数据库是“test”。 Java代码如下,显示数据库“test”内的所有表名。

Java代码如下。这里,MySQL 和 Java 之间建立了连接 -

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.DatabaseMetaData;

public class GetAllTables {
   public static void main(String[] args) throws SQLException {
      Connection conn = null;
      try {
         try {
            Class.forName("com.mysql.jdbc.Driver");
         } catch (Exception e) {
            System.out.println(e);
         }
         conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test", "Manish", "123456");
         System.out.println("Connection is created succcessfully:");
      } catch (Exception e) {
         System.out.println(e);
      }
      ResultSet rs = null;
      DatabaseMetaData meta = (DatabaseMetaData) conn.getMetaData();
      rs = meta.getTables(null, null, null, new String[] {
         "TABLE"
      });
      int count = 0;
      System.out.println("All table names are in test database:");
      while (rs.next()) {
         String tblName = rs.getString("TABLE_NAME");
         System.out.println(tblName);
         count++;
      }
      System.out.println(count + " Rows in set ");
   }
}

以下是显示数据库测试中所有表的输出 -

Wed Dec 12 14:55:28 IST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL = false, or set useSSL = true and provide truststore for server certificate verification.
Connection is created succcessfully:
All table names are in test database:
add30minutesdemo
addcolumn
addoneday
agecalculatesdemo
aliasdemo
allcharacterbeforespace
allownulldemo
appendingdatademo
autoincrementdemo
betweendatedemo
bigintandintdemo
bigintdemo
bookdatedemo
changecolumnpositiondemo
changeenginetabledemo
charsetdemo
concatenatetwocolumnsdemo
constraintdemo
cumulativesumdemo
currentdatetimedemo
customers
dateasstringdemo
dateformatdemo
dateinsertdemo
datesofoneweek
datetimedemo
dayofweekdemo
decimaltointdemo
decrementdemo
defaultdemo
deleteallfromtable
deletemanyrows
destination
differencetimestamp
distinctdemo
employee
employeedesignation
findlowercasevalue
generatingnumbersdemo
gmailsignin
groupbytwofieldsdemo
groupmonthandyeardemo
highestidorderby
highestnumberdemo
ifnulldemo
increasevarchardemo
insert
insertignoredemo
insertwithmultipleandsigle
int11demo
intvsintanythingdemo
lasttwocharacters
likebinarydemo
likedemo
maxlengthfunctiondemo
moviecollectiondemo
myisamtoinnodbdemo
newtableduplicate
notequalsdemo
nowandcurdatedemo
nthrecorddemo
nullandemptydemo
orderbycharacterlength
orderbynullfirstdemo
orderindemo
originaltable
parsedatedemo
passinganarraydemo
persons
prependstringoncolumnname
pricedemo
queryresultdemo
replacedemo
rowexistdemo
rowpositiondemo
rowwithsamevalue
safedeletedemo
searchtextdemo
selectdataonyearandmonthdemo
selectdistincttwocolumns
selectdomainnameonly
sha256demo
skiplasttenrecords
sortcolumnzeroatlastdemo
storedproctable
stringreplacedemo
stringtodate
student
studentdemo
studentmodifytabledemo
studenttable
subtract3hours
temporarycolumnwithvaluedemo
timetosecond
timetoseconddemo
toggledemo
toogledemo
truncatetabledemo
updatealldemo
updatevalueincrementally
wheredemo
wholewordmatchdemo
zipcodepadwithzerodemo

103 Rows in set

要进行交叉检查,请使用 MySQL show 命令显示数据库“test”内的所有表。查询如下 -

mysql> use test;
Database changed
mysql> show tables;

以下是输出 -

+------------------------------+
| Tables_in_test               |
+------------------------------+
| add30minutesdemo             |
| addcolumn                    |
| addoneday                    |
| agecalculatesdemo            |
| aliasdemo                    |
| allcharacterbeforespace      |
| allownulldemo                |
| appendingdatademo            |
| autoincrementdemo            |
| betweendatedemo              |
| bigintandintdemo             |
| bigintdemo                   |
| bookdatedemo                 |
| changecolumnpositiondemo     |
| changeenginetabledemo        |
| charsetdemo                  |
| concatenatetwocolumnsdemo    |
| constraintdemo               |
| cumulativesumdemo            |
| currentdatetimedemo          |
| customers                    |
| dateasstringdemo             |
| dateformatdemo               |
| dateinsertdemo               |
| datesofoneweek               |
| datetimedemo                 |
| dayofweekdemo                |
| decimaltointdemo             |
| decrementdemo                |
| defaultdemo                  |
| deleteallfromtable           |
| deletemanyrows               |
| destination                  |
| differencetimestamp          |
| distinctdemo                 |
| employee                     |
| employeedesignation          |
| findlowercasevalue           |
| generatingnumbersdemo        | 
| gmailsignin                  |
| groupbytwofieldsdemo         |
| groupmonthandyeardemo        |
| highestidorderby             |
| highestnumberdemo            |
| ifnulldemo                   |
| increasevarchardemo          |
| insert                       |
| insertignoredemo             |
| insertwithmultipleandsigle   |
| int11demo                    |
| intvsintanythingdemo         |
| lasttwocharacters            |
| likebinarydemo               |
| likedemo                     |
| maxlengthfunctiondemo        |
| moviecollectiondemo          |
| myisamtoinnodbdemo           |
| newtableduplicate            |
| notequalsdemo                |
| nowandcurdatedemo            |
| nthrecorddemo                |
| nullandemptydemo             |
| orderbycharacterlength       |
| orderbynullfirstdemo         |
| orderindemo                  |
| originaltable                |
| parsedatedemo                |
| passinganarraydemo           |
| persons                      |
| prependstringoncolumnname    |
| pricedemo                    |
| queryresultdemo              |
| replacedemo                  |
| rowexistdemo                 |
| rowpositiondemo              |
| rowwithsamevalue             |
| safedeletedemo               |
| searchtextdemo               |
| selectdataonyearandmonthdemo |
| selectdistincttwocolumns     |
| selectdomainnameonly         |
| sha256demo                   |
| skiplasttenrecords           |
| sortcolumnzeroatlastdemo     |
| storedproctable              |
| stringreplacedemo            |
| stringtodate                 |
| student                      |
| studentdemo                  |
| studentmodifytabledemo       |
| studenttable                 |
| subtract3hours               |
| temporarycolumnwithvaluedemo |
| timetosecond                 |
| timetoseconddemo             |
| toggledemo                   |
| toogledemo                   |
| truncatetabledemo            |
| updatealldemo                |
| updatevalueincrementally     |
| wheredemo                    |
| wholewordmatchdemo           |
| zipcodepadwithzerodemo       |
+------------------------------+
103 rows in set (0.01 sec)

正如您在上面看到的,它们都给出了相同的结果。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯