KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > db > DefaultDataSource


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

17 package org.apache.log.output.db;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24
25 /**
26  * A basic datasource that doesn't do any pooling but just wraps
27  * around default mechanisms.
28  *
29  * @author Peter Donald
30  */

31 public class DefaultDataSource
32     implements DataSource JavaDoc
33 {
34     private final String JavaDoc m_username;
35     private final String JavaDoc m_password;
36     private final String JavaDoc m_url;
37
38     private PrintWriter JavaDoc m_logWriter;
39     private int m_loginTimeout;
40
41     public DefaultDataSource( final String JavaDoc url,
42                               final String JavaDoc username,
43                               final String JavaDoc password )
44     {
45         m_url = url;
46         m_username = username;
47         m_password = password;
48
49         m_logWriter = new PrintWriter JavaDoc( System.err, true );
50     }
51
52     /**
53      * Attempt to establish a database connection.
54      *
55      * @return the Connection
56      */

57     public Connection JavaDoc getConnection()
58         throws SQLException JavaDoc
59     {
60         return getConnection( m_username, m_password );
61     }
62
63     /**
64      * Attempt to establish a database connection.
65      *
66      * @return the Connection
67      */

68     public Connection JavaDoc getConnection( final String JavaDoc username, final String JavaDoc password )
69         throws SQLException JavaDoc
70     {
71         return DriverManager.getConnection( m_url, username, password );
72     }
73
74     /**
75      * Gets the maximum time in seconds that this data source can wait while
76      * attempting to connect to a database.
77      *
78      * @return the login time
79      */

80     public int getLoginTimeout()
81         throws SQLException JavaDoc
82     {
83         return m_loginTimeout;
84     }
85
86     /**
87      * Get the log writer for this data source.
88      *
89      * @return the LogWriter
90      */

91     public PrintWriter JavaDoc getLogWriter()
92         throws SQLException JavaDoc
93     {
94         return m_logWriter;
95     }
96
97     /**
98      * Sets the maximum time in seconds that this data source will wait
99      * while attempting to connect to a database.
100      *
101      * @param loginTimeout the loging timeout in seconds
102      */

103     public void setLoginTimeout( final int loginTimeout )
104         throws SQLException JavaDoc
105     {
106         m_loginTimeout = loginTimeout;
107     }
108
109     public void setLogWriter( final PrintWriter JavaDoc logWriter )
110         throws SQLException JavaDoc
111     {
112         m_logWriter = logWriter;
113     }
114 }
115
Popular Tags