Example usage for java.sql DatabaseMetaData getDriverName

List of usage examples for java.sql DatabaseMetaData getDriverName

Introduction

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

Prototype

String getDriverName() throws SQLException;

Source Link

Document

Retrieves the name of this JDBC driver.

Usage

From source file:Main.java

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

    DatabaseMetaData md = conn.getMetaData();

    System.out.println("JDBC Driver: " + md.getDriverName() + " " + md.getDriverVersion());
    System.out.println("Database: " + md.getURL());
    System.out.println("User: " + md.getUserName());

    conn.close();//from   w  w w  . j  ava2s.  c  o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);/*from   w w w  .j a  v a  2s  .  c  o m*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();
    String driverName = metadata.getDriverName();
    String driverVersion = metadata.getDriverVersion();
    int majorVersion = metadata.getDriverMajorVersion();
    int minorVersion = metadata.getDriverMinorVersion();

    System.out.println("driverName = " + driverName);
    System.out.println("driverVersion = " + driverVersion);
    System.out.println("majorVersion = " + majorVersion);
    System.out.println("minorVersion = " + minorVersion);
    connection.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;/*from  w ww  .j av a  2s .com*/
    DatabaseMetaData meta = conn.getMetaData();
    System.out.println(meta.getDriverName());

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "guest";
    String password = "guest";

    try {/*from  w  w w. j a va  2s.  co  m*/
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

        // Get the MetaData
        DatabaseMetaData metaData = conn.getMetaData();

        // Get driver information
        System.out.println("Driver Informaion");
        System.out.println(metaData.getDriverName());
        System.out.println(metaData.getDriverVersion());
        // Get schema information
        System.out.println("Schemas");
        ResultSet schemas = metaData.getSchemas();
        while (schemas.next()) {
            System.out.println(schemas.getString(1));
        }
        // Get table information
        System.out.println("Tables");
        ResultSet tables = metaData.getTables("", "", "", null);
        while (tables.next()) {
            System.out.println(tables.getString(3));
        }
        conn.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

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

    DatabaseMetaData mtdt = conn.getMetaData();
    System.out.println("URL in use: " + mtdt.getURL());
    System.out.println("User name: " + mtdt.getUserName());
    System.out.println("DBMS name: " + mtdt.getDatabaseProductName());
    System.out.println("DBMS version: " + mtdt.getDatabaseProductVersion());
    System.out.println("Driver name: " + mtdt.getDriverName());
    System.out.println("Driver version: " + mtdt.getDriverVersion());
    System.out.println("supp. SQL Keywords: " + mtdt.getSQLKeywords());

    conn.close();/*from ww w  .j a v a2  s  .  c o  m*/
}

From source file:GetDBInfo.java

public static void main(String[] args) {
    Connection c = null; // The JDBC connection to the database server
    try {//from  w w  w  . ja va 2 s. co m
        // Look for the properties file DB.props in the same directory as
        // this program. It will contain default values for the various
        // parameters needed to connect to a database
        Properties p = new Properties();
        try {
            p.load(GetDBInfo.class.getResourceAsStream("DB.props"));
        } catch (Exception e) {
        }

        // Get default values from the properties file
        String driver = p.getProperty("driver"); // Driver class name
        String server = p.getProperty("server", ""); // JDBC URL for server
        String user = p.getProperty("user", ""); // db user name
        String password = p.getProperty("password", ""); // db password

        // These variables don't have defaults
        String database = null; // The db name (appended to server URL)
        String table = null; // The optional name of a table in the db

        // Parse the command-line args to override the default values above
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-d"))
                driver = args[++i]; //-d <driver>
            else if (args[i].equals("-s"))
                server = args[++i];//-s <server>
            else if (args[i].equals("-u"))
                user = args[++i]; //-u <user>
            else if (args[i].equals("-p"))
                password = args[++i];
            else if (database == null)
                database = args[i]; // <dbname>
            else if (table == null)
                table = args[i]; // <table>
            else
                throw new IllegalArgumentException("Unknown argument: " + args[i]);
        }

        // Make sure that at least a server or a database were specified.
        // If not, we have no idea what to connect to, and cannot continue.
        if ((server.length() == 0) && (database.length() == 0))
            throw new IllegalArgumentException("No database specified.");

        // Load the db driver, if any was specified.
        if (driver != null)
            Class.forName(driver);

        // Now attempt to open a connection to the specified database on
        // the specified server, using the specified name and password
        c = DriverManager.getConnection(server + database, user, password);

        // Get the DatabaseMetaData object for the connection. This is the
        // object that will return us all the data we're interested in here
        DatabaseMetaData md = c.getMetaData();

        // Display information about the server, the driver, etc.
        System.out.println("DBMS: " + md.getDatabaseProductName() + " " + md.getDatabaseProductVersion());
        System.out.println("JDBC Driver: " + md.getDriverName() + " " + md.getDriverVersion());
        System.out.println("Database: " + md.getURL());
        System.out.println("User: " + md.getUserName());

        // Now, if the user did not specify a table, then display a list of
        // all tables defined in the named database. Note that tables are
        // returned in a ResultSet, just like query results are.
        if (table == null) {
            System.out.println("Tables:");
            ResultSet r = md.getTables("", "", "%", null);
            while (r.next())
                System.out.println("\t" + r.getString(3));
        }

        // Otherwise, list all columns of the specified table.
        // Again, information about the columns is returned in a ResultSet
        else {
            System.out.println("Columns of " + table + ": ");
            ResultSet r = md.getColumns("", "", table, "%");
            while (r.next())
                System.out.println("\t" + r.getString(4) + " : " + r.getString(6));
        }
    }
    // Print an error message if anything goes wrong.
    catch (Exception e) {
        System.err.println(e);
        if (e instanceof SQLException)
            System.err.println(((SQLException) e).getSQLState());
        System.err.println("Usage: java GetDBInfo [-d <driver] " + "[-s <dbserver>]\n"
                + "\t[-u <username>] [-p <password>] <dbname>");
    }
    // Always remember to close the Connection object when we're done!
    finally {
        try {
            c.close();
        } catch (Exception e) {
        }
    }
}

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')");

    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {/*w w  w .j  a v a 2  s .  c o m*/
        int jdbcMajorVersion = meta.getJDBCMajorVersion();
        System.out.println("jdbcMajorVersion:" + jdbcMajorVersion);
    } catch (Exception e) {
        System.out.println("jdbcMajorVersion unsupported feature");
    }

    try {
        int jdbcMinorVersion = meta.getJDBCMinorVersion();
        System.out.println("jdbcMinorVersion:" + jdbcMinorVersion);
    } catch (Exception e) {
        System.out.println("jdbcMinorVersion unsupported feature");
    }

    String driverName = meta.getDriverName();
    String driverVersion = meta.getDriverVersion();
    System.out.println("driverName=" + driverName);
    System.out.println("driverVersion=" + driverVersion);

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

From source file:com.aurel.track.admin.server.status.ServerStatusBL.java

private static void loadDatabaseInfo(ServerStatusTO serverStatusTO) {
    Connection conn = null;/*www  .  j  av a2  s  .c  om*/
    try {
        conn = Torque.getConnection(BaseTSitePeer.DATABASE_NAME);
        DatabaseMetaData dbm = conn.getMetaData();
        serverStatusTO.setDatabase(dbm.getDatabaseProductName() + " " + dbm.getDatabaseProductVersion());
        serverStatusTO.setJdbcDriver(dbm.getDriverName() + " " + dbm.getDriverVersion());
        serverStatusTO.setJdbcUrl(dbm.getURL());
    } catch (Exception e) {
        LOGGER.error("Problem retrieving database meta data: " + e.getMessage());
    } finally {
        if (conn != null) {
            Torque.closeConnection(conn);
        }
    }
    Double ping = loadPing();
    serverStatusTO.setPingTime(ping.toString() + " ms");
}

From source file:com.jagornet.dhcp.db.DbSchemaManager.java

/**
 * Validate schema.//from w  w w. j  av a  2s  . c o  m
 * 
 * @param dataSource the data source
 * 
 * @throws SQLException if there is a problem with the database
 * @throws IOExcpetion if there is a problem reading the schema file
 * 
 * returns true if database was created, false otherwise
 */
public static boolean validateSchema(DataSource dataSource, String schemaFilename, int schemaVersion)
        throws SQLException, IOException {
    boolean schemaCreated = false;

    List<String> tableNames = new ArrayList<String>();

    Connection conn = dataSource.getConnection();
    DatabaseMetaData dbMetaData = conn.getMetaData();

    log.info("JDBC Connection Info:\n" + "url = " + dbMetaData.getURL() + "\n" + "database = "
            + dbMetaData.getDatabaseProductName() + " " + dbMetaData.getDatabaseProductVersion() + "\n"
            + "driver = " + dbMetaData.getDriverName() + " " + dbMetaData.getDriverVersion());

    String[] types = { "TABLE" };
    ResultSet rs = dbMetaData.getTables(null, null, "%", types);
    if (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    } else {
        createSchema(dataSource, schemaFilename);
        dbMetaData = conn.getMetaData();
        rs = dbMetaData.getTables(null, null, "%", types);
        schemaCreated = true;
    }
    while (rs.next()) {
        tableNames.add(rs.getString("TABLE_NAME"));
    }

    String[] schemaTableNames;
    if (schemaVersion <= 1) {
        schemaTableNames = TABLE_NAMES;
    } else {
        schemaTableNames = TABLE_NAMES_V2;
    }

    if (tableNames.size() == schemaTableNames.length) {
        for (int i = 0; i < schemaTableNames.length; i++) {
            if (!tableNames.contains(schemaTableNames[i])) {
                throw new IllegalStateException("Invalid database schema: unknown tables");
            }
        }
    } else {
        throw new IllegalStateException("Invalid database schema: wrong number of tables");
    }

    return schemaCreated;
}

From source file:com.tern.db.db.java

public static Database establish(DataSource ds, String name) throws SQLException {
    if (name == null || name.length() <= 0) {
        name = "default";
    }//  w w w.  ja  va  2  s.c om
    if (Database.dbs.containsKey(name)) {
        throw new SQLException("Has duplicate database name:" + name);
    }

    Connection con = null;
    String driverName = null;
    try {
        con = ds.getConnection();
        java.sql.DatabaseMetaData meta = con.getMetaData();
        driverName = meta.getDriverName();
    } catch (SQLException e) {
        Trace.write(Trace.Error, e, "Create database from DataSource");
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (java.sql.SQLException ex1) {
            }
        }
    }

    if (driverName == null) {
        return null;
    }

    Database d = null;

    if (driverName.indexOf("Oracle") >= 0) {
        d = new OracleDB();
    } else if (driverName.indexOf("mysql") >= 0) {
        d = new MySqlDB();
    } else {
        throw new SQLException("Unknown database driver:" + driverName);
    }

    d.ds = ds;

    if (Database.db == null) {
        Database.db = d;
    }

    Database.dbs.put(name, d);
    Trace.write(Trace.Running, "database added,name = %s,driver=%s", name, driverName);

    return d;
}