使用Java显示MySQL数据库中的所有表?
来源:tutorialspoint
时间:2023-09-04 14:42:07 484浏览 收藏
亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《使用Java显示MySQL数据库中的所有表?》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。
我们将在这里看到如何使用 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)
正如您在上面看到的,它们都给出了相同的结果。
到这里,我们也就讲完了《使用Java显示MySQL数据库中的所有表?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
189 收藏
-
366 收藏
-
265 收藏
-
499 收藏
-
423 收藏
-
487 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习