Example usage for java.sql DatabaseMetaData getTables

List of usage examples for java.sql DatabaseMetaData getTables

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getTables.

Prototype

ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[])
        throws SQLException;

Source Link

Document

Retrieves a description of the tables available in the given catalog.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection c = null;//from   w ww.  jav a2  s .  c  om
    DatabaseMetaData dbm = c.getMetaData();
    ResultSet rs = dbm.getTables(null, null, "employee", null);
    if (rs.next()) {
        System.out.println("Table exists");
    } else {
        System.out.println("Table does not exist");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();

    DatabaseMetaData mtdt = conn.getMetaData();

    ResultSet rs = mtdt.getTables(conn.getCatalog(), "%", "%", null);

    ResultSetMetaData rsmd = rs.getMetaData();
    int numCols = rsmd.getColumnCount();
    for (int i = 1; i <= numCols; i++) {
        if (i > 1)
            System.out.print(", ");
        System.out.print(rsmd.getColumnLabel(i));
    }//from w  w  w  .  j  a  v a 2s  . c  o m
    System.out.println("");
    while (rs.next()) {
        for (int i = 1; i <= numCols; i++) {
            if (i > 1)
                System.out.print(", ");
            System.out.print(rs.getString(i));
        }
        System.out.println("");
    }
    conn.close();
}

From source file:Main.java

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 rs = null;/*www .  jav  a  2s  .  c om*/
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "TABLE", "VIEW" });
    while (rs.next()) {
        String tableOrViewName = rs.getString("TABLE_NAME");
        System.out.println("getTableNames(): tableOrViewName=" + tableOrViewName);
    }

    st.close();
    conn.close();
}

From source file:Main.java

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

    ResultSet rs = null;// w w w .  j  ava2 s .  c  om
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "TABLE" });

    while (rs.next()) {
        String tableName = rs.getString("TABLE_NAME");
        System.out.println("tableName=" + tableName);
    }

    st.close();
    conn.close();
}

From source file:Main.java

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

    ResultSet rs = null;/*from   w w  w  .  ja va  2 s  .com*/
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTables(null, null, null, new String[] { "VIEW" });

    while (rs.next()) {
        String tableName = rs.getString("TABLE_NAME");
        System.out.println("tableName=" + tableName);
    }

    st.close();
    conn.close();
}

From source file:ListTables.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    // Enable logging
    // DriverManager.setLogStream(System.err);

    System.out.println("Getting Connection");
    Connection c = DriverManager.getConnection("jdbc:odbc:Companies", "ian", ""); // user, passwd
    DatabaseMetaData md = c.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
        System.out.println(rs.getString(3));
    }// w ww  .  j  ava  2 s. c  om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver);//  w  ww .  j av  a  2  s  . com
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    DatabaseMetaData dbm = con.getMetaData();
    String[] types = { "TABLE" };
    ResultSet rs = dbm.getTables(null, null, "%", types);
    System.out.println("Table name:");
    while (rs.next()) {
        String table = rs.getString("TABLE_NAME");
        System.out.println(table);
        con.close();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatabaseMetaData md = conn.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
        System.out.println(rs.getString(3));
    }//  w w  w.jav  a 2  s  .  c  om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   www. j a  v a  2 s  .  c o  m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    DatabaseMetaData dbmd = connection.getMetaData();

    String[] types = { "TABLE" };
    ResultSet resultSet = dbmd.getTables(null, null, "%", types);

    while (resultSet.next()) {
        String tableName = resultSet.getString(3);

        String tableCatalog = resultSet.getString(1);
        String tableSchema = resultSet.getString(2);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection conn = DriverManager.getConnection(URL, "user", "passw");

    DatabaseMetaData dmd = conn.getMetaData();
    ResultSet rs1 = dmd.getSchemas();
    while (rs1.next()) {
        String ss = rs1.getString(1);
        ResultSet rs2 = dmd.getTables(null, ss, "%", null);
        while (rs2.next())
            System.out.println(rs2.getString(3) + " " + rs2.getString(4));
    }/*  www.  jav  a 2  s .c o m*/
    conn.close();
}