Coverage Report - net.mtu.eggplant.util.sql.SQLConnectionDialog
 
Classes in this File Line Coverage Branch Coverage Complexity
SQLConnectionDialog
0%
0/94
0%
0/2
3.667
 
 1  
 /*
 2  
  * Copyright (c) 2000
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.util.sql;
 29  
 
 30  
 import java.awt.GridBagConstraints;
 31  
 import java.awt.GridBagLayout;
 32  
 import java.sql.Connection;
 33  
 import java.sql.DriverManager;
 34  
 import java.sql.SQLException;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 37  
 
 38  
 import javax.swing.JComboBox;
 39  
 import javax.swing.JDialog;
 40  
 import javax.swing.JLabel;
 41  
 import javax.swing.JPasswordField;
 42  
 import javax.swing.JTextField;
 43  
 
 44  
 import org.slf4j.Logger;
 45  
 import org.slf4j.LoggerFactory;
 46  
 
 47  
 /**
 48  
  * This dialog allows the user to fill in the parameters needed for a
 49  
  * connection. It is hardcoded to use either the standard jdbc to odbc driver or
 50  
  * to use the rmi driver.
 51  
  * 
 52  
  * @version $Revision$
 53  
  */
 54  
 public class SQLConnectionDialog extends JDialog {
 55  
 
 56  0
   private static final Logger LOGGER = LoggerFactory.getLogger(SQLConnectionDialog.class);
 57  
 
 58  
   /**
 59  
    * Create a dialog for making an SQL connection.
 60  
    * 
 61  
    * @param parent the parent frame
 62  
    * @param drivers key is name of driver, value is classname of the driver
 63  
    */
 64  
   public SQLConnectionDialog(final java.awt.Frame parent,
 65  
                              final Map<String, String> drivers) {
 66  0
     super(parent, "Open JDBC Connection", true);
 67  0
     _drivers = new HashMap<String, String>(drivers);
 68  
 
 69  0
     getContentPane().setLayout(new GridBagLayout());
 70  
     GridBagConstraints gbc;
 71  0
     setSize(300, 150);
 72  
 
 73  0
     JLabel driverLabel = new JLabel("Driver: ");
 74  0
     gbc = new GridBagConstraints();
 75  0
     gbc.fill = GridBagConstraints.HORIZONTAL;
 76  0
     gbc.weightx = 1.0;
 77  0
     gbc.weighty = 0.0;
 78  0
     getContentPane().add(driverLabel, gbc);
 79  
 
 80  0
     _driverCombo = new JComboBox<>();
 81  
     // waiting for jdk1.2!
 82  
     // _driverModel = new DefaultComboBoxModel();
 83  0
     for (final String name : _drivers.keySet()) {
 84  0
       _driverCombo.addItem(name);
 85  0
     }
 86  
     /*
 87  
      * _driverCombo.addItem("Standard JDBC driver");
 88  
      * _driverCombo.addItem("RMI JDBC driver");
 89  
      * _driverCombo.setSelectedItem("Standard JDBC driver");
 90  
      */
 91  0
     _driverCombo.setSelectedIndex(0);
 92  
 
 93  
     // _driverCombo.setModel(_driverModel);
 94  
 
 95  0
     gbc = new GridBagConstraints();
 96  0
     gbc.fill = GridBagConstraints.NONE;
 97  0
     gbc.weightx = 0.0;
 98  0
     gbc.weighty = 0.0;
 99  0
     gbc.gridwidth = GridBagConstraints.REMAINDER;
 100  0
     getContentPane().add(_driverCombo, gbc);
 101  
 
 102  0
     JLabel dataSourceLabel = new JLabel("Data Source:");
 103  0
     gbc = new GridBagConstraints();
 104  0
     gbc.fill = GridBagConstraints.HORIZONTAL;
 105  0
     gbc.weightx = 1.0;
 106  0
     gbc.weighty = 0.0;
 107  0
     getContentPane().add(dataSourceLabel, gbc);
 108  
 
 109  0
     _dataSourceText = new JTextField(20);
 110  0
     gbc = new GridBagConstraints();
 111  0
     gbc.fill = GridBagConstraints.NONE;
 112  0
     gbc.weightx = 0.0;
 113  0
     gbc.weighty = 0.0;
 114  0
     gbc.gridwidth = GridBagConstraints.REMAINDER;
 115  0
     getContentPane().add(_dataSourceText, gbc);
 116  
 
 117  0
     JLabel userLabel = new JLabel("Username:");
 118  0
     gbc = new GridBagConstraints();
 119  0
     gbc.fill = GridBagConstraints.HORIZONTAL;
 120  0
     gbc.weightx = 1.0;
 121  0
     gbc.weighty = 0.0;
 122  0
     getContentPane().add(userLabel, gbc);
 123  
 
 124  0
     _userText = new JTextField(20);
 125  0
     gbc = new GridBagConstraints();
 126  0
     gbc.fill = GridBagConstraints.NONE;
 127  0
     gbc.weightx = 0.0;
 128  0
     gbc.weighty = 0.0;
 129  0
     gbc.gridwidth = GridBagConstraints.REMAINDER;
 130  0
     getContentPane().add(_userText, gbc);
 131  
 
 132  0
     JLabel passLabel = new JLabel("Password:");
 133  0
     gbc = new GridBagConstraints();
 134  0
     gbc.fill = GridBagConstraints.HORIZONTAL;
 135  0
     gbc.weightx = 1.0;
 136  0
     gbc.weighty = 0.0;
 137  0
     getContentPane().add(passLabel, gbc);
 138  
 
 139  0
     _passText = new JPasswordField(20);
 140  0
     gbc = new GridBagConstraints();
 141  0
     gbc.fill = GridBagConstraints.NONE;
 142  0
     gbc.weightx = 0.0;
 143  0
     gbc.weighty = 0.0;
 144  0
     gbc.gridwidth = GridBagConstraints.REMAINDER;
 145  0
     getContentPane().add(_passText, gbc);
 146  
 
 147  
     /*
 148  
      * OkCancelApplyPanel ocap = new OkCancelApplyPanel(this, true, true,
 149  
      * false); gbc = new GridBagConstraints(); gbc.gridwidth = gbc.REMAINDER;
 150  
      * gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = gbc.HORIZONTAL;
 151  
      * getContentPane().add(ocap, gbc);
 152  
      */
 153  0
   }
 154  
 
 155  
   /*
 156  
    * public void applyButtonClicked(ActionEvent ae) { depopulate(); } public
 157  
    * void okButtonClicked(ActionEvent ae) { depopulate(); setVisible(false); }
 158  
    * public void cancelButtonClicked(ActionEvent ae) { _connection = null;
 159  
    * setVisible(false); }
 160  
    */
 161  
 
 162  
   /**
 163  
    * pull the data out of the dialog and store it internally, then build the
 164  
    * connection object
 165  
    **/
 166  
   public void depopulate() {
 167  
     // add the driver to the driver manager.
 168  
 
 169  0
     String dataSource = _dataSourceText.getText();
 170  0
     String url = "jdbc:odbc:"
 171  
         + dataSource;
 172  0
     String pass = new String(_passText.getPassword());
 173  0
     String user = _userText.getText();
 174  0
     String connectString = null;
 175  0
     final String driverName = (String) _driverCombo.getSelectedItem();
 176  
 
 177  
     try {
 178  0
       Class.forName(_drivers.get(driverName)).newInstance();
 179  0
     } catch (final ClassNotFoundException e) {
 180  0
       LOGGER.error("Couldn't find driver: "
 181  0
           + _drivers.get(driverName));
 182  0
       _connection = null;
 183  0
       return;
 184  0
     } catch (final InstantiationException e) {
 185  0
       LOGGER.error("Couldn't find driver: "
 186  0
           + _drivers.get(driverName));
 187  0
       _connection = null;
 188  0
       return;
 189  0
     } catch (final IllegalAccessException e) {
 190  0
       LOGGER.error("Couldn't find driver: "
 191  0
           + _drivers.get(driverName));
 192  0
       _connection = null;
 193  0
       return;
 194  0
     }
 195  0
     connectString = url;
 196  
 
 197  
     // try {
 198  
     // Class.forName("RmiJdbc.RJDriver").newInstance();
 199  
     // } catch (final Exception e) {
 200  
     // LOGGER.debug("Couldn't find the driver");
 201  
     // _connection = null;
 202  
     // return;
 203  
     // }
 204  
     //
 205  
     // // get this from the dialog box
 206  
     // String rmiHost = "//129.235.65.128";
 207  
     // connectString = "jdbc:rmi:" + rmiHost + "/" + url;
 208  
     /*
 209  
      * generic connectString = jdbc:subprotocol:subname jdbc:???://host/db
 210  
      * examples subprotocol subname jdbc:rst://host/db postgre rst //host/db
 211  
      * jdbc:rmi://host/jdbc:odbc:db rmi rmi //host/jdbc:odbc:db jdbc:odbc:db sun
 212  
      * odbc db Driver.acceptsUrl(connectString);
 213  
      * DriverManager.getConnection(connectString, user, pass);
 214  
      */
 215  
 
 216  
     try {
 217  0
       _connection = DriverManager.getConnection(connectString, user, pass);
 218  0
     } catch (final SQLException sqle) {
 219  0
       LOGGER.debug("caught sql error opening connection: "
 220  
           + sqle);
 221  0
       _connection = null;
 222  0
     }
 223  0
   }
 224  
 
 225  
   /**
 226  
    * get the connection from this dialog.
 227  
    * 
 228  
    * @return the connection, <code>null</code> if there was an error
 229  
    **/
 230  
   public Connection getConnection() {
 231  0
     return _connection;
 232  
   }
 233  
 
 234  
   private final Map<String, String> _drivers;
 235  
 
 236  0
   private Connection _connection = null;
 237  
 
 238  
   private JTextField _userText;
 239  
 
 240  
   private JComboBox<String> _driverCombo;
 241  
 
 242  
   private JPasswordField _passText;
 243  
 
 244  
   private JTextField _dataSourceText;
 245  
 }