package search; import java.io.IOException; import java.util.Properties; import chemaxon.jchem.db.SettingsHandler; import chemaxon.util.ConnectionHandler; /** * Example codes for handling database connections. * * Shows two ways of setting connection handler parameters * (JDBC driver, database url, property table name, * database user name, password). * * The first method fills parameters with default values * (default values can be entered by user as well), * and also saves these values in a configuration file * that contains user settings. * * Second method loads connection parameters from * the above mentioned configuration file. * * Default place of database settings: * WINDOWS: C:\Documents and Settings\%USER_HOME%\chemaxon\.jchem * UNIX/LINUX: ~/.chemaxon/.jchem * * @author Tamas Csizmazia * @version 5.0.3, 04/26/2008 * */ public class Connection { private static String driver = "org.gjt.mm.mysql.Driver"; private static String url = "jdbc:mysql://localhost/test"; private static String propTableName = "jchemproperties"; private static String userName = "scott"; private static String password = "tiger"; /** * Strings are used to set connection parameters. * Parameters are saved in configuration file. * * @param ch connection handler to set * @throws IOException: Settings cannot be loaded from file */ public static void setConnectionPropertiesUsingDefaultParameters( ConnectionHandler ch) throws IOException { // Setting examples can be found at // http://chemaxon.com/jchem/FAQ.html#dburl // e.g. connection.jdbcDriver=org.gjt.mm.mysql.Driver ch.setDriver(driver); ch.setUrl(url); ch.setPropertyTable(propTableName); ch.setLoginName(userName); ch.setPassword(password); // Settings can be saved as well in .jchem file Properties props = new SettingsHandler().getSettings(); ch.storeValuesToProperties(props); } /** * Parameters stored in configuration file are used to set * connection parameters. * @param ch connection handler to set * @throws IOException: JDBC driver or database url is missing */ public static void setConnectionPropertiesFromUserSettings( ConnectionHandler ch) throws IOException{ Properties props = new SettingsHandler().getSettings(); if (!ch.loadValuesFromProperties(props)) { // If propertyTableName is null, uses "JChemProperties" // Throws exception only when driver or url is null throw new IOException("Insufficient connection data" + "(JDBC driver or database url is missing)."); } } /** * Returns an open database connection. * Gets the connection parameters from * stored jchem settings. * * @param methodType Two types of origin of connection parameters<br> * 1 - parameters are "hard-coded" (or from user input)<br> * 2 - parameters are in .jchem file * * @throws IOException: can have several reasons that occur * during connecting to database: * database error; cannot find database handler class; * insufficient permissions; ... * * @return the open connection */ public static ConnectionHandler getConnectionHandler( int methodType) throws IOException { try { ConnectionHandler ch = new ConnectionHandler(); if (methodType == 1) { setConnectionPropertiesUsingDefaultParameters(ch); } else { setConnectionPropertiesFromUserSettings(ch); } ch.connect(); return ch; } catch (Exception e) { // Handling exceptions possibly caused // by ch.connect() throw new IOException(e.getMessage()); } } }