28 Dec 2016

Leisure Activity: Python3 welcomes upcoming new year..!

Code:


 '''  
 Created on 28-Dec-2016  
   
 @author: linuxbeginner  
 '''  
 from time import sleep  
   
 def anim():  
   sleep(0.10)  
   
 welcome = ['W','E','L','C','O','M','E']  
 twok = ['T','w','o','*','*','T','h','o','u','s','a','n','d']  
 seventeen = ['S','e','v','e','n','t','e','e','n']  
 welcome_counter = 0  
 twok_counter = 0  
 seventeen_counter = 0  
   
 for i in range(0,5):  
   for sp in range(0,7):  
     print(' ',end="")  
   for j in range(4,i,-1):  
     print(' ',end="")  
   for k in range(0,i):  
     if i == 4:  
       print('{0}'.format(welcome[welcome_counter]),end="")  
       welcome_counter += 1  
     else:  
       print('*',end="")  
   for l in range(1,i):  
     if i == 4:  
       print('{0}'.format(welcome[welcome_counter]),end="")  
       welcome_counter += 1  
     else:  
       print('*',end="")  
   print(end="\n")  
   anim()  
     
 for i in range (0,3):  
   for j in range(0,i):  
     print(' ',end="")  
   for k in range(3,i,-1):  
     print('*',end="")  
   for l in range(0,15):  
     if i == 0 and l in range(1,14):  
       print('{0}'.format(twok[twok_counter]),end="")  
       twok_counter += 1  
     elif i == 1 and l in range(3,12):  
       print('{0}'.format(seventeen[seventeen_counter]),end="")  
       seventeen_counter += 1  
     else:  
       print('*',end="")  
   for m in range(3,i,-1):  
     print('*',end="")  
   print(end="\n")  
   anim()  
     
 print(end="\n")  

Output is shown in the following GIF image:



12 Dec 2016

Leisure Activity: Python3 wishes a good bye to 2016..!

Code:


 '''  
 Created on 12-Dec-2016  
   
 @author: linuxbeginner  
 '''  
 bye = ['B','Y','E']  
 two = ['T','W','O']  
 thousand = ['T','H','O','U','S','A','N','D']  
 sixteen = ['S','I','X','T','E','E','N']  
 num = ['2','0','1','6']  
 two_counter = 0  
 thousand_counter = 0  
 sixteen_counter = 0  
 num_counter = 0  
   
 print(end="\n")  
 for i in range(0,13):  
   for j in range(13,i,-1):  
     print(' ',end="")  
   for k in range(0,i+1):  
     if i == 0:  
       print('{0} '.format(bye[0]),end="")  
     elif i == 1:  
       print('{0} '.format(bye[1]),end="")  
     elif i == 2 and (k == 0 or k == 2):  
       print('{0} '.format(bye[2]),end="")  
     elif i == 4 and (k in range(1,4)):  
       print('{0} '.format(two[two_counter]),end="")  
       two_counter += 1  
     elif i == 7:  
       print('{0} '.format(thousand[thousand_counter]),end="")  
       thousand_counter += 1  
     elif i == 10 and (k in range(2,9)):  
       print('{0} '.format(sixteen[sixteen_counter]),end="")  
       sixteen_counter += 1  
     elif (i == 12 and (k in range(2,6))) or (i == 12 and (k in range(7,11))):  
       print('{0} '.format(num[num_counter]),end="")  
       num_counter += 1  
       if k == 5:  
         num_counter = 0  
     else:  
       print('* ',end="")  
   print(end="\n")  
 print(end="\n")  

Output:



8 Dec 2016

Recursion and File Handling demonstration using Python

The following program of Fibonacci series computes using recursive function and saves the output to a text file. The code is as shown below:

 # A program demonstrating recursion and file handling  
   
 x = 0 ; y = 1   
   
 def fibonacci(x,y,lim):  
      if lim != 0:  
           z = x + y  
           f.write('{0} '.format(z))  
           print('{0}'.format(z),end="\t")  
           x = y  
           y = z  
           lim -= 1  
           fibonacci(x,y,lim)  
        
 lim = int(input('Enter the size of series:'))            
 f = open('output.txt','w')  
 fibonacci(x,y,lim)  
 f.close()  
 print(end="\n")  

