KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jerpa > component > mysql > MySQLComponent


1 package org.jerpa.component.mysql;
2
3 /*
4  * JERPA - Java Enterprise Resource Planning Architecture
5  * Copyright (c) 2002 JERPA-Team <info@jerpa.org>
6  * This is GNU software. Look at COPYING for details.
7  */

8
9 import java.sql.*;
10 import org.jerpa.component.*;
11 import org.jerpa.component.configuration.*;
12 import org.jerpa.component.logger.*;
13 import org.jerpa.util.*;
14 import org.jerpa.component.api.*;
15
16 public class MySQLComponent extends Component
17     implements SQLConnector
18 {
19     LoggerComponent logger = null;
20     
21     Connection connection = null;
22     
23     public MySQLComponent()
24     {
25         setName("MySQL Connector Component");
26         setRole("org.jerpa.component.SQLConnector");
27         setDesc("This is a connector for the MySQL database.");
28         setAuthor("Michael Kleinhenz");
29         setCopyright("The JERPA Team. This is GNU Software.");
30         setVersion(new Version(0,0,1));
31         setServiceable(new Boolean JavaDoc(true));
32
33         // Set default keys and values
34
configuration.addStringItem(new StringConfigurationItem("host","localhost"));
35         configuration.addStringItem(new StringConfigurationItem("database","jerpa"));
36         configuration.addStringItem(new StringConfigurationItem("user","dbuser"));
37         configuration.addStringItem(new StringConfigurationItem("password","secret"));
38     };
39
40     public void init(ComponentManager _manager)
41     {
42         componentManager = _manager;
43         // put dependencies on other components here
44
// NOT IN THE CONSTRUCTOR!!
45
try
46         {
47             logger = (LoggerComponent)componentManager.lookup("org.jerpa.component.Logger");
48         }
49         catch (ComponentException e)
50         {
51             System.out.println(e.getMessage());
52         };
53
54         // Put any more inits here.
55
logger.log(this,"Init called.");
56
57         // Load JDBC driver class.
58
try
59         {
60             Class.forName("org.gjt.mm.mysql.Driver").newInstance();
61         }
62         catch (Exception JavaDoc e)
63         {
64             logger.log(this,"Can't load database driver: " + e.getMessage());
65             return;
66         }
67         
68         // Establish connection.
69
try
70         {
71             String JavaDoc url = "jdbc:mysql"
72                 + "://" + configuration.getStringItem("host").getValue()
73                 + "/" + configuration.getStringItem("database").getValue()
74                 + "?user=" + configuration.getStringItem("user").getValue()
75                 + "&password=" + configuration.getStringItem("password").getValue();
76         
77             connection = DriverManager.getConnection(url);
78         }
79         catch (ConfigurationException e)
80         {
81             logger.log(this,"Can't load establish connection: " + e.getMessage());
82             return;
83         }
84         catch (SQLException e)
85         {
86             logger.log(this,"Can't load establish connection: " + e.getMessage());
87             return;
88         }
89     };
90     
91     public void finalize()
92     {
93         // put any finalizings here.
94
logger.log(this,"Finalize called.");
95         // Close connection.
96
try
97         {
98             connection.close();
99         }
100         catch (SQLException e)
101         {
102             logger.log(this,"Can't close connection: " + e.getMessage());
103         }
104     };
105
106     public Configuration getConfiguration()
107     {
108         return configuration;
109     };
110     
111     public void query(String JavaDoc _query)
112     {
113         logger.log(this,"Query called: " + _query);
114     };
115 };
116
117
118
Popular Tags