Data Access Object

Objectives

We will show you how to use JDBC directly from within an EJB.

Datasource

Once you have declared your JDBC datasource for your application server, you just have to retrieve it through JNDI via a simple lookup:

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:/Jyperion/MySqlDS");
Then, to get the connection back:

Connection connection = dataSource.getConnection();
Here you go! You can now start to execute SQL statements within EJBs or within POJOs.

Exercise

Using a simple table of your choice, develop a POJO that uses a connection obtained from the DataSource in order to insert, select, delete elements in/from/in the table.