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.



No comments:

Post a Comment