Wcf Service With Java Client

In this session we will learn how to consume wcf service into java application . First we need to create wcf class library project . Inside the interface i defined one method with three parameters.

Here is the method ..


  [OperationContract]
 string InsertStudentRecord(string Name, string Email, string Address, string Mobile);


Here is the code for method  implementation .

  public string InsertStudentRecord(string Name, string Email, string Address, string Mobile)
        {
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=3TierInWindowsApplication;Integrated Security=True");
 SqlCommand cmd = new SqlCommand("AddNewStudent", con);
  cmd.CommandType = CommandType.StoredProcedure;

 cmd.Parameters.AddWithValue("@Name", Name);
 cmd.Parameters.AddWithValue("@Address", Address);
 cmd.Parameters.AddWithValue("@EmailId", Email);
 cmd.Parameters.AddWithValue("@Mobile", Mobile);

  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
   return "Success";
        }

Then we need to add wsdl adrress into java project . Finally just add a JS From with text boxes and label and the button  .


Here is the code for .svc file .




 <%@ ServiceHost Language="C#" Debug="true" Service="StudentServiceWcf.StudentService"%>


Here is the screen shot of the windows form ..


Double click the submit button to generate the event  and copy and paste following codes .


 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       
          String Name,Address,Email,Mobile;
          
           Name=jTextField1.getText();
           Address=jTextField2.getText();
           Email =jTextField3.getText();
           Mobile=jTextField4.getText();
          insertStudentRecord(Name,Address,Email,Mobile);
             JOptionPane.showMessageDialog(null,"Record is Inserted");
    }    


 private static String insertStudentRecord(java.lang.String name, java.lang.String email, java.lang.String address, java.lang.String mobile) {
        org.tempuri.StudentService service = new org.tempuri.StudentService();
        org.tempuri.IStudentService port = service.getBasicHttpBindingIStudentService();
        return port.insertStudentRecord(name, email, address, mobile);
    }


2 comments:

  1. Code throwing null refereance exception when I
    Run the code and tried to insert new data

    ReplyDelete
    Replies
    1. To avoid this error make sure you pass all the
      required values inside textbox . Hope you will
      be able to insert the data . Thanks

      Delete