The output is shown in the following GIF image:


8 Nov 2016

Creating Foreign Key in phpMyAdmin

Step 1: Create two tables tbl_course_detail and tbl_student_detail in the database student as per the following schemas shown below:

tbl_course_detail

Name Type Constraint
Course_ID Varchar(10) Primary Key
Course_name Varchar(20) Not Null
Credit Int(1) Not Null
Semester_type Varchar(15) Not Null

tbl_student_detail

Name Type Constraint
Enr_No Int(12) Primary Key
Name Varchar(30) Not Null
City Varchar(20) Not Null
Course_ID Varchar(10) Not Null

Our aim is to import the Primary Key Course_ID of table tbl_course_detail as a Foreign Key in the table tbl_student_detail.


Step 2: Open the "Structure" of tbl_student_detail and click the "Relation view" button.



Step 3: Select 'tbl_course_detail' from the drop-down beside Course_ID. You will automatically get Course_ID in the second field. Click the "Save" button.


You have successfully created the Foreign Key in table tbl_student_detail. You can check by inserting the values. The entire process is shown in the GIF image below:

7 Nov 2016

Recreational Coding with HTML (Leisure Activity)

Code:

 <!-----------------------------------------------  
 Author: Qaidjohar  
 Date Created: Nov 7 2016  
 Logical Comment: Leisure Activity  
 ------------------------------------------------>  
   
 <html>  
   <head>  
     <title>Mario</title>  
     <style type="text/css">  
       *  
       {  
         margin: 0px;  
         padding: 0px;  
       }  
       .main  
       {  
         width:1000px;  
         margin-left: 183px;  
       }  
       .header  
       {  
         background-color: greenyellow;  
         height: 50px;  
       }  
       .container  
       {  
         background-color: darkseagreen;  
       }  
       .footer  
       {  
         background-color: black;  
         height: 25px;  
       }  
       td  
       {  
         height: 20px;  
         width: 20px;  
         background-color: darkseagreen;  
       }  
       h1  
       {  
         text-align: center;  
         color: green;  
         font-style: oblique;  
         line-height: 50px;  
       }  
       h4  
       {  
         color: yellow;  
         text-align: center;  
         line-height: 25px;  
       }  
       .mario  
       {  
         animation: mario-anim 10s infinite;  
       }  
       .red  
       {  
         background-color: red;  
       }  
       .yellow  
       {  
         background-color: yellow;  
       }  
       .brown  
       {  
         background-color: saddlebrown;  
       }  
       .black  
       {  
         background-color: black;  
       }  
       .blue  
       {  
         background-color: blue;  
       }  
       @keyframes mario-anim  
       {  
         50%{margin-left: 720px;}  
         100%{margin-left: 0px;}  
       }  
     </style>  
   </head>  
     
   <body>  
     <div class="main">  
       <div class="header">  
         <h1>Recreational Coding with HTML</h1>  
       </div>  
       <div class="container">  
         <div class="mario">  
           <table cellspacing="0" cellpadding="0">  
             <!-- 1 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class=""></td> <!-- 4 -->  
               <td class=""></td> <!-- 5 -->  
               <td class=""></td> <!-- 6 -->  
               <td class=""></td> <!-- 7 -->  
               <td class=""></td> <!-- 8 -->  
               <td class=""></td> <!-- 9 -->  
               <td class=""></td> <!-- 10 -->  
               <td class=""></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 2 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class=""></td> <!-- 4 -->  
               <td class="red"></td> <!-- 5 -->  
               <td class="red"></td> <!-- 6 -->  
               <td class="red"></td> <!-- 7 -->  
               <td class="red"></td> <!-- 8 -->  
               <td class="red"></td> <!-- 9 -->  
               <td class=""></td> <!-- 10 -->  
               <td class=""></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 3 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class="red"></td> <!-- 4 -->  
               <td class="red"></td> <!-- 5 -->  
               <td class="red"></td> <!-- 6 -->  
               <td class="red"></td> <!-- 7 -->  
               <td class="red"></td> <!-- 8 -->  
               <td class="red"></td> <!-- 9 -->  
               <td class="red"></td> <!-- 10 -->  
               <td class="red"></td> <!-- 11 -->  
               <td class="red"></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 4 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class="brown"></td> <!-- 4 -->  
               <td class="brown"></td> <!-- 5 -->  
               <td class="brown"></td> <!-- 6 -->  
               <td class="yellow"></td> <!-- 7 -->  
               <td class="yellow"></td> <!-- 8 -->  
               <td class="black"></td> <!-- 9 -->  
               <td class="yellow"></td> <!-- 10 -->  
               <td class=""></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 5 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class="brown"></td> <!-- 3 -->  
               <td class="yellow"></td> <!-- 4 -->  
               <td class="brown"></td> <!-- 5 -->  
               <td class="yellow"></td> <!-- 6 -->  
               <td class="yellow"></td> <!-- 7 -->  
               <td class="yellow"></td> <!-- 8 -->  
               <td class="black"></td> <!-- 9 -->  
               <td class="yellow"></td> <!-- 10 -->  
               <td class="yellow"></td> <!-- 11 -->  
               <td class="yellow"></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 6 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class="brown"></td> <!-- 3 -->  
               <td class="yellow"></td> <!-- 4 -->  
               <td class="brown"></td> <!-- 5 -->  
               <td class="brown"></td> <!-- 6 -->  
               <td class="yellow"></td> <!-- 7 -->  
               <td class="yellow"></td> <!-- 8 -->  
               <td class="yellow"></td> <!-- 9 -->  
               <td class="black"></td> <!-- 10 -->  
               <td class="yellow"></td> <!-- 11 -->  
               <td class="yellow"></td> <!-- 12 -->  
               <td class="yellow"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 7 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class="brown"></td> <!-- 4 -->  
               <td class="yellow"></td> <!-- 5 -->  
               <td class="yellow"></td> <!-- 6 -->  
               <td class="yellow"></td> <!-- 7 -->  
               <td class="yellow"></td> <!-- 8 -->  
               <td class="black"></td> <!-- 9 -->  
               <td class="black"></td> <!-- 10 -->  
               <td class="black"></td> <!-- 11 -->  
               <td class="black"></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 8 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class=""></td> <!-- 4 -->  
               <td class="yellow"></td> <!-- 5 -->  
               <td class="yellow"></td> <!-- 6 -->  
               <td class="yellow"></td> <!-- 7 -->  
               <td class="yellow"></td> <!-- 8 -->  
               <td class="yellow"></td> <!-- 9 -->  
               <td class="yellow"></td> <!-- 10 -->  
               <td class=""></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 9 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class="red"></td> <!-- 4 -->  
               <td class="red"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class="red"></td> <!-- 7 -->  
               <td class="red"></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="red"></td> <!-- 10 -->  
               <td class="red"></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 10 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class="red"></td> <!-- 3 -->  
               <td class="red"></td> <!-- 4 -->  
               <td class="red"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class="red"></td> <!-- 7 -->  
               <td class="red"></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="red"></td> <!-- 10 -->  
               <td class="red"></td> <!-- 11 -->  
               <td class="red"></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 11 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class="red"></td> <!-- 2 -->  
               <td class="red"></td> <!-- 3 -->  
               <td class="red"></td> <!-- 4 -->  
               <td class="red"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class="blue"></td> <!-- 7 -->  
               <td class="blue"></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="red"></td> <!-- 10 -->  
               <td class="red"></td> <!-- 11 -->  
               <td class="red"></td> <!-- 12 -->  
               <td class="red"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 12 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class="yellow"></td> <!-- 2 -->  
               <td class="yellow"></td> <!-- 3 -->  
               <td class="red"></td> <!-- 4 -->  
               <td class="blue"></td> <!-- 5 -->  
               <td class="yellow"></td> <!-- 6 -->  
               <td class="blue"></td> <!-- 7 -->  
               <td class="blue"></td> <!-- 8 -->  
               <td class="yellow"></td> <!-- 9 -->  
               <td class="blue"></td> <!-- 10 -->  
               <td class="red"></td> <!-- 11 -->  
               <td class="yellow"></td> <!-- 12 -->  
               <td class="yellow"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 13 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class="yellow"></td> <!-- 2 -->  
               <td class="yellow"></td> <!-- 3 -->  
               <td class="yellow"></td> <!-- 4 -->  
               <td class="blue"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class="blue"></td> <!-- 7 -->  
               <td class="blue"></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="blue"></td> <!-- 10 -->  
               <td class="yellow"></td> <!-- 11 -->  
               <td class="yellow"></td> <!-- 12 -->  
               <td class="yellow"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 14 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class="yellow"></td> <!-- 2 -->  
               <td class="yellow"></td> <!-- 3 -->  
               <td class="blue"></td> <!-- 4 -->  
               <td class="blue"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class="blue"></td> <!-- 7 -->  
               <td class="blue"></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="blue"></td> <!-- 10 -->  
               <td class="blue"></td> <!-- 11 -->  
               <td class="yellow"></td> <!-- 12 -->  
               <td class="yellow"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 15 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class="blue"></td> <!-- 4 -->  
               <td class="blue"></td> <!-- 5 -->  
               <td class="blue"></td> <!-- 6 -->  
               <td class=""></td> <!-- 7 -->  
               <td class=""></td> <!-- 8 -->  
               <td class="blue"></td> <!-- 9 -->  
               <td class="blue"></td> <!-- 10 -->  
               <td class="blue"></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 16 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class="brown"></td> <!-- 3 -->  
               <td class="brown"></td> <!-- 4 -->  
               <td class="brown"></td> <!-- 5 -->  
               <td class=""></td> <!-- 6 -->  
               <td class=""></td> <!-- 7 -->  
               <td class=""></td> <!-- 8 -->  
               <td class=""></td> <!-- 9 -->  
               <td class="brown"></td> <!-- 10 -->  
               <td class="brown"></td> <!-- 11 -->  
               <td class="brown"></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 17 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class="brown"></td> <!-- 2 -->  
               <td class="brown"></td> <!-- 3 -->  
               <td class="brown"></td> <!-- 4 -->  
               <td class="brown"></td> <!-- 5 -->  
               <td class=""></td> <!-- 6 -->  
               <td class=""></td> <!-- 7 -->  
               <td class=""></td> <!-- 8 -->  
               <td class=""></td> <!-- 9 -->  
               <td class="brown"></td> <!-- 10 -->  
               <td class="brown"></td> <!-- 11 -->  
               <td class="brown"></td> <!-- 12 -->  
               <td class="brown"></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
   
             <!-- 18 -->  
             <tr>  
               <td class=""></td> <!-- 1 -->  
               <td class=""></td> <!-- 2 -->  
               <td class=""></td> <!-- 3 -->  
               <td class=""></td> <!-- 4 -->  
               <td class=""></td> <!-- 5 -->  
               <td class=""></td> <!-- 6 -->  
               <td class=""></td> <!-- 7 -->  
               <td class=""></td> <!-- 8 -->  
               <td class=""></td> <!-- 9 -->  
               <td class=""></td> <!-- 10 -->  
               <td class=""></td> <!-- 11 -->  
               <td class=""></td> <!-- 12 -->  
               <td class=""></td> <!-- 13 -->  
               <td class=""></td> <!-- 14 -->  
             </tr>  
           </table>  
         </div>  
       </div>  
       <div class="footer">  
         <h4>Keep Calm and Love Open Source</h4>   
       </div>  
     </div>  
   </body>  
 </html>  

