1 package org.devaki.nextobjects.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
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 }