WhatsApp example PHP et SQL

Apache NetBeans 12.3

The most efficient way to implement communication between the server and database is to set up a database connection pool. Creating a new connection for each client request can be very time consuming, especially for applications that continuously receive a large number of requests. To remedy this, numerous connections are created and maintained in a connection pool. Any incoming requests that require access to the application’s data layer use an already-created connection from the pool. Likewise, when a request is completed, the connection is not closed down, but returned to the pool.

After preparing the data source and connection pool for the server, you then need to instruct the application to use the data source. This is typically done by creating an entry in the application’s deployment descriptor. Finally, you need to ensure that the database driver (MySQL Connector / J JDBC Driver) is accessible to the server.

* Important: * From this point forward, you need you ensure that you have a MySQL database instance named set up that contains sample data provided in ifpwafcad.sql. This SQL file creates two tables, and, then populates them with sample data. If you have not already done this, or if you need help with this task, see Connecting to a MySQL Database before proceeding further.

Also, your database needs to be password-protected to create a data source and work with the GlassFish server in this tutorial. If you are using the default MySQL account with an empty password, you can set the password from a command-line prompt.

This tutorial uses as an example password. To set your password to, navigate to your MySQL installation’s directory in the command-line prompt and enter the following:

Setting up a JDBC data source and connection pool

The GlassFish Server Open Source Edition contains Database Connection Pooling (DBCP) libraries that provide connection pooling functionality in a way that is transparent to you as a developer. To take advantage of this, you need to configure a JDBC (Java Database Connectivity) data source for the server which your application can use for connection pooling.

You could configure the data source directly within the GlassFish server Admin Console, or, as described below, you can declare the resources that your application needs in a file. When the application is deployed, the server reads in the resource declarations, and creates the necessary resources.

The following steps demonstrate how to declare a connection pool, and a data source that relies on the connection pool. The NetBeans JDBC Resource wizard allows you to perform both actions.

  1. Open the New File wizard by pressing the New File ( ) button in the IDE’s main toolbar. Select the GlassFish server category, then select JDBC Resource and click Next.

  2. In step 2, General Attributes, choose the Create New JDBC Connection Pool option, then in the JNDI Name text field, type in jdbc / IFPWAFCAD.

Figure 11. Specify data source settings in the JDBC Resource wizard

The JDBC data source relies on JNDI, the Java Naming and Directory Interface. The JNDI API provides a uniform way for applications to find and access data sources. For more information, see The JNDI Tutorial.

  1. Optionally, add a description for the data source. For example, type in:.

  2. Click Next, then click Next again to skip step 3, Additional Properties.

  3. In Step 4, type in IfpwafcadPool for JDBC Connection Pool Name. Make sure the Extract from Existing Connection option is selected, and choose from the drop-down list. Click Next.

Figure 12. Specify connection pool settings in the JDBC Resource wizard

* Note: * The wizard detects any database connections that have been set up in the IDE. Therefore, you need to have already created a connection to the database at this point. You can verify what connections have been created by opening the Services window (Ctrl-5; ⌘-5 on Mac) and looking for connection nodes ( ) under the Databases category.

  1. In Step 5, select in the Resource Type drop-down list.

Note that the IDE extracts information from the database connection you specified in the previous step, and sets name-value properties for the new connection pool.

Figure 13. Default values ​​are based on information extracted from the selected database connection
  1. Click finish. The wizard generates a file that contains entries for the data source and connection pool you specified.

In the Projects window, you can open the file that was created under the Server Resources node and note that, within the tags, a data source and connection pool have been declared containing the values ​​you previously specified.

To confirm that a new data source and connection pool are indeed registered with the GlassFish server, you can deploy the project to the server, then locate the resources in the IDE’s Services window:

  1. In the Projects window, right-click the IFPWAFCAD project node and choose Deploy. The server starts up if not already running, and the project is compiled and deployed to it.

  2. Open the Services window (Ctrl-5; ⌘-5 on Mac) and expand the Servers> GlassFish> Resources> JDBC> JDBC Resources and Connection Pools nodes. Note that the new data source and connection pool are now displayed:

Figure 14. New data source and connection pool displayed in Services window

Referencing the data source from the application

You need to reference the JDBC resource you just configured from the web application. To do so, you can create an entry in the application’s deployment descriptor.

Deployment descriptors are XML-based text files that contain information describing how an application is to be deployed to a specific environment. For example, they are normally used to specify application context parameters and behavioral patterns, security settings, as well as mappings for servlets, filters and listeners.

Grade. If you specified Java EE 6 or Java EE 7 as the Java version when you created the project, you need to create the deployment descriptor file by choosing Web> Standard Deployment Descriptor in the New File wizard.

Perform the following steps to reference the data source in the application’s deployment descriptor.

  1. In the Projects window, expand the Configuration Files folder and double-click to open the file in the editor.

  2. Click the References tab located along the top of the editor.

  3. Expand the Resource References heading and click Add to open the Add Resource Reference dialog.

  4. For Resource Name, enter the resource name that you gave when configuring the data source for the server above ().

  5. Type in the Resource Type field. Click OK.

The Description field is optional, but you can enter a human-readable description of the resource, e.g.,.

Figure 15. Specify resource properties in the Add Resource Reference dialog

The new resource is now listed under the Resource References heading.

  1. To verify that the resource is now added to the file, click the Source tab located along the top of the editor. Notice that the following <`resource-ref`> tags are now included.

Adding the database driver’s JAR file to the server

Adding the database driver’s JAR file is another step that is vital to enabling the server to communicate with your database. Ordinarily, you would need to locate your database driver’s installation directory and copy the file from the driver’s root directory into the library folder of the server you are using. Fortunately, the IDE’s server management is able to detect at deployment whether the JAR file has been added - and if not, it does so automatically.

In order to demonstrate this, open the Servers manager (Choose Tools> Servers). The IDE provides a JDBC driver deployment option. If the option is enabled, it initiates a check to determine whether any drivers are required for the server’s deployed applications. In the case of MySQL, if the driver is required and it is missing, the IDE’s bundled driver is deployed to the appropriate location on the server.

  1. Choose Tools> Servers to open the Servers manager. Select the GlassFish server in the left pane.

  2. In the main pane, select the Enable JDBC Driver Deployment option.

Figure 16. JDBC Driver Deployment option enables automatic driver deployment
  1. Before you close the Servers manager, make a note of the path indicated in the Domains folder text field. When you connect to the GlassFish server in the IDE, you are actually connecting to an instance of the application server. Each instance runs applications in a unique domain, and the Domain Name field indicates the name of the domain your server is using. As shown in the image above, the driver JAR file should be located within, which is the default domain created upon installing the GlassFish server.

  2. Click Close to exit the Servers manager.

  3. On your computer, navigate to the GlassFish server installation directory and drill into the>> subfolder. Because you should have already deployed the IFPWAFCAD project to the server, you should see the file. If you do not see the driver JAR file, perform the following step.

  4. Deploy your project to the server. In the IDE’s Projects window, choose Deploy from the right-click menu of the project node. You can view progress in the IDE’s Output window (Ctrl-4; ⌘-4 on Mac). The output indicates that the MySQL driver is deployed to a location in the GlassFish server.

Figure 17. Output window indicates that the MySQL driver has been deployed

Now, if you return to the subfolder on your computer, you can see that the file has been automatically added.