Added tutorial for portal-sdk apps
[portal.git] / docs / tutorials / portal-sdk / setting-up-db.rst
1 Setting up a database connection
2 ================================
3
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.
5
6 Injecting data
7 --------------
8
9 First, let's generate some fake data to display. Create an sql file and populate it with the following:
10
11 .. code-block:: sql
12
13   use ecomp_sdk;
14
15   create table MOCK_DATA_AVG_SPEED (
16     data_date DATE,
17     speedmbps INT,
18     direction varchar(10)
19   );
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');
34
35 Now, run it. Something like this:
36
37 ::
38
39         mysql -p<passwd> -u<user> < mock_data.sql
40
41 .. _connectionjava:
42
43 Setting up a connection in Java
44 -------------------------------
45
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:
47
48 .. code-block:: java
49
50         private HashMap<String,DataSource> m_dataSources;
51
52 Don't forget to import the HashMap object:
53
54 .. code-block:: java
55
56         import java.util.HashMap;
57
58 Now, we'll add a new private function, :code:`_getDataSources`:
59
60 .. code-block:: java
61
62   private HashMap<String,DataSource> _getDataSources() throws Exception {
63     HashMap<String,DataSource> dataSources = new HashMap<String,DataSource>();
64     ComboPooledDataSource ds = new ComboPooledDataSource();
65     try {
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);
74     }
75     catch (Exception e) {
76       throw e;
77     }
78     
79     return dataSources;
80   }
81
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.:
83
84 .. code-block:: java
85
86         ds.setDriverClass(SystemProperties.getProperty("db.some_other_driver"));
87
88 Now, we need to add some code to our constructor so that the connection is set up when the controller is instantiated:
89
90 .. code-block:: java
91
92   public MyAppController() {
93     super();
94     try {
95       this.m_dataSources = _getDataSources();
96     }
97     catch (Exception e) {
98       // Probably a good idea to do something here ;-)
99     }
100   }
101
102