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)
 36           Class.forName(driver);
 37     }
 38  
 39     /**
 40        Gets a connection to the database.
 41        @return the database connection
 42     */
 43     public static Connection getConnection() throws SQLException
 44     {
 45        return DriverManager.getConnection(url, username, password);
 46     }
 47  }
 48  
 49  
 50  
 51  
 52  
 53  
 54  
 55  
 56  
 57  
 58