In Chapter 4, Creating JEE Database Applications, we created the JDBC datasource locally in the application. In this chapter, we will create a JDBC datasource in GlassFish. GlassFish Server is not packaged with the JDBC driver for MySQL. So, we need to place the .jar file for MySQLDriver in the path where GlassFish can find it. You can place such external libraries in the lib/ext folder of the GlassFish domain in which you want to deploy your application. For this example, we will copy the JAR in <glassfish_home>/glassfish/domains/domain1/lib/ext.
If you do not have the MySQL JDBC driver, you can download it from http://dev.mysql.com/downloads/connector/j/:
- Open the GlassFish admin console, either by right-clicking on the server in the Servers view and selecting GlassFish | View Admin Console (this opens the admin console inside Eclipse) or browsing to http://localhost:4848 (4848 is the default port to which the GlassFish admin console application listens). In the admin console, select Resources | JDBC | JDBC Connection Pools. Click the New button on the JDBC Connection Pool page:
- Set Pool Name as MySQLconnectionPool and select javax.sql.DataSource as Resource Type. Select MySql from the Database Driver Vendor list and click Next. In the next page, select the correct Datasource Classname (com.mysql.jdbc.jdbc2.optional.MysqlDatasource):
- We need to set the hostname, port, username, and password of MySQL. In the admin page, scroll down to the Additional Properties section and set the following properties:
Properties |
Values |
Port/PortNumber |
3306 |
DatabaseName |
<schemaname_of_coursemanagement>, for example, course_management. See Chapter 4, Creating JEE Database Applications, for details on creating the MySQL schema for the Course Management database. |
Password |
MySQL database password. |
URL/Url |
jdbc:mysql://:3306/<database_name> , for example, jdbc:mysql://:3306/course_management |
ServerName |
localhost |
User |
MySQL username |
- Click Finish. The new connection pool is added to the list in the left pane. Click on the newly added connection pool. In the General tab, click on the Ping button and make sure that the ping is successful:
- Next, we need to create a JNDI resource for this connection pool so that it can be accessed from the client application. Select the Resources | JDBC | JDBC Resources node in the left pane. Click the New button to create a new JDBC resource:
- Set JNDI Name as jdbc/CourseManagement. From the Pool Name drop-down list, select the connection pool that we created for MySQL, MySQLconnectionPool. Click Save.