Advertisement
Guest User

Untitled

a guest
Jun 17th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. package cs4347.jdbcProject.ecomm.testing;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.sql.Connection;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9.  
  10. import javax.sql.DataSource;
  11.  
  12. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  13.  
  14. /**
  15. * Application use the static method getDataSource() to obtain an open
  16. * connection to the MySQL server. The DBMS connection parameters (url, id, and
  17. * password) is maintained in a property file 'dbconfig.properties'. The
  18. * property file must be located on the application's CLASSPATH. See the
  19. * configuration property file is loaded by the method
  20. * getPropertiesFromClasspath().
  21. */
  22. public class DataSourceManager
  23. {
  24.  
  25. public synchronized static DataSource getDataSource() throws IOException
  26. {
  27.  
  28. MysqlDataSource ds = null;
  29. Properties props = getPropertiesFromClasspath();
  30. String url = props.getProperty("url");
  31. if (url == null || url.isEmpty()) {
  32. throw new RuntimeException("property 'url' not found in configuration file");
  33. }
  34. String id = props.getProperty("id");
  35. if (id == null || id.isEmpty()) {
  36. throw new RuntimeException("property 'id' not found in configuration file");
  37. }
  38. String passwd = props.getProperty("passwd");
  39. if (passwd == null || passwd.isEmpty()) {
  40. throw new RuntimeException("property 'passwd' not found in configuration file");
  41. }
  42.  
  43. ds = new MysqlDataSource();
  44. ds.setURL(url);
  45. ds.setUser(id);
  46. ds.setPassword(passwd);
  47. return ds;
  48. }
  49.  
  50. private static final String propFileName = "dbconfig.properties";
  51.  
  52. public static Properties getPropertiesFromClasspath() throws IOException
  53. {
  54. // Load dbconfig.properties from the classpath
  55. Properties props = new Properties();
  56. InputStream inputStream = DataSourceManager.class.getClassLoader().getResourceAsStream(propFileName);
  57.  
  58. if (inputStream == null) {
  59. throw new RuntimeException("property file '" + propFileName + "' not found in the classpath");
  60. }
  61.  
  62. props.load(inputStream);
  63.  
  64. return props;
  65. }
  66.  
  67. public static void main(String args[]) {
  68. try {
  69. DataSource ds = DataSourceManager.getDataSource();
  70. Connection con = ds.getConnection();
  71. Statement stat = con.createStatement();
  72. ResultSet rs = stat.executeQuery("select count(*) from simple_company.customer");
  73. if(rs.next()) {
  74. System.out.println("Count: " + rs.getInt(1));
  75. }
  76. System.out.println("Finished");
  77. }
  78. catch(Exception ex) {
  79. ex.printStackTrace();
  80. }
  81. }
  82.  
  83. }
  84.  
  85. /*
  86. dbconfig.properties
  87. url=jdbc:mysql://localhost:3306/simple_company
  88. id=root
  89. passwd=coolkid
  90.  
  91. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement