“Creating and configuring MySQL Datasource in GlassFish Application Server”

Noor ul Huda

Step 1:

Copy  MySQL-connector-java-x.x.x-bin.jar in (\glassfish3\glassfishv3\glassfish\lib) folder.

Step2:

Open Eclipse, Create new Server , Select Glass fish 3 server and finish wizard.
Start Glass Fish Server , in admin console : 



COMMON TASKS> Resources> Connection Pools…
 Create new Connection Pool, Enter following Details:




Step 3:

Click on Next:
Now  there is step2 of creating connection Pool, In bottom of page, there is additional properties.
Create new property, username (enter your MySQL username) and its value.
Create new property password(enter your MySQL password) and its value.


Step 4:

Click on finish, now click connection pool which we create, there is an option of ping, click on it, if it return message “Ping Succeeded”, Ur connection pool created successfully.

Step 5:

Now in resources menu, there is JDBC resources option,  Create new JDBC resource, 
Enter following details there:
In JNDI name: Enter jdbc/(MySQL database name)
Pool name: MySQL

Click OK. Now JDBC resource created.

Step 6:

Create new Dynamic Web project in eclipse.
Project Name: JSF_Data Source_Application


Finish Dynamic Web Project Wizard. 

Step 7: 

Now, create new class Bank.java as back end bean for JSF page
Add this code:
public class Bank {

     
public int c_id;
public String c_name;
public String c_account;
public String c_nic;


     // Getters and Setters
      public int getC_id() {
            return c_id;
      }

      public void setC_id(int c_id) {
            this.c_id = c_id;
      }

      public String getC_name() {
            return c_name;
      }

      public void setC_name(String c_name) {
            this.c_name = c_name;
      }

      public String getC_account() {
            return c_account;
      }

      public void setC_account(String c_account) {
            this.c_account = c_account;
      }

      public String getC_nic() {
            return c_nic;
      }

      public void setC_nic(String c_nic) {
            this.c_nic = c_nic;
      }
     
}



Step 8:

Now create Class BankBean.java for accessing data source:

ManagedBean(name="bank")
@SessionScoped
public class bankbean implements Serializable{

//resource injection
@Resource(name="jdbc/dbstatebank")
private DataSource ds;

              //DataSource ds=null;
             //if resource inject is not support, you still can get it manually.

             public bankbean()
             {
             }
            //connect to DB and get customer list
            public List<Bank> getBankList() throws SQLException{
                        if(ds==null)
                                    throw new SQLException("Can't get data source .......");
                       
                        //get database connection
                        Connection con = ds.getConnection();
                       
                        if(con==null)
                                    throw new SQLException("Can't get database connection");
                       
                        PreparedStatement ps
                                    = con.prepareStatement(
                                                "select C_id, C_name, C_Accountno, C_NIC from tblaccount");
                       
                        //get customer data from database
                        ResultSet result =  ps.executeQuery();
                       
                        List<Bank> list = new ArrayList<Bank>();
                       
                        while(result.next()){
                                    Bank bank = new Bank();
                                    bank.setC_id(result.getInt("C_id"));
                                    bank.setC_name(result.getString("C_name"));
                                    bank.setC_account(result.getString("C_Accountno"));
                                    bank.setC_nic(result.getString("C_NIC"));
                                                                       
                                    //store all data into a List
                                    list.add(bank);
                        }
                                   
                        return list;
            }
}


Step 9:

Add context.xml file in META-INF folder of Webontent:




Context.xml:

<Context>
<Resource name="jdbc/dbstatebank" auth="Container" type="javax.sql.DataSource"
               maxActive="50" maxIdle="30" maxWait="10000"
               username="root" password="root"
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/dbstatebank"/>
</Context>

Step 10:

Now Add reference of context.xml in web.xml file:

  
      <!-- Datasource Refrence -->

      <resource-ref>
            <description>MySQL Datasource example</description>
            <res-ref-name>jdbc/dbstatebank</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
      </resource-ref>


Step 11:

Now Create index.xhtml page for displaying output from datasource, add this code and get output on web browser:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >
 <head>
 <link rel="StyleSheet" href="css/table-style.css" type="text/css" />
 </head>
 <h:body>

  <h1>DataSource And Connection Pooling in Glassfishv3</h1>
  <h:dataTable value="#{bank.bankList}" var="b"
                  styleClass="order-table"
                  headerClass="order-table-header"
                  rowClasses="order-table-odd-row,order-table-even-row" >

            <h:column>
                  <f:facet name="header">
                         ID
                  </f:facet>
                        #{b.c_id}
            </h:column>

            <h:column>
                  <f:facet name="header">
                        Name
                        </f:facet>
                        #{b.c_name}
            </h:column>

                  <h:column>
                  <f:facet name="header">
                        Account no
                        </f:facet>
                        #{b.c_account}
            </h:column>
           
            <h:column>
                  <f:facet name="header">
                        NIC
                        </f:facet>
                        #{b.c_nic}
            </h:column>
           
      </h:dataTable>
           
    </h:body>

</html>


Step 12:

Now Run this on server and get output Page on Web browser :


Now Try this tutorial and enjoy it…:)

Get database file from this link: Database.sql file
Tags

Post a Comment

2Comments
  1. Great Job. Keep posting good work :)

    ReplyDelete
  2. new blogger in town?? well best of luck... and nice work by the way

    ReplyDelete
Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !