Get Catalog From Database Metadata : Metadata DB Info « Database SQL JDBC « Java






Get Catalog From Database Metadata

  

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

  public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet catalogs = null;
    DatabaseMetaData meta = conn.getMetaData();
    catalogs = meta.getCatalogs();
     
     while (catalogs.next()) {
       String catalog = catalogs.getString(1);  //"TABLE_CATALOG"
       System.out.println("catalog: "+catalog);
     }
   
    st.close();
    conn.close();
  }

  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }

  public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:caspian";
    String username = "mp";
    String password = "mp2";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

}

           
         
    
  








GetCatalogFromDatabaseMetadata.zip( 2,516 k)

Related examples in the same category

1.Get all key words in database
2.Get Database Schema From MetaData
3.Is statement pooling supported?
4.Database MetaData: Database version
5.Type info in database metadata
6.A database MetaData query
7.DatabaseMetaData class to obtain information about the
8.Database Info
9.JDBC Performance
10.Driver Property Info
11.If database support transaction
12.If database support scrollable result sets
13.Get database product information
14.Get data types supported by database
15.If database support batch update
16.Get database maximum table name length
17.Get numeric functions supported by database
18.Get JDBC driver information
19.Get system functions supported by database?
20.Get the max concurrent connection to a database?
21.Get date time functions supported by database
22.Get column names of a table using ResultSetMetaData
23.Get column's precision and scale value?
24.Get string functions supported by database?
25.JDBC Version App
26.Listing All Non-SQL92 Keywords Used by a Database
27.Listing the String Functions Supported by a Database: retrieves a list of string functions that a database supports.
28.Listing the Numeric Functions Supported by a Database
29.Listing the System Functions Supported by a Database
30.Listing the Time and Date Functions Supported by a Database
31.Getting the Maximum Table Name Length allowed in a Database
32.Detect if a table exists
33.This program uses metadata to display arbitrary tables in a database.