1 Replies - 405 Views - Last Post: 08 August 2017 - 05:59 AM

#1 tmt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-August 17

how to insert data in two different tables using java and pojo

Posted 08 August 2017 - 05:35 AM

Here is my servlet
public int insertUserR2(UserR2pojo urps) throws SQLException, ClassNotFoundException
    {
        
            // Create a SQL statement with ten parameters (?)
            String insertUserR2 = "insert into registration values (?,?,?,?,?,?,?,?,?,?)";
            

            // Get Connection
            Connection con = new DbConnection().getConnection();

            // Create a PreparedStatement object.
            PreparedStatement ps = con.prepareStatement(insertUserR2);
            //PreparedStatement ps2 = con.prepareStatement(insertUserR2a);

            // Set value for the first parameter (First '?')
            ps.setInt(1, urps.getCoop_no());

            ps.setString(2, urps.getStaff_no()); 
            ps.setString(3, urps.getReg_date());
            ps.setString(4, urps.getSalutation());
            ps.setString(5, urps.getFirst_name());
            ps.setString(6, urps.getMiddle_name());
            ps.setString(7, urps.getLast_name());
            ps.setString(8, urps.getSex());
            ps.setString(9, urps.getNext_of_kin());
            ps.setString(10, urps.getStatus());
            ps.executeUpdate();
            
            ps.close();
       
            String insertUserR2a = "insert into contact values (?,?,?,?,?,?,?)";
           
            // Create a PreparedStatement object.
            PreparedStatement ps2 = con.prepareStatement(insertUserR2a);
            
            ps2.setInt(1, urps.getCoop_no());
            ps2.setString(2, urps.getFaculty());
            ps2.setString(3, urps.getDepartment());
            ps2.setString(4, urps.getContact_address());
            ps2.setString(5, urps.getMobile_phone());
            ps2.setString(6, urps.getOffice_phone());
            ps2.setString(7, urps.getEmail());
            //ps.setString(6, urp.getMonthly_savings());
            ps2.executeUpdate();
            ps2.close();
            // Execute statement
            // executeUpdate(String) using for Insert, Update, Delete statement.
            int i = ps2.executeUpdate();
            
            return i;
        
    }



Here is my pojo
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sua.pojo;

/**
 *
 * @author so
 */
public class UserR2pojo {
    
    private int coop_no; //This is of data type 'int' which must be the same as what we have on the column or field 'coop_no' on the database table.
    private String staff_no;
    private String reg_date;
    private String salutation;
    private String first_name;
    private String middle_name;
    private String last_name;
    private String sex;
    private String next_of_kin;
    private String status;
    private String contact_address;
    private String faculty;
    private String department;
    private String mobile_phone;
    private String office_phone;
    private String email;
    //private String monthly_savings;


    

    public String getContact_address() {
        return contact_address;
    }

    public void setContact_address(String contact_address) {
        this.contact_address = contact_address;
    }

    public int getCoop_no() {
        return coop_no;
    }

