>  기사  >  데이터 베이스  >  Java를 사용하여 MySQL 데이터베이스의 모든 테이블을 표시하시겠습니까?

Java를 사용하여 MySQL 데이터베이스의 모든 테이블을 표시하시겠습니까?

王林
王林앞으로
2023-09-17 20:37:02864검색

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 명령을 사용하여 데이터베이스 내의 모든 테이블을 표시합니다. ". 쿼리는 다음과 같습니다. -

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 데이터베이스의 모든 테이블을 표시하시겠습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제