1  import java.sql.Connection;
  2  import java.sql.DriverManager;
  3  import java.sql.SQLException;
  4  import java.io.FileInputStream;
  5  import java.io.IOException;
  6  import java.util.Properties;
  7  
  8  /**
  9     A simple data source for getting database connections. 
 10  */
 11  public class SimpleDataSource
 12  {
 13     private static String url;
 14     private static String username;
 15     private static String password;
 16  
 17     /**
 18        Initializes the data source.
 19        @param fileName the name of the property file that 
 20        contains the database driver, URL, username, and password
 21     */
 22     public static void init(String fileName)
 23           throws IOException, ClassNotFoundException
 24     {  
 25        Properties props = new Properties();
 26        FileInputStream in = new FileInputStream(fileName);
 27        props.load(in);
 28  
 29        String driver = props.getProperty("jdbc.driver");
 30        url = props.getProperty("jdbc.url");
 31        username = props.getProperty("jdbc.username");
 32        if (username == null) { username = ""; }
 33        password = props.getProperty("jdbc.password");
 34        if (password == null) { password = ""; }
 35        if (driver != null) { Class.forName(driver); }
 36     }
 37  
 38     /**
 39        Gets a connection to the database.
 40        @return the database connection
 41     */
 42     public static Connection getConnection() throws SQLException
 43     {
 44        return DriverManager.getConnection(url, username, password);
 45     }
 46  }