    public void setCoop_no(int coop_no) {
        this.coop_no = coop_no;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

  

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getMiddle_name() {
        return middle_name;
    }

    public void setMiddle_name(String middle_name) {
        this.middle_name = middle_name;
    }

    public String getMobile_phone() {
        return mobile_phone;
    }

    public void setMobile_phone(String mobile_phone) {
        this.mobile_phone = mobile_phone;
    }

    public String getNext_of_kin() {
        return next_of_kin;
    }

    public void setNext_of_kin(String next_of_kin) {
        this.next_of_kin = next_of_kin;
    }

    public String getOffice_phone() {
        return office_phone;
    }

    public void setOffice_phone(String office_phone) {
        this.office_phone = office_phone;
    }

    public String getReg_date() {
        return reg_date;
    }

    public void setReg_date(String reg_date) {
        this.reg_date = reg_date;
    }

    public String getSalutation() {
        return salutation;
    }

    public void setSalutation(String salutation) {
        this.salutation = salutation;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getStaff_no() {
        return staff_no;
    }

    public void setStaff_no(String staff_no) {
        this.staff_no = staff_no;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getFaculty() {
        return faculty;
    }

    public void setFaculty(String faculty) {
        this.faculty = faculty;
    }
    
    
    
}



Here is my other servlet
package com.sua.servlet;

import com.sua.db.DbManipulation;
import com.sua.pojo.UserCPojo;
import com.sua.pojo.UserR2pojo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class UserRegistration2 extends HttpServlet {

    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try 
        {
            
            String coopno = request.getParameter("coop_no") ; 
            String $staff_no = request.getParameter("staff_no") ;
            String $reg_date = request.getParameter("reg_date");
            String $salutation = request.getParameter("salutation") ; 
            String $first_name = request.getParameter("first_name") ;
            String $middle_name = request.getParameter("middle_name") ;
            String $last_name = request.getParameter("last_name") ;
            String $sex = request.getParameter("sex") ;
            String $next_of_kin = request.getParameter("next_of_kin") ;
            String $status = request.getParameter("status");
            String $contact_address = request.getParameter("contact_address") ;
            String $faculty = request.getParameter("faculty") ;
            String $department = request.getParameter("department") ;
            String $mobile_phone = request.getParameter("mobile_phone") ;
            String $office_phone = request.getParameter("office_phone") ;
            String $email = request.getParameter("email") ;
            //String $monthly_savings = request.getParameter("monthly_savings") ;
                    
            //Here i tried to convert the String data type 'coopno' above to an Integer Data type.
            // Integer x = Integer.valueOf(Str);    OR 
            // int y = integer.parseInt(Str);
            Integer $coop_no = Integer.parseInt(coopno);
            //Date reg_date = Date.valueOf($reg_date);
            
            //Object Instantiation.
            /* 
             * And in other to instantiate an Object, you first declare the Data Type of the Object, it is called the 'UserRpojo'. Its almost like declaring a new Variable remember.
             * Now what is this 'UserRpojo();' Where did it comes from? Because if you look at it(in the UserRpojo.java project)i never put in a method called UserRpojo(); ,that doesn't take any parameter.
             * This 'UserRpojo()' is called the CONSTRUCTOR and a constructor is a special method solely used for instantiating Classes into Objects or for instantiating an Object.
             * So now that i'm calling the ability to instantiate an object here,ok 'up'. 
            */
            /* This is Object Instantiation. I'm instantiating a new instance of the 'UserCPojo' Class that is going to reference an Object in the memory called 'up'
            *and the way i do that is that i do Two things, I create a variable 'up' of the appropriate Data Type 'UserRpojo'
            *and I called the 'new' operator and i say next to it the name of the Constructor 'UserRpojo()'. Now a CONSTRUCTOR is a special method used for Object Instantiation 
            *that always has the same name as the Class it is used to instantiate.
            * So on line 56 I'm utilising what we called the DEFAULT CONSTRUCTOR (For this example this is An Implicit Default Constructor)
            */ 
            UserR2pojo up = new UserR2pojo(); // 'up' here is a reference to a 'UserRpojo' class or a 'UserRpojo' object
            
            //Define the attributes of my new UserRpojo by invoking the method of the class.
            up.setCoop_no($coop_no); //In other to set the coop_no of 'up' to '$coop_no', we can invoke the 'setCoop_no()' method using 'up.setCoop_no($coop_no)'
            up.setStaff_no($staff_no);
            up.setReg_date($reg_date);
            up.setSalutation($salutation);
            up.setFirst_name($first_name);
            up.setMiddle_name($middle_name);
            up.setLast_name($last_name);
            up.setSex($sex);
            up.setNext_of_kin($next_of_kin);
            up.setStatus($status);
            
           
            //Define the attributes of my new UserCPojo by invoking the method of the class.
            up.setCoop_no($coop_no); //In other to set the coop_no of 'up' to '$coop_no', we can invoke the 'setCoop_no()' method using 'up.setCoop_no($coop_no)'
            up.setFaculty($faculty);
            up.setDepartment($department);
            up.setContact_address($contact_address);
            up.setMobile_phone($mobile_phone);
            up.setOffice_phone($office_phone);
            up.setEmail($email);
            
            
            //Object Instantiation.
            DbManipulation dmn = new DbManipulation(); // 'dmn' here is a reference to a 'DbManipulation' class or a 'DbManipulation' object
            
            int j = 0;
            
            try 
            {
                j = dmn.insertUserR2(up); //Invoke the 'insertUserR' method in 'DbManipulation' class and pass in the parameter 'up'  and assign it to the variable 'j'.
            } 
            catch (SQLException ex) 
            {
                Logger.getLogger(UserContact.class.getName()).log(Level.SEVERE, null, ex);
            } 
            catch (ClassNotFoundException ex) 
            {
                Logger.getLogger(UserContact.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if(j != 0){
                
                out.println("Registration Successful");
            
            }
            else
            {
            
                out.println("Registration Failed");
            
            }
            
            
            
        } 
        finally 
        {            
            out.close();
        }
    }
    
}





Here is my JSP Form

<form method="post" action="UserRegistration2" ><table>
					
					
					<tr> 
						<td colspan="2" bgcolor="#009966" ><div align="center"><strong>Registration Form</strong></div></td>
					</tr>
					
					
					<tr> 
						<td>Cooperative No:</td>
						<td><input type="text" name="coop_no" id="ccnum" style="background-color: #ffffa0"></td>
					</tr>
					
					<tr>
						<td>Staff No :</td>
						<td><input type="text" name="staff_no" id="ssnum" style="background-color: #ffffa0"></td>
					</tr>
					<!--
					<tr> 
						<td> Date of Registration: </td>
						<td id="date"> <?php echo $this->my_dateA();?> </td>
					</tr>
					-->
                                      
					 <!-- Datepicker -->
					<tr>
                                            <td class="demoHeaders">Datepicker</td>
                                            <td><input name="reg_date" id="datepicker" /><br /></td>
                                        </tr> 
					
                                        
   
					
					<tr>
						<td>Salutation:</td>
						<td>
							<select name="salutation" id="sal" style="background-color: #ffffa0">
								<option value="">Select</option>
								<option value="Prof">Prof</option>
								<option value="Dr">Dr</option>
								<option value="Mr">Mr</option>
								<option value="Mrs">Mrs</option>
							</select>
						</td>
					</tr>
					
					<tr>
						<td>First Name</td>
						<td><input type="text" name="first_name" id="fname" style="background-color: #ffffa0"></td>
					</tr>
					
					<tr>
						<td>Middle Name</td>
						<td><input type="text" name="middle_name" id="mname" style="background-color: #ffffa0"></td>
					</tr>
					
					<tr>
						<td>Last Name</td>
						<td><input type="text" name="last_name" id="lname" style="background-color: #ffffa0"></td>
					</tr>
					
					<!--
					<tr>
						<td><label for="sex-select">I am:</label></td>
						<td>
							<div class="input-container">
								<select name="sex-select" id="sex-select">
									<option value="0">Select Sex:</option>
									<option value="1">Female</option>
									<option value="2">Male</option>
								</select>
							</div>
						
						</td>
					</tr>
					-->
					
					<tr>
						<td>Sex:</td></td>
						<td>
							<select name="sex" id="sex" style="background-color: #ffffa0">
								<option value="">Select</option>
								<option value="Male">Male</option>
								<option value="Female">Female</option>
							</select> 
						</td>
					</tr>
					
					
					<tr>
						<td>Next of Kin:</td>
						<td><input type="text" name="next_of_kin" id="nxtk" style="background-color: #ffffa0"></td>
					</tr>
                                        
                                        <tr>
						<td>Status:</td>
						<td><input type="checkbox" name="status" value="ON" style="background-color: #ffffa0">ON
                                                <input type="checkbox" name="status" value="OFF" style="background-color: #ffffa0">OFF</td>
					</tr>
                                        
					
					<td colspan="2" bgcolor="#009966"><div align="center"><strong>Contact Section</strong></div></td>
					
					<tr>
						<td>Contact Address:</td>
						<td><input type="text" name="contact_address" id="cadd" style="background-color: #ffffa0"></td>
					</tr>
					
					
					
					<tr>
						<td>Faculty</td>
						<td>
							<select name="faculty" style="background-color: #ffffa0" onchange="showDept(this.value)">
                                                            <option selected="selected" value="" >--Select Faculty--</option>
                                                                <%      Connection con = new DbConnection().getConnection();
                                                                        Statement st = con.createStatement( );
                                                                        ResultSet rs1=st.executeQuery("SELECT * FROM transfaculties"); 
                                                                        while(rs1.next())
                                                                        { 
                                                                            %>
                                                                                <option value="<%=rs1.getString("faculty_id")%>">
                                                                                    <%=rs1.getString("facultyname")%>
                                                                                </option>
                                                                            <%
                                                                        }
                                                                %>
                                                        </select>
						</td>
			   
					</tr>
				
					<tr>
						<td>Department</td>
						<td>
							<select name="department"  style="background-color: #ffffa0" id="department">
                                                          <option value="-1">--Select Dept--</option>
                                                          
                                                        </select>
						</td>
			   
					</tr> 
				
						   
					<tr>
						<td>Mobile Phone</td>
						<td><input name="mobile_phone" type="text" id="mphone" style="background-color: #ffffa0"></td>
					</tr>
					
					
					<tr>
						<td>Office Phone</td>
						<td><input name="office_phone" type="text" id="ophone" style="background-color: #ffffa0"></td>
					</tr>
					
					<tr>
						<td>Email :</td>
						<td><input type="text" name="email" id="email" style="background-color: #ffffa0"></td>
					</tr>
			
					
					<!--
					<tr>
						<td>Monthly Deduction:</td>
						<td><input name="monthly_savings" type="text" id="mdec" style="background-color: #ffffa0"></td>
					</tr>
					-->
                                        
                                        
					<!--
					<label>Password :</label>
					<input type="password" name="password" id="password">
					
					<label>Confirm Password :</label>
					<input type="password" name="cpassword" id="cpassword">
					-->
						
					
					<!--
					<table>
						<tr>
							<td>&nbsp;</td>
								<td><input type="submit" class="greenButton" value="Sign Up" /><img id="loading" src="img/ajax-loader.gif" alt="working.." />
							</td>
						</tr>
					</table>
					-->
					
					<table width="75%" border="0" cellspacing="0" cellpadding="0" align="center">
						<tr>
							<td><input type="submit" name="submit" value="Register" /></td>  <!-- margin="1px 0px 0px 80px" -->
						</tr>
					</table>
					
					
				</table> </form>



But it is not submitting to my registration and contact tables

Is This A Good Question/Topic? 0
  • +

Replies To: how to insert data in two different tables using java and pojo

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: how to insert data in two different tables using java and pojo

Posted 08 August 2017 - 05:59 AM

You haven't posted the source of the class doing the insert
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1