KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > jdbc > PMJdbcImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.ws.jaxme.pm.jdbc;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.Driver JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import javax.xml.bind.JAXBException;
25
26 import org.apache.ws.jaxme.JMManager;
27 import org.apache.ws.jaxme.pm.impl.PMImpl;
28 import org.apache.ws.jaxme.util.ClassLoader;
29
30 /**
31  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
32  */

33 public abstract class PMJdbcImpl extends PMImpl {
34   private String JavaDoc driver, url, user, password;
35
36   /** <p>Creates a new instance of PMJdbcImpl.</p>
37    */

38   public PMJdbcImpl() {
39     super();
40   }
41
42   public void init(JMManager pManager) throws JAXBException {
43     super.init(pManager);
44     String JavaDoc myDriver = pManager.getProperty("jdbc.driver");
45     if (myDriver == null || myDriver.length() == 0) {
46       throw new JAXBException("Missing property: 'jdbc.driver' (JDBC driver class)");
47     }
48     driver = myDriver;
49     try {
50       ClassLoader.getClass(myDriver, Driver JavaDoc.class);
51     } catch (ClassNotFoundException JavaDoc e) {
52       throw new JAXBException("Unable to load driver class " + myDriver);
53     }
54     String JavaDoc myUrl = pManager.getProperty("jdbc.url");
55     if (myUrl == null || myUrl.length() == 0) {
56       throw new JAXBException("Missing property: 'jdbc.url' (JDBC database URL)");
57     }
58     url = myUrl;
59
60     user = pManager.getProperty("jdbc.user");
61
62     password = pManager.getProperty("jdbc.password");
63   }
64
65   /** Returns the configured JDBC driver.
66    */

67   public String JavaDoc getJdbcDriver() {
68     return driver;
69   }
70
71   /** Returns the configured JDBC URL.
72    */

73   public String JavaDoc getJdbcUrl() {
74     return url;
75   }
76
77   /** Returns the configured JDBC user.
78    */

79   public String JavaDoc getJdbcUser() {
80     return user;
81   }
82
83   /** Returns the configured JDBC password.
84    */

85   public String JavaDoc getJdbcPassword() {
86     return password;
87   }
88
89   /** Creates a new database connection.
90    */

91   public Connection JavaDoc getConnection() throws SQLException JavaDoc {
92     return DriverManager.getConnection(getJdbcUrl(), getJdbcUser(), getJdbcPassword());
93   }
94 }
95
Popular Tags