Output:


22 Oct 2016

Inserting Data into Database using Java Web Application (Building Java Web Application Part III)

Step 1: Create DataController.java class in controller package including logic for both inserting as well as viewing the inserted data

 

DataController.java Code:

 package engdictionary.controller;  
   
 import java.io.IOException;  
 import java.sql.Connection;  
 import java.sql.PreparedStatement;  
 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.WordModel;  
 import engdictionary.connection.DbConnection;  
   
 public class DataController extends ActionSupport implements ModelDriven<WordModel> {  
      WordModel model = new WordModel();  
      List<WordModel> words = new ArrayList<WordModel>();  
      public WordModel getModel() {  
           return model;  
      }  
      public void setModel(WordModel model) {  
           this.model = model;  
      }  
             
      public List<WordModel> getWords() {  
           return words;  
      }  
      public void setWords(List<WordModel> words) {  
           this.words = words;  
      }  
      public String addWord() throws ClassNotFoundException,IOException  
      {  
           try {  
                Connection con = DbConnection.getConnection();  
                String query = "insert into tbl_word (word,type,meaning1,meaning2,meaning3,comment) values (?,?,?,?,?,?)";  
                PreparedStatement ps = con.prepareStatement(query);  
                ps.setString(1, model.getWord());  
                ps.setString(2, model.getWord_type());  
                ps.setString(3, model.getMeaning1());  
                ps.setString(4, model.getMeaning2());  
                ps.setString(5, model.getMeaning3());  
                ps.setString(6, model.getComment());  
                ps.executeUpdate();  
                return ERROR;  
           }   
           catch (SQLException e) {  
                // TODO: handle exception  
           e.printStackTrace();  
           }  
           return SUCCESS;  
      }  
   
      public String viewWord() throws ClassNotFoundException,IOException  
      {  
           try {  
                  
                Connection con = DbConnection.getConnection();  
                Statement st;  
                  
                st = con.createStatement();  
                ResultSet rs = st.executeQuery("select * from tbl_word");  
                WordModel model = null;  
                while(rs.next()){  
                     model = new WordModel();  
                     model.setWord(rs.getString("word"));  
                     model.setWord_type(rs.getString("type"));  
                     model.setMeaning1(rs.getString("meaning1"));  
                     model.setMeaning2(rs.getString("meaning2"));  
                     model.setMeaning3(rs.getString("meaning3"));  
                     model.setComment(rs.getString("comment"));  
                     words.add(model);            
                }  
                setWords(words);            
           } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
           return SUCCESS;  
      }  
 }  

 

