View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  import java.sql.Connection;
17  import java.sql.DriverManager;
18  import java.sql.SQLException;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.devaki.nextobjects.ui.components.CustomTextArea;
23  import org.devaki.nextobjects.ui.main.NOLog;
24  import org.devaki.nextobjects.workspace.models.BaseModel;
25  /***
26   * Test the jdbc connection
27   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
28  */
29  public class JDBCTest
30  {
31      /***
32       * the logger
33       */
34      private static Log logger =
35          LogFactory.getLog(DatabaseLoader.class.getName());
36          /***
37           * Test a connection for a given model
38           * @param pdm the model who give the connection
39           * @param info a textarea for output
40           */
41      public JDBCTest(final BaseModel pdm, final CustomTextArea info)
42      {
43          NOLog.showPane(0); // show log
44          String driverClass = pdm.getDatabaseDriver();
45          if (driverClass != null)
46          {
47              try
48              {
49                  Class.forName(driverClass);
50                  Connection connection =
51                      DriverManager.getConnection(
52                          pdm.getCreateDatabaseUrl(),
53                          pdm.getDatabaseUser(),
54                          pdm.getDatabasePassword());
55                  connection.setAutoCommit(false);
56                  if (connection != null)
57                  {
58                      String result =
59                          "Connection succed : "
60                              + connection.getMetaData().getDatabaseProductName()
61                              + connection
62                                  .getMetaData()
63                                  .getDatabaseProductVersion();
64                      logger.info(result);
65                      info.setText(result);
66                      connection.close();
67                  }
68              }
69              catch (SQLException ex)
70              {
71                  info.setText(ex.getMessage());
72                  logger.error(ex.getMessage());
73              }
74              catch (java.lang.ClassNotFoundException ex)
75              {
76                  info.setText(
77                      "No valid JDBC driver in classpath. Giving up :(.");
78                  logger.error("Driver error " + ex.getMessage());
79              }
80          }
81      }
82  }