List of usage examples for java.sql Driver getPropertyInfo
DriverPropertyInfo[] getPropertyInfo(String url, java.util.Properties info) throws SQLException;
From source file:Main.java
public static void main(String[] argv) throws Exception { String driverName = "org.gjt.mm.mysql.Driver"; Class.forName(driverName);//from www .j a v a 2 s .c o m String url = "jdbc:mysql://a/b"; Driver driver = DriverManager.getDriver(url); DriverPropertyInfo[] info = driver.getPropertyInfo(url, null); for (int i = 0; i < info.length; i++) { String name = info[i].name; boolean isRequired = info[i].required; String value = info[i].value; String desc = info[i].description; String[] choices = info[i].choices; } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.hsqldb.jdbcDriver"); String url = "jdbc:hsqldb:mem:data/tutorial"; Driver driver = DriverManager.getDriver(url); DriverPropertyInfo[] info = driver.getPropertyInfo(url, null); for (int i = 0; i < info.length; i++) { System.out.println(info[i].name); // Is property value required? System.out.println(info[i].required); // Get current value System.out.println(info[i].value); // Get description of property System.out.println(info[i].description); // Get possible choices for property; // if null, value can be any string String[] choices = info[i].choices; if (choices != null) { for (int c = 0; c < choices.length; c++) { System.out.println(choices[c]); }/*from w ww . j a v a2 s . com*/ } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.gjt.mm.mysql.Driver"); Properties info = new Properties(); Driver driver = DriverManager.getDriver("jdbc:mysql://localhost/demo2s"); System.out.println("driver=" + driver); DriverPropertyInfo[] attributes = driver.getPropertyInfo("jdbc:mysql://localhost/demo2s", info); System.out.println("attributes=" + attributes); // zero length means a connection attempt can be made System.out.println("Resolving properties for: " + driver.getClass().getName()); for (int i = 0; i < attributes.length; i++) { // get the property metadata String name = attributes[i].name; String[] choices = attributes[i].choices; boolean required = attributes[i].required; String description = attributes[i].description; // printout property metadata System.out.println(name + " (Required: " + required + ")"); if (choices == null) { System.out.println(" No choices."); } else {/*from w ww . ja v a2 s . c om*/ System.out.print(" Choices are: "); for (int j = 0; j < choices.length; j++) { System.out.print(" " + choices[j]); } } System.out.println(" Description: " + description); } }
From source file:TerminalMonitor.java
static public void main(String args[]) { DriverPropertyInfo[] required; StringBuffer buffer = new StringBuffer(); Properties props = new Properties(); boolean connected = false; Driver driver; String url;//from ww w .j a v a2 s . co m int line = 1; // Mark current input line if (args.length < 1) { System.out.println("Syntax: <java -Djdbc.drivers=DRIVER_NAME " + "TerminalMonitor JDBC_URL>"); return; } url = args[0]; // We have to get a reference to the driver so we can // find out what values to prompt the user for in order // to make a connection. try { driver = DriverManager.getDriver(url); } catch (SQLException e) { e.printStackTrace(); System.err.println("Unable to find a driver for the specified " + "URL."); System.err.println("Make sure you passed the jdbc.drivers " + "property on the command line to specify " + "the driver to be used."); return; } try { required = driver.getPropertyInfo(url, props); } catch (SQLException e) { e.printStackTrace(); System.err.println("Unable to get driver property information."); return; } input = new BufferedReader(new InputStreamReader(System.in)); // some drivers do not implement this properly // if that is the case, prompt for user name and password try { if (required.length < 1) { props.put("user", prompt("user: ")); props.put("password", prompt("password: ")); } else { // for each required attribute in the driver property info // prompt the user for the value for (int i = 0; i < required.length; i++) { if (!required[i].required) { continue; } props.put(required[i].name, prompt(required[i].name + ": ")); } } } catch (IOException e) { e.printStackTrace(); System.err.println("Unable to read property info."); return; } // Make the connection. try { connection = DriverManager.getConnection(url, props); } catch (SQLException e) { e.printStackTrace(); System.err.println("Unable to connect to the database."); } connected = true; System.out.println("Connected to " + url); // Enter into a user input loop while (connected) { String tmp, cmd; // Print a prompt if (line == 1) { System.out.print("TM > "); } else { System.out.print(line + " -> "); } System.out.flush(); // Get the next line of input try { tmp = input.readLine(); } catch (java.io.IOException e) { e.printStackTrace(); return; } // Get rid of extra space in the command cmd = tmp.trim(); // The user wants to commit pending transactions if (cmd.equals("commit")) { try { connection.commit(); System.out.println("Commit successful."); } catch (SQLException e) { System.out.println("Error in commit: " + e.getMessage()); } buffer = new StringBuffer(); line = 1; } // The user wants to execute the current buffer else if (cmd.equals("go")) { if (!buffer.equals("")) { try { executeStatement(buffer); } catch (SQLException e) { System.out.println(e.getMessage()); } } buffer = new StringBuffer(); line = 1; continue; } // The user wants to quit else if (cmd.equals("quit")) { connected = false; continue; } // The user wants to clear the current buffer else if (cmd.equals("reset")) { buffer = new StringBuffer(); line = 1; continue; } // The user wants to abort a pending transaction else if (cmd.equals("rollback")) { try { connection.rollback(); System.out.println("Rollback successful."); } catch (SQLException e) { System.out.println("An error occurred during rollback: " + e.getMessage()); } buffer = new StringBuffer(); line = 1; } // The user wants version info else if (cmd.startsWith("show")) { DatabaseMetaData meta; try { meta = connection.getMetaData(); cmd = cmd.substring(5, cmd.length()).trim(); if (cmd.equals("version")) { showVersion(meta); } else { System.out.println("show version"); // Bad arg } } catch (SQLException e) { System.out.println("Failed to load meta data: " + e.getMessage()); } buffer = new StringBuffer(); line = 1; } // The input that is not a keyword should appended be to the buffer else { buffer.append(" " + tmp); line++; continue; } } try { connection.close(); } catch (SQLException e) { System.out.println("Error closing connection: " + e.getMessage()); } System.out.println("Connection closed."); }
From source file:com.twosigma.beaker.sql.ConnectionStringHolder.java
protected static String getProperty(String property, String connectionString, Driver dbDriver) { String ret = null;//w w w.j a v a2 s . com if (property != null && !property.isEmpty() && dbDriver != null && connectionString != null && !connectionString.isEmpty()) { try { for (DriverPropertyInfo dpi : dbDriver.getPropertyInfo(connectionString, null)) { if (property.equalsIgnoreCase(dpi.name.trim())) { ret = dpi.value; break; } } } catch (SQLException e) { } } return ret; }
From source file:net.big_oh.common.jdbc.JdbcDriverProxy.java
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { Driver d = lookupDelegateDriver(url); return (d == null) ? new DriverPropertyInfo[0] : d.getPropertyInfo(url, info); }
From source file:org.sqsh.SQLDriverManager.java
/** * Attempts to connect to the database utilizing a driver. * /*from ww w .ja va 2 s. c om*/ * @param driver The name of the driver to utilize. The special * driver name "generic" can be provided if the manager does not * have a registered driver that can be used. * @param session The session for which the connection is being * established. The session is primarly used as a place to * send error messages. * @param properties Connection properties to be utilized. * @return A newly created connection. * @throws SQLException Thrown if the connection could not be * established. */ public SQLConnectionContext connect(Session session, ConnectionDescriptor connDesc) throws SQLException { SQLDriver sqlDriver = null; /* * If no driver was supplied, then set it to the default. */ if (connDesc.getDriver() == null) { connDesc.setDriver(defaultDriver); } /* * Make sure we are going to have enough to connect with here! */ if (connDesc.getDriver() == null && connDesc.getUrl() == null) { throw new SQLException("Either an explicit JSqsh driver name must be supplied " + "(via --driver) or a JDBC fully qualified JDBC url " + "must be provided (via --jdbc-url). In most cases the " + "JDBC url must also be accompanied with the " + "name of the JDBC driver class (via --jdbc-class"); } /* * We need to have a SQLDriver object because it makes our life * easier expanding variables. To allow for no driver being * supplied by the user, we will use one called "generic". */ if (connDesc.getDriver() == null) { connDesc.setDriver("generic"); } sqlDriver = getDriver(connDesc.getDriver()); if (sqlDriver == null) { throw new SQLException("JSqsh has no driver registered under " + "the name '" + connDesc.getDriver() + "'. To see a list of available drivers, use the " + "\\drivers command"); } /* * If the user asked for a JDBC driver class, then make sure * that we can load it. */ if (connDesc.getJdbcClass() != null) { try { Class<? extends Driver> driverClass = Class.forName(connDesc.getJdbcClass(), true, classLoader) .asSubclass(Driver.class); Driver d = driverClass.newInstance(); DriverManager.registerDriver(new DriverShim(d)); } catch (Exception e) { throw new SQLException( "Cannot load JDBC driver class '" + connDesc.getJdbcClass() + "': " + e.getMessage()); } } /* * The JDBC URL will either be one the one supplied by the user * or the one that is defined by the JDBC driver. */ String url = connDesc.getUrl(); if (url == null) { url = sqlDriver.getUrl(); } /* * Turn our connection descriptor into properties that can be * referenced while expanding the URL. */ Map<String, String> properties = toProperties(session, connDesc); /* * Expand the url of its variables. */ url = getUrl(session, properties, sqlDriver.getVariables(), url); Connection conn = null; try { Driver jdbcDriver = DriverManager.getDriver(url); /* * Similar to above, we'll iterate through the properties supported by * the driver and set them as necessary. */ Properties props = new Properties(); try { /* * One little trick we can do is to ask the driver for all * of the properties that it supports. If there so happens to * exist an variable associated with the driver that matches * that variable name, we will set it. */ DriverPropertyInfo[] supportedProperties = jdbcDriver.getPropertyInfo(url, null); for (int i = 0; i < supportedProperties.length; i++) { String name = supportedProperties[i].name; String value = getProperty(properties, sqlDriver.getVariables(), name); if (!SQLDriver.SERVER_PROPERTY.equals(name) && (value != null)) { LOG.fine("Setting connection property '" + name + "' to '" + value + "'"); props.put(name, value); } } } catch (Exception e) { session.err.println("WARNING: Failed to retrieve JDBC driver " + "supported connection property list (" + e.getMessage() + ")"); } /* * If the driver explicitly declares a property * we just blindly pass it in. */ for (String name : sqlDriver.getPropertyNames()) { props.put(name, sqlDriver.getProperty(name)); } /* * If the connection descriptor specified a set of properties to use * then use them too (wow, we have a lot of ways to get properties * to the driver!) */ Map<String, String> descProps = connDesc.getPropertiesMap(); if (descProps.size() > 0) { for (Entry<String, String> e : descProps.entrySet()) { props.put(e.getKey(), e.getValue()); } } String s = getProperty(properties, sqlDriver.getVariables(), SQLDriver.USER_PROPERTY); if (s == null) { if (defaultUsername == null) { s = System.getProperty("user.name"); } if (s == null) { s = promptInput(session, "Username", false); } connDesc.setUsername(s); } if (s != null) { props.put(SQLDriver.USER_PROPERTY, s); } s = getProperty(properties, sqlDriver.getVariables(), SQLDriver.PASSWORD_PROPERTY); if (s == null) { s = promptInput(session, "Password", true); } if (s != null) { props.put(SQLDriver.PASSWORD_PROPERTY, s); } conn = DriverManager.getConnection(url, props); SQLTools.printWarnings(session, conn); } catch (SQLException e) { throw new SQLException("Unable to connect via JDBC url '" + url + "': " + e.getMessage(), e.getSQLState(), e.getErrorCode(), e); } String database = getProperty(properties, sqlDriver.getVariables(), SQLDriver.DATABASE_PROPERTY); if (database == null && defaultDatabase != null) { database = defaultDatabase; } if (database != null) { /* try { conn.setCatalog(database); SQLTools.printWarnings(session, conn); } catch (SQLException e) { session.err.println("WARNING: Could not switch database context" + " to '" + database + "': " + e.getMessage()); } */ } try { conn.setAutoCommit(defaultAutoCommit); SQLTools.printWarnings(session, conn); } catch (SQLException e) { session.err.println("WARNING: Unable to set auto-commit mode to " + defaultAutoCommit); } /* * AWFUL AWFUL HACK!!! * In a second we will transfer variables defined by the * driver via the SessionVariable setting. However, often * these variables will be setting information in the ConnectionContext * that belongs to the session -- which is likely the one we are * about to return, but haven't yet. This hack temporarily * stuffs it into the session so it can get set, then pulls it * back out. */ ConnectionContext oldContext = session.getConnectionContext(); SQLConnectionContext newContext = new SQLConnectionContext(session, connDesc, conn, url, sqlDriver.getAnalyzer(), sqlDriver.getNormalizer(), sqlDriver.getCurrentSchemaQuery()); session.setConnectionContext(newContext, false); try { /* * Now that we have our connection established, set session * variables that have been requested by the driver. */ Iterator<String> varIter = sqlDriver.getSessionVariables().keySet().iterator(); while (varIter.hasNext()) { String name = varIter.next(); session.setVariable(name, sqlDriver.getSessionVariable(name)); } } finally { session.setConnectionContext(oldContext, false); } return newContext; }