01: import java.sql.Connection;
02: import java.sql.DriverManager;
03: import java.sql.SQLException;
04: import java.io.FileInputStream;
05: import java.io.IOException;
06: import java.util.Properties;
07: 
08: /**
09:    A simple data source for getting database connections. 
10: */
11: public class SimpleDataSource
12: {
13:    /**
14:       Initializes the data source.
15:       @param fileName the name of the property file that 
16:       contains the database driver, url, username and password
17:     */
18:    public static void init(String fileName)
19:       throws IOException, ClassNotFoundException
20:    {  
21:       Properties props = new Properties();
22:       FileInputStream in = new FileInputStream(fileName);
23:       props.load(in);
24: 
25:       String driver = props.getProperty("jdbc.driver");
26:       url = props.getProperty("jdbc.url");
27:       String username = props.getProperty("jdbc.username");
28:       String password = props.getProperty("jdbc.password");
29: 
30:       Class.forName(driver);
31:    }
32: 
33:    /**
34:       Gets a connection to the database.
35:       @return the database connection
36:    */
37:    public static Connection getConnection() throws SQLException
38:    {
39:       return DriverManager.getConnection(url, 
40:          username, password);
41:    }
42: 
43:    private static String url;
44:    private static String username;
45:    private static String password;
46: }
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: