29 Ağustos 2023 Salı

JDBC DatabaseMetaData.getTables metodu

Giriş
Bir makale burada. MySQL açısından bir veri tabanı şeklen şöyle
db 			<-- Ben yarattım
information_schema
mysql                   <-- MySQL kurulumu ile geliyor
performance_schema
sys
Açıklaması şöyle
The MySQL installation comes with default databases. One of the database is named 'mysql'.

db veri tabanını yaratmak için şöyle yaptım
CREATE TABLE IF NOT EXISTS db.myworker (...);
Örnek - catalog değeri geç
Elimizde şöyle bir kod olsun
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( connection.getCatalog(),
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet te boş geliyor


2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet dolu gelir ve içinde sadece db veri tabanında yarattığım tabloları görürüm

Örnek - catalog null yani catalog değeri geçme
Elimizde şöyle bir kod olsun. Bu sefer catalog yerine null geçtim. 
try (Connection connection = ...;
  ResultSet tables = connection.getMetaData().getTables( null,
        null,
        null,
        new String[]{"TABLE", "VIEW"})) {
  ...
}
1. Eğer bağlantı şöyle ise yani veri tabanı belirtilmemişse 
jdbc:mysql://localhost:3306/
connection.getCatalog() çağrısı "" döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

2. Eğer bağlantı şöyle ise yani veri tabanı belirtilmişse
jdbc:mysql://localhost:3306/db
connection.getCatalog() çağrısı db döner
connection.getSchema() çağrısı null döner
ResultSet  dolu gelir ve içinde sys ve db dahil her şeyi görürüm. Çünkü catalog değeri olarak null geçtim

Hiç yorum yok:

Yorum Gönder

Soft Delete

Giriş Açıklaması  şöyle When using the soft delete mechanism on the database, you might run into a situation where a record with a unique co...