1 Setting up a database connection
2 ================================
4 Most applications will need access to a database. In this tutorial, we'll connect to a database in order to pull data for displaying in a Google chart.
9 First, let's generate some fake data to display. Create an sql file and populate it with the following:
15 create table MOCK_DATA_AVG_SPEED (
20 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-01', 40, 'download');
21 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-02', 18, 'download');
22 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-03', 25, 'download');
23 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-04', 48, 'download');
24 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-05', 49, 'download');
25 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-06', 46, 'download');
26 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-07', 35, 'download');
27 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-01', 10, 'upload');
28 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-02', 15, 'upload');
29 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-03', 14, 'upload');
30 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-04', 9, 'upload');
31 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-05', 12, 'upload');
32 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-06', 13, 'upload');
33 insert into MOCK_DATA_AVG_SPEED (data_date, speedmbps, direction) values ('2017-08-07', 15, 'upload');
35 Now, run it. Something like this:
39 mysql -p<passwd> -u<user> < mock_data.sql
43 Setting up a connection in Java
44 -------------------------------
46 We'll need a place to store some data sources. In this case, we only need one, but your application might have more. Add the following member variable to your :code:`MyAppController.java` class:
50 private HashMap<String,DataSource> m_dataSources;
52 Don't forget to import the HashMap object:
56 import java.util.HashMap;
58 Now, we'll add a new private function, :code:`_getDataSources`:
62 private HashMap<String,DataSource> _getDataSources() throws Exception {
63 HashMap<String,DataSource> dataSources = new HashMap<String,DataSource>();
64 ComboPooledDataSource ds = new ComboPooledDataSource();
66 ds.setDriverClass(SystemProperties.getProperty("db.driver"));
67 ds.setJdbcUrl(SystemProperties.getProperty("db.connectionURL"));
68 ds.setUser(SystemProperties.getProperty("db.userName"));
69 ds.setPassword(SystemProperties.getProperty("db.password"));
70 ds.setMinPoolSize(Integer.parseInt(SystemProperties.getProperty(SystemProperties.DB_MIN_POOL_SIZE)));
71 ds.setMaxPoolSize(Integer.parseInt(SystemProperties.getProperty(SystemProperties.DB_MAX_POOL_SIZE)));
72 ds.setIdleConnectionTestPeriod(Integer.parseInt(SystemProperties.getProperty(SystemProperties.IDLE_CONNECTION_TEST_PERIOD)));
73 dataSources.put("myappdb", ds);
82 Notice that because we're piggy-backing our data to the ecomp_sdk database, we're borrowing a few properties as well. You can also add your own properties to :code:`sdk/ecomp-sdk/epsdk-app-os/src/main/webapp/WEB-INF/conf/system.properties` and use them. This allows you to create any number of connections and connection methods in your app. E.g.:
86 ds.setDriverClass(SystemProperties.getProperty("db.some_other_driver"));
88 Now, we need to add some code to our constructor so that the connection is set up when the controller is instantiated:
92 public MyAppController() {
95 this.m_dataSources = _getDataSources();
98 // Probably a good idea to do something here ;-)