List of usage examples for java.sql DatabaseMetaData getURL
String getURL() throws SQLException;
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. jav a2 s . c om }
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 ww w. j a va2 s . c om*/ }
From source file:Main.java
public static void main(String[] argv) { Connection connection = null; Statement statement;//w ww. jav a2 s . co m ResultSet rs; try { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB"); connection = ds.getConnection(); DatabaseMetaData md = connection.getMetaData(); statement = connection.createStatement(); System.out.println("getURL() - " + md.getURL()); System.out.println("getUserName() - " + md.getUserName()); System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion()); System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion()); System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion()); System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh()); System.out.println("<H1>Feature Support</H1>"); System.out.println( "supportsAlterTableWithDropColumn - " + md.supportsAlterTableWithDropColumn() + "<BR>"); System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates()); System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames()); System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete()); System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins()); System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures()); System.out.println("supportsMixedCaseQuotedIdentifiers - " + md.supportsMixedCaseQuotedIdentifiers()); System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL()); System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar()); System.out.println("getMaxRowSize - " + md.getMaxRowSize()); System.out.println("getMaxStatementLength - " + md.getMaxStatementLength()); System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()); System.out.println("getMaxConnections - " + md.getMaxConnections()); System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength()); System.out.println("getTableTypes()"); rs = md.getTableTypes(); while (rs.next()) { System.out.println(rs.getString(1)); } System.out.println("getTables()"); rs = md.getTables("accounts", "", "%", new String[0]); while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); } System.out.println("Transaction Support"); System.out.println("getDefaultTransactionIsolation() - " + md.getDefaultTransactionIsolation()); System.out .println("dataDefinitionIgnoredInTransactions() - " + md.dataDefinitionIgnoredInTransactions()); System.out.println("General Source Information"); System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect()); System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable()); System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions()); System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar()); System.out.println("getTypeInfo()"); rs = md.getTypeInfo(); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); } }
From source file:GetDBInfo.java
public static void main(String[] args) { Connection c = null; // The JDBC connection to the database server try {/*from w ww. ja va 2s . c o 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:mangotiger.sql.SQL.java
private static String asString(final Connection connection) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); return metaData.getURL() + ":username=" + metaData.getUserName(); }
From source file:com.jagornet.dhcp.db.DbSchemaManager.java
/** * Validate schema.// www .j av a2 s.co 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.anyi.gp.license.RegisterTools.java
public static String getDBServerURL() { Connection conn = null;/* w ww. ja v a 2 s . c o m*/ try { conn = DAOFactory.getInstance().getConnection(); if (conn != null) { DatabaseMetaData meta = conn.getMetaData(); return (meta.getURL() + ":" + meta.getUserName()).toUpperCase(); } } catch (SQLException e) { e.printStackTrace(); } finally { DBHelper.closeConnection(conn); } return ""; }
From source file:com.aurel.track.dbase.UpdateDbSchema.java
private static boolean hasTables(Connection conn) { boolean hasTables = false; try {// w w w . ja va 2s.c om DatabaseMetaData md = conn.getMetaData(); String userName = md.getUserName(); String url = md.getURL(); boolean isOracleOrDb2 = url.startsWith("jdbc:oracle") || url.startsWith("jdbc:db2"); ResultSet rsTables = md.getTables(conn.getCatalog(), isOracleOrDb2 ? userName : null, null, null); LOGGER.info("Getting the tables metadata"); if (rsTables != null && rsTables.next()) { LOGGER.info("Find TSITE table..."); while (rsTables.next()) { String tableName = rsTables.getString("TABLE_NAME"); String tablenameUpperCase = tableName.toUpperCase(); if ("TSITE".equals(tablenameUpperCase)) { LOGGER.info("TSITE table found"); hasTables = true; break; } else { if (tablenameUpperCase.endsWith("TSITE")) { LOGGER.info(tablenameUpperCase + " table found"); hasTables = true; break; } } } } } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e)); } if (!hasTables) { Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT OBJECTID FROM TSITE"); if (rs.next()) { hasTables = true; } } catch (SQLException e) { LOGGER.info("Table TSITE does not exist"); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { } } } } return hasTables; }
From source file:com.aurel.track.admin.server.status.ServerStatusBL.java
private static void loadDatabaseInfo(ServerStatusTO serverStatusTO) { Connection conn = null;//from w ww. jav a 2s .c o m 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:net.bull.javamelody.internal.model.JavaInformations.java
private static void appendDataBaseVersion(StringBuilder result, Connection connection) throws SQLException { final DatabaseMetaData metaData = connection.getMetaData(); // Scurit: pour l'instant on n'indique pas metaData.getUserName() result.append(metaData.getURL()).append('\n'); result.append(metaData.getDatabaseProductName()).append(", ").append(metaData.getDatabaseProductVersion()) .append('\n'); result.append("Driver JDBC:\n").append(metaData.getDriverName()).append(", ") .append(metaData.getDriverVersion()); }