| ConnectionUtil.java |
1 package util;
2
3 import java.io.IOException;
4 import java.sql.SQLException;
5 import java.util.Properties;
6
7 import chemaxon.jchem.db.SettingsHandler;
8 import chemaxon.util.ConnectionHandler;
9
10/**
11 * Example codes for handling database connections.
12 * <p>
13 * This class shows two ways of setting database connection properties (JDBC driver, database
14 * URL, database user name, password). The first method fills properties according to the given
15 * parameters, while the second method loads these settings from the user configuration file
16 * (.jchem file).
17 * <p>
18 * Default location of JChem configuration file:
19 * <ul>
20 * <li>WINDOWS: %USERPROFILE%\chemaxon\.jchem
21 * <li>UNIX/LINUX: ~/.chemaxon/.jchem
22 * </ul>
23 * <p>
24 * Examples of connection settings can be found in the
25 * <a href="http://www.chemaxon.com/jchem/doc/admin/JChemBaseFAQ.html#dburl">JChemBase FAQ</a>.
26 *
27 * @author JChem Base team, ChemAxon Ltd.
28 */
29public final class ConnectionUtil {
30
31 /**
32 * Returns a connection handler using the specified parameters.
33 *
34 * @param driverClass class name of the database driver
35 * @param dbUrl URL of the database
36 * @param userName user name for the database
37 * @param password password for the database
38 * @return initialized connection handler
39 */
40 public static ConnectionHandler getConnectionHandler(String driverClass, String dbUrl,
41 String userName, String password) {
42
43 ConnectionHandler connHandler = new ConnectionHandler();
44 connHandler.setDriver(driverClass);
45 connHandler.setUrl(dbUrl);
46 connHandler.setLoginName(userName);
47 connHandler.setPassword(password);
48
49 // The name of the property table could also be changed:
50 // connHandler.setPropertyTable("MyPropertyTable");
51 // The default value is "JChemProperties".
52
53 return connHandler;
54 }
55
56 /**
57 * Returns a connection handler using properties defined in user settings (the .jchem
58 * configuration file).
59 *
60 * @return initialized connection handler
61 * @throws IOException if JDBC driver or database URL is missing in the user settings
62 */
63 public static ConnectionHandler getDefaultConnectionHandler() throws IOException {
64 ConnectionHandler connHandler = new ConnectionHandler();
65 Properties props = new SettingsHandler().getSettings();
66 if (!connHandler.loadValuesFromProperties(props)) {
67 // Throw exception only when driver or URL is null
68 throw new IOException("Insufficient connection data "
69 + "(JDBC driver or database URL is missing).");
70 }
71 return connHandler;
72 }
73
74 /**
75 * Saves the properties of the given connection handler to user settings (the .jchem
76 * configuration file).
77 *
78 * @param connHandler connection handler
79 * @throws IOException if the properties cannot be saved
80 */
81 public static void saveConnectionProperties(ConnectionHandler connHandler)
82 throws IOException {
83 Properties props = new Properties();
84 connHandler.storeValuesToProperties(props);
85 new SettingsHandler().save(props);
86 }
87
88 /**
89 * Connects to the database specified in the user settings (the .jchem configuration file).
90 *
91 * @return the established connection handler
92 * @throws IOException if an error occurs during database connection
93 */
94 public static ConnectionHandler connectToDB() throws IOException {
95 try {
96 ConnectionHandler connHandler = ConnectionUtil.getDefaultConnectionHandler();
97 connHandler.connectToDatabase();
98 System.out.println("Connection estabilished to " + connHandler.getUrl());
99 return connHandler;
00 } catch (Exception e) {
01 throw new IOException("Error connecting database", e);
02 }
03 }
04
05 /**
06 * Closes the connection represented by the given connection handler.
07 *
08 * @param connHandler connection handler
09 */
10 public static void closeConnection(ConnectionHandler connHandler) {
11 try {
12 connHandler.close();
13 System.out.println("Connection closed.");
14 } catch (SQLException e) {
15 System.err.println("Unable to close connection!");
16 e.printStackTrace();
17 }
18 }
19
20}
21