Step 2: Add the following action in struts.xml


 <action name="addWord" class="engdictionary.controller.DataController" method="addWord">  
                <result name="success" type="tiles">add.tiles</result>  
                <result name="error" type="tiles">add.tiles</result>  
           </action>  

 

Step 3: Add action argument to the form tag in add.jsp


 <form name="f1" method="post" action="addWord.action" enctype="multipart/form-data">  
 .  
 .  
 </form  

 

Step 4: Inserting the data in add.jsp


 

Step 5: Viewing the data

Step 5.1: Update the code of <tbody /> tag in view.jsp with the following code.

 <tbody>  
             <s:iterator value="words">  
              <tr>  
                   <td><s:property value="word"/></td>  
                   <td><s:property value="word_type"/></td>  
                   <td><s:property value="meaning1"/></td>  
                   <td><s:property value="meaning2"/></td>  
                   <td><s:property value="meaning3"/></td>  
                   <td><s:property value="comment"/></td>  
              </tr>   
         </s:iterator>    
        </tbody>  

Step 5.2: Add class and method arguments to action named view in struts.xml.

 <action name="view" class="engdictionary.controller.DataController" method="viewWord">  
                <result type="tiles">view.tiles</result>  
           </action>  

Step 5.3: View inserted data by running the web application.



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.