List of usage examples for java.sql Connection getMetaData
DatabaseMetaData getMetaData() throws SQLException;
DatabaseMetaData
object that contains metadata about the database to which this Connection
object represents a connection. From source file:PrimaryKeysSuppliers.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String createString = "create table SUPPLIERSPK " + "(SUP_ID INTEGER NOT NULL, " + "SUP_NAME VARCHAR(40), " + "STREET VARCHAR(40), " + "CITY VARCHAR(20), " + "STATE CHAR(2), " + "ZIP CHAR(5), " + "primary key(SUP_ID))"; Statement stmt;/*from w w w . j a v a 2 s .c o m*/ try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); stmt.executeUpdate(createString); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getPrimaryKeys(null, null, "SUPPLIERSPK"); while (rs.next()) { String name = rs.getString("TABLE_NAME"); String columnName = rs.getString("COLUMN_NAME"); String keySeq = rs.getString("KEY_SEQ"); String pkName = rs.getString("PK_NAME"); System.out.println("table name : " + name); System.out.println("column name: " + columnName); System.out.println("sequence in key: " + keySeq); System.out.println("primary key name: " + pkName); System.out.println(""); } rs.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:ForeignKeysCoffees.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con; String createString = "create table COFFEESFK " + "(COF_NAME varchar(32) NOT NULL, " + "SUP_ID int, " + "PRICE float, " + "SALES int, " + "TOTAL int, " + "primary key(COF_NAME), " + "foreign key(SUP_ID) references SUPPLIERSPK)"; Statement stmt;/*w w w . j av a 2 s .c o m*/ try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(); stmt.executeUpdate(createString); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getImportedKeys(null, null, "COFFEESFK"); while (rs.next()) { String pkTable = rs.getString("PKTABLE_NAME"); String pkColName = rs.getString("PKCOLUMN_NAME"); String fkTable = rs.getString("FKTABLE_NAME"); String fkColName = rs.getString("FKCOLUMN_NAME"); short updateRule = rs.getShort("UPDATE_RULE"); short deleteRule = rs.getShort("DELETE_RULE"); System.out.println("primary key table name : " + pkTable); System.out.print("primary key column name : "); System.out.println(pkColName); System.out.println("foreign key table name : " + fkTable); System.out.print("foreign key column name : "); System.out.println(fkColName); System.out.println("update rule: " + updateRule); System.out.println("delete rule: " + deleteRule); System.out.println(""); } rs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } }
From source file:GetDBInfo.java
public static void main(String[] args) { Connection c = null; // The JDBC connection to the database server try {/*from ww w . j av a 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:TestSupportsTransactions.java
public static boolean supportsTransactions(Connection conn) throws SQLException { if (conn == null) { return false; }// w w w .j a v a2 s. c o m DatabaseMetaData dbMetaData = conn.getMetaData(); if (dbMetaData == null) { // metadata is not supported return false; } return dbMetaData.supportsTransactions(); }
From source file:com.financial.tools.recorderserver.tools.util.DataLoader.java
protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException { Connection connection = dataSource.getConnection(); String url = connection.getMetaData().getURL(); if (StringUtils.contains(url, ":h2:")) { return new H2Connection(connection, null); } else if (StringUtils.contains(url, ":mysql:")) { return new MySqlConnection(connection, null); } else if (StringUtils.contains(url, ":oracle:")) { return new OracleConnection(connection, null); } else {/*from w w w .j a v a 2s . c o m*/ return new DatabaseConnection(connection); } }
From source file:eu.databata.engine.util.PropagatorTableExport.java
private static void exportTablesData(JdbcTemplate jdbcTemplate, String delimiter, String tableNamePattern) { try {/*from w w w. j ava 2 s. c o m*/ System.out.println("Trying to load tables from metadata"); Connection connection = jdbcTemplate.getDataSource().getConnection(); ResultSet tables = connection.getMetaData().getTables(null, null, tableNamePattern, new String[] { "TABLE" }); while (tables.next()) { System.out.println("Exporting data from " + tables.getString("TABLE_NAME")); exportData(jdbcTemplate, tables.getString("TABLE_NAME"), delimiter); } } catch (SQLException e) { System.out.println("\nError when trying to get table names from DB."); } }
From source file:com.jt.leave.tools.DataFixtures.java
/** * DataSource?Connection(??)?url??Connection. *//* w w w . java2 s. c o m*/ protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException { Connection connection = dataSource.getConnection(); String jdbcUrl = connection.getMetaData().getURL(); if (StringUtils.contains(jdbcUrl, ":h2:")) { return new H2Connection(connection, null); } else if (StringUtils.contains(jdbcUrl, ":mysql:")) { return new MySqlConnection(connection, null); } else if (StringUtils.contains(jdbcUrl, ":oracle:")) { return new OracleConnection(connection, null); } else { return new DatabaseConnection(connection); } }
From source file:com.micmiu.modules.test.data.DataFixtures.java
/** * DataSource?Connection(??)?url??Connection. *///from w w w.j ava 2 s . c o m protected static IDatabaseConnection getConnection(DataSource dataSource) throws DatabaseUnitException, SQLException { Connection connection = dataSource.getConnection(); String dbName = connection.getMetaData().getURL(); if (StringUtils.contains(dbName, ":h2:")) { return new H2Connection(connection, null); } else if (StringUtils.contains(dbName, ":mysql:")) { return new MySqlConnection(connection, null); } else if (StringUtils.contains(dbName, ":oracle:")) { return new OracleConnection(connection, null); } else { return new DatabaseConnection(connection); } }
From source file:Main.java
public static void getTables(Connection conn) throws Exception { String TABLE_NAME = "TABLE_NAME"; String TABLE_SCHEMA = "TABLE_SCHEM"; String[] TABLE_TYPES = { "TABLE" }; DatabaseMetaData dbmd = conn.getMetaData(); ResultSet tables = dbmd.getTables(null, null, null, TABLE_TYPES); while (tables.next()) { System.out.println(tables.getString(TABLE_NAME)); System.out.println(tables.getString(TABLE_SCHEMA)); }//ww w.j a va2 s.c o m }
From source file:com.googlecode.flyway.core.dbsupport.DbSupportFactory.java
/** * Retrieves the name of the database product. * * @param connection The connection to use to query the database. * @return The name of the database product. Ex.: Oracle, MySQL, ... *///from w ww .j av a2 s . com private static String getDatabaseProductName(Connection connection) { try { DatabaseMetaData databaseMetaData = connection.getMetaData(); if (databaseMetaData == null) { throw new FlywayException("Unable to read database metadata while it is null!"); } return databaseMetaData.getDatabaseProductName(); } catch (SQLException e) { throw new FlywayException("Error while determining database product name", e); } }