22 Oct 2016

Database Connectivity and Iterating Values in Drop-down List from Database (Building Java Web Application Part II)

Step 1: Creating connection class (DbConnection.java) in connection package

DbConnection.java code:

 package engdictionary.connection;  
   
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.SQLException;  
   
 public class DbConnection {  
      static Connection con;       
      public static Connection getConnection() throws ClassNotFoundException  
      {            
           try  
           {  
                Class.forName("com.mysql.jdbc.Driver");  
                con = DriverManager.getConnection("jdbc:mysql://localhost/db_english_dictionary","root","");  
           }  
           catch(SQLException e)  
           {  
           e.printStackTrace();       
           }  
      return con;  
      }  
 }  

Step 2: Creating model classes in model package for every relation (table) in the database

Step 2.1: Declare the variables corresponding to the fields in the relation. Note that every relation will have its own individual model class.


Step 2.2: Generate Getters and Setters of variables as shown below:


Select All the variables and press OK button.


Similarly create the model class for another relation in our database.

I have created TypeModel.java and WordModel.java model classes corresponding to tbl_type and tbl_word relations in the database db_english_dictionary, respectively. 

TypeModel.java code:

 package engdictionary.model;  
   
 public class TypeModel {  
 String type,abbr;  
   
 public String getType() {  
      return type;  
 }  
   
 public void setType(String type) {  
      this.type = type;  
 }  
   
 public String getAbbr() {  
      return abbr;  
 }  
   
 public void setAbbr(String abbr) {  
      this.abbr = abbr;  
 }  
 }  
   

WordModel.java code:

 package engdictionary.model;  
   
 public class WordModel {  
 String word,type,meaning1,meaning2,meaning3,comment;  
   
 public String getWord() {  
      return word;  
 }  
   
 public void setWord(String word) {  
      this.word = word;  
 }  
   
 public String getType() {  
      return type;  
 }  
   
 public void setType(String type) {  
      this.type = type;  
 }  
   
 public String getMeaning1() {  
      return meaning1;  
 }  
   
 public void setMeaning1(String meaning1) {  
      this.meaning1 = meaning1;  
 }  
   
 public String getMeaning2() {  
      return meaning2;  
 }  
   
 public void setMeaning2(String meaning2) {  
      this.meaning2 = meaning2;  
 }  
   
 public String getMeaning3() {  
      return meaning3;  
 }  
   
 public void setMeaning3(String meaning3) {  
      this.meaning3 = meaning3;  
 }  
   
 public String getComment() {  
      return comment;  
 }  
   
 public void setComment(String comment) {  
      this.comment = comment;  
 }  
   
 }  
   

Step 3: Creating type controller class (TypeController.java) in controller package

We are creating TypeController.java in order to iterate values of tbl_type into the following drop-down list. Controller classes comprises of the actual logic.


TypeController.java code:

 package engdictionary.controller;  
   
 import java.io.IOException;  
 import java.sql.Connection;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.sql.Statement;  
 import java.util.ArrayList;  
 import java.util.List;  
   
 import com.opensymphony.xwork2.ActionSupport;  
 import com.opensymphony.xwork2.ModelDriven;  
   
 import engdictionary.model.TypeModel;  
 import engdictionary.connection.DbConnection;  
   
 public class TypeController extends ActionSupport implements ModelDriven<TypeModel> {  
      TypeModel model = new TypeModel();  
      List<TypeModel> word_type = new ArrayList<TypeModel>();  
        
      public TypeModel getModel() {  
           return model;  
      }  
      public void setModel(TypeModel model) {  
           this.model = model;  
      }  
        
      public List<TypeModel> getWord_type() {  
           return word_type;  
      }  
      public void setWord_type(List<TypeModel> word_type) {  
           this.word_type = word_type;  
      }  
      public String viewType() throws ClassNotFoundException,IOException  
      {  
           try {            
                Connection con = DbConnection.getConnection();  
                Statement st;  
                  
                st = con.createStatement();  
                ResultSet rs = st.executeQuery("select * from tbl_type");  
                TypeModel model = null;  
                while(rs.next()){  
                     model = new TypeModel();  
                     model.setType(rs.getString("type"));  
                     model.setAbbr(rs.getString("abbr"));  
                     word_type.add(model);            
                }  
                setWord_type(word_type);            
           } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();       
           }  
           return SUCCESS;  
      }  
 }  
   

Step 4: Iterating the values

Step 4.1: Add the following lines at the top of add.jsp (page comprising the drop-down list).

 <%@ taglib uri="/struts-tags" prefix="s" %>  
   <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>  

Step 4.2: Update <select /> block with the following code:

 <select type="text" name="word_type" style="height:auto;" onfocus='this.size=8;' onblur='this.size=1;' onchange='this.size=1;this.blur();'>  
          <option value="none">~Choose the word type~</option>  
               <s:iterator value="word_type">  
                               <option value="<s:property value="abbr"/>"><s:property value="type"/></option>   
               </s:iterator>  
          </select>  

add.jsp code:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
   pageEncoding="ISO-8859-1"%>  
   <%@ taglib uri="/struts-tags" prefix="s" %>  
   <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="t" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Insert title here</title>  
 </head>  
 <body>  
 <div class="container">  
      <div class="for_title"><span id="add_title">Add A New Word</span></div>  
      <hr/>  
      <form name="f1" method="post" enctype="multipart/form-data">  
        <div class="col">  
       <div class="label">New Word:<sup>*</sup></div>  
        <div class="field">  
        <input type="text" name="word" placeholder="Enter the word"/>  
        </div>  
        </div>  
          
        <div class="col">  
       <div class="label">Type:<sup>*</sup></div>  
        <div class="field">  
        <select type="text" name="word_type" style="height:auto;" onfocus='this.size=8;' onblur='this.size=1;' onchange='this.size=1;this.blur();'>  
          <option value="none">~Choose the word type~</option>  
               <s:iterator value="word_type">  
                               <option value="<s:property value="abbr"/>"><s:property value="type"/></option>   
               </s:iterator>  
          </select>  
        </div>  
        </div>  
          
        <div class="col">  
       <div class="label">Meaning 1:<sup>*</sup></div>  
        <div class="field">  
        <input type="text" name="meaning1" placeholder="Enter the meaning"/>  
        </div>  
        </div>  
          
        <div class="col">  
       <div class="label">Meaning 2:</div>  
        <div class="field">  
        <input type="text" name="meaning2" placeholder="Enter the second meaning (optional)"/>  
        </div>  
        </div>  
          
        <div class="col">  
       <div class="label">Meaning 3:</div>  
        <div class="field">  
        <input type="text" name="meaning3" placeholder="Enter the third meaning (optional)"/>  
        </div>  
        </div>  
     
        <div class="col">  
       <div class="label">Comment:</div>  
        <div class="field">  
        <input type="text" name="comment" placeholder="Enter the special comment (if any)"/>  
        </div>  
        </div>  
          
        <div class="col">  
         <button type="submit" onClick="return validation()">Add</button>  
        </div>  
      </form>  
    </div>  
 </body>  
 </html>  

Step 4.3: Add class and method arguments to action named add in struts.xml.

 <action name="add" class="engdictionary.controller.TypeController" method="viewType">  
                <result type="tiles">add.tiles</result>  
           </action>  

Step 4.4: Run the project and you will see the iterated values in the drop-down list as follows.


No comments:

Post a Comment