5 Replies Latest reply on Dec 6, 2011 1:09 AM by karthik kannan J

    blazeds sample application with Db connectivity

    karthik kannan J

      Hi folks,

       

             i m new face for blazeds. i m trying with simple examles with blazeds. i learned and ran the blazeds examples without db connection. i want to know db connection with blazeds. could u anyone guide me for this task. if u give some sample application for this db connection, it ll may more helpful for me..

       

       

                        thanks in advance........

        • 1. Re: blazeds sample application with Db connectivity
          Vibhuti Gosavi(InfoCepts) Level 3

          Hi,

           

          I have implemented this..I am giving you the code for both the Flex and for services in java..You will have to do settings in Blaze ds configuration files..

           

          The mxml is as follows:

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                           xmlns:s="library://ns.adobe.com/flex/spark"

                                           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"

                                           minHeight="600" xmlns:services="services.*" xmlns:valueObjects="valueObjects.*">

                    <fx:Style source="EmployeeDemo.css"/>

                    <fx:Script>

                              <![CDATA[

                                        import mx.controls.Alert;

                                        import mx.events.FlexEvent;

                                        import mx.rpc.events.ResultEvent;

           

           

                                        protected function button_clickHandler(event:MouseEvent):void

                                        {

           

                                                  obj.getEmployees();

                                        }

           

                                        protected function button3_clickHandler(event:MouseEvent):void

                                        {

                                                  employee.empId= empIdTextInput.text as int;

                                                  employee.empName = empNameTextInput.text;

                                                  employee.email = emailTextInput.text;

                                                  employee.designation = designationTextInput.text;

                                                  employee.mobile = mobileTextInput.text;

                                                  obj.createEmployee(empNameTextInput.text,mobileTextInput.text,emailTe xtInput.text,designationTextInput.text);

                                                  trace(empIdTextInput.text);

                                                  Alert.show("Inserted Successfully");

                                        }

           

           

                                        protected function button2_clickHandler(event:MouseEvent):void

                                        {

                                                  obj.getEmployee(srch.text);

           

                                        }

           

           

           

                                        protected function button4_clickHandler(event:MouseEvent):void

                                        {

                                                            empNameTextInput.text="";

                                                            mobileTextInput.text="";

                                                            emailTextInput.text="";

                                                            designationTextInput.text="";

                                                            srch.text="";

                                        }

           

           

           

           

                                        protected function button5_clickHandler(event:MouseEvent):void

                                        {

           

                                                  obj.remove(empNameTextInput.text);

                                                  Alert.show("Removed Successfully");

                                        }

           

           

           

           

                                        protected function button6_clickHandler(event:MouseEvent):void

                                        {

           

                                                            obj.updateEmployee(empNameTextInput.text,mobileTextInput.text,em ailTextInput.text,designationTextInput.text,empIdTextInput.text);

                                        }

                                        private function getEmployees1(event:ResultEvent):void

                                        {

                                                            dataGrid.dataProvider=event.result;

                                        }

                                        private function getEmployee1(event:ResultEvent):void

                                        {

                                                            dataGrid.dataProvider=event.result;

                                        }

                                        private function createEmployee1(event:ResultEvent):void

                                        {

                                                            event.result;

                                        }

                                        private function remove1(event:ResultEvent):void

                                        {

                                                            event.result;

                                        }

                                        private function updateEmployee1(event:ResultEvent):void

                                        {

                                                            event.result;

                                        }

           

           

           

           

                              ]]>

                    </fx:Script>

           

                    <fx:Declarations>

           

                              <!-- Place non-visual elements (e.g., services, value objects) here -->

                              <valueObjects:Employee id="employee"/>

           

                              <s:RemoteObject id="obj" destination="emp" source="EmployeeService" showBusyCursor="true">

                                        <s:method name="getEmployees" result="getEmployees1(event)"/>

                                        <s:method name="getEmployee" result="getEmployee1(event)"/>

                                        <s:method name="createEmployee" result="createEmployee1(event)"/>

                                        <s:method name="remove" result="remove1(event)"/>

                                        <s:method name="updateEmployee" result="updateEmployee1(event)"/>

                              </s:RemoteObject>

           

           

                    </fx:Declarations>

           

                    <fx:Binding source="dataGrid.selectedItem as Employee" destination="employee"/>

           

           

                    <mx:DataGrid x="392" y="110" id="dataGrid"  width="456" height="260" >

                              <mx:columns>

                                        <!--<mx:DataGridColumn headerText="Emp Id" dataField="empId"/>-->

                                        <mx:DataGridColumn headerText="Emp Name" dataField="empName"/>

                                        <mx:DataGridColumn headerText="Email" dataField="email"/>

                                        <mx:DataGridColumn headerText="Mobile" dataField="mobile"/>

                                        <mx:DataGridColumn headerText="Designation" dataField="designation"/>

           

                              </mx:columns>

                    </mx:DataGrid>

           

                    <s:Button x="690" y="47" label="Search" id="button2"  click="button2_clickHandler(event)"/>

                    <s:TextInput x="526" y="48" id="srch"/>

           

                    <s:Button x="615" y="413" label="Load Data" id="button" click="button_clickHandler(event)"/>

                    <s:Panel defaultButton="{button3}" x="35" y="106" width="302" height="331" title="Employee Details" fontSize="12">

                              <mx:FormItem label="Emp Id     " x="45" y="30" width="212" visible="false">

                                        <s:TextInput id="empIdTextInput" text="{employee.empId}" width="157" editable="false" />

                              </mx:FormItem>

                              <!--<mx:FormItem label="Emp Name      " x="45" y="59" width="212">

                                        <s:TextInput id="empNameTextInput" text="{employee.empName}" width="129"/>

                              </mx:FormItem>

                              <mx:FormItem label="Mobile         " x="45" y="101">

                                        <s:TextInput id="mobileTextInput" text="{employee.mobile}" width="150"/>

                              </mx:FormItem>

                              <mx:FormItem label="Email                " x="45" y="131" width="208">

                                        <s:TextInput id="emailTextInput" text="{employee.email}" width="156"/>

                              </mx:FormItem>

                              <mx:FormItem label="Designation" x="45" y="170" width="206">

                                        <s:TextInput id="designationTextInput" text="{employee.designation}" width="119"/>

                              </mx:FormItem>-->

           

                              <s:Button id="button3" label="Submit" click="button3_clickHandler(event)" x="20" y="218" fontSize="12"/>

                              <s:Button label="Reset" x="114" y="262" click="button4_clickHandler(event)" fontSize="12"/>

                              <s:Button x="208" y="218" label="Remove" click="button5_clickHandler(event)" id="button4" fontSize="12"/>

                              <s:Button x="114" y="218" label="Update" click="button6_clickHandler(event)" id="button5" fontSize="12"/>

                              <s:Label x="50" y="36" text="Emp Name" fontSize="12"/>

                              <s:Label x="50" y="78" text="Mobile" fontSize="12"/>

                              <s:Label x="49" y="129" text="Email" fontSize="12"/>

                              <s:Label x="49" y="175" text="Designation" fontSize="12"/>

                              <s:TextInput x="150" y="36" id="empNameTextInput" text="{employee.empName}" fontSize="12"/>

                              <s:TextInput x="150" y="78" id="mobileTextInput" text="{employee.mobile}" fontSize="12"/>

                              <s:TextInput x="150" y="119" id="emailTextInput" text="{employee.email}" fontSize="12"/>

                              <s:TextInput x="150" y="165" id="designationTextInput" text="{employee.designation}" fontSize="12"/>

                    </s:Panel>

                    <s:Label x="112" y="34" text="BlazeDS Demo" fontSize="25" color="#0F6FF1"/>

          </s:Application>

           

          Have a connection manager for db:

           

          import java.sql.Connection;

          import java.sql.DriverManager;

          import java.sql.SQLException;

           

           

           

           

          /*This class contains methods for creating database connection and closing database connection.*/

          public class ConnectionManager

          {

                    private String url;

                    private static ConnectionManager instance;

           

           

                    /*Constructor of the class*/

                    private ConnectionManager()

                    {

                              try

                              {

                                        Class.forName("com.mysql.jdbc.Driver");  //calls jdbc:mysql driver

                                        url = "jdbc:mysql://localhost:3306/employee";

                              }

                              catch (Exception e)

                              {

                                        e.printStackTrace();

                              }

                    }

           

           

                    /*This method is establishing connection to MySQL Database*/

                    public static Connection getConnection() throws SQLException

                    {

                              if (instance == null)

                              {

                                        instance = new ConnectionManager();

                              }

                              try {

                                        return DriverManager.getConnection(instance.url,"root","root");

                              }

                              catch (SQLException e)

                              {

                                        throw e;

                              }

                    }

           

                    /*This method is used to close the connection with the database*/

                    public static void close(Connection connection)

                    {

                              try

                              {

                                        if (connection != null)

                                        {

                                                  connection.close();

                                        }

                              }

                              catch (SQLException e)

                              {

                                        e.printStackTrace();

                              }

                    }

          }

           

          and following are the services and a file for getter and setter:

           

          import java.sql.*;

          import java.util.ArrayList;

           

           

          public class EmployeeService

          {

           

           

                    ConnectionManager connMgr;

                    Connection conn;

           

                    public ArrayList<Employee> getEmployees() throws SQLException {

           

           

                                ArrayList<Employee> product = new ArrayList<Employee>();

                               

                                conn = ConnectionManager.getConnection();

           

           

           

           

                                PreparedStatement ps=conn.prepareStatement("SELECT Emp_id, Emp_Name, Emp_Mobile, Emp_Email, Emp_Designation FROM employee_table");

                                

                                 ResultSet rs = ps.executeQuery();

                                

                                 while (rs.next()) {

                                           Employee pro = new Employee();

                                          

                                  pro.setEmpId(rs.getInt("Emp_id"));                     

                                  pro.setEmpName(rs.getString("Emp_Name"));

                                  pro.setMobile(rs.getString("Emp_Mobile"));

                                  pro.setEmail(rs.getString("Emp_Email"));

                                  pro.setDesignation(rs.getString("Emp_Designation"));

                                  product.add(pro);

                                 

                                 }

                                 //System.out.println("data "+product);

                                 conn.close();

                                return product;

                               }

           

                    public ArrayList<Employee> getEmployee(String name) throws SQLException {

           

           

                              ArrayList<Employee> product = new ArrayList<Employee>();

                               

                                conn = ConnectionManager.getConnection();

           

           

           

           

                                        PreparedStatement ps = conn.prepareStatement("SELECT Emp_id, Emp_Name, Emp_Mobile, Emp_Email, Emp_Designation FROM employee_table WHERE Emp_Name=?");

                                        ps.setString(1, name);

                                        ResultSet rs = ps.executeQuery();

           

                                        while (rs.next()) {

                                                  Employee pro = new Employee();

                                                  pro.setEmpId(rs.getInt("Emp_id"));                     

                                            pro.setEmpName(rs.getString("Emp_Name"));

                                            pro.setMobile(rs.getString("Emp_Mobile"));

                                            pro.setEmail(rs.getString("Emp_Email"));

                                            pro.setDesignation(rs.getString("Emp_Designation"));

                                            product.add(pro);

                                           

                                         }

                                        //System.out.println("data "+product);

                                           conn.close();

                                          return product;

                    }

           

           

                    public void createEmployee(String ename, String mobile, String email, String dsg)throws SQLException 

                    {

                                 

           

                                        conn = ConnectionManager.getConnection();

                                        PreparedStatement st=conn.prepareStatement("Insert into employee_table (Emp_Name, Emp_Mobile, Emp_Email, Emp_Designation) values(?,?,?,?)");

           

                                        st.setString(1,ename);

                                        st.setString(2,mobile);

                                        st.setString(3,email);

                                        st.setString(4,dsg);

                                        //st.setString(5,empid);

           

                                        st.executeUpdate();

           

                                        conn.close();

           

                    }

                    public void updateEmployee(String ename, String mobile, String email, String dsg ,String empid)throws SQLException 

                    {

                                 

           

                                        conn = ConnectionManager.getConnection();

                                        PreparedStatement st=conn.prepareStatement("UPDATE employee_table SET Emp_Name=?, Emp_Mobile=?, Emp_Email=?, Emp_Designation=? WHERE Emp_id=?");

           

                                        st.setString(1,ename);

                                        st.setString(2,mobile);

                                        st.setString(3,email);

                                        st.setString(4,dsg);

                                        int temp = Integer.parseInt(empid);

                                        st.setInt(5,temp);

                                        st.executeUpdate();

           

                                        conn.close();

           

                    }

           

           

                    public void remove(String ename) throws SQLException {

           

           

                              conn = ConnectionManager.getConnection();

                                        PreparedStatement ps = conn.prepareStatement("DELETE FROM employee_table WHERE Emp_Name=?");

                                        ps.setString(1,ename);

                                        ps.executeUpdate();

                                        conn.close();

                    }

           

          Class for getter and setter:

           

           

          public class Employee {

           

                    private int empId;

                    private String empName;

                    private String mobile;

                    private String email;

                    private String desig;

           

                    //constructor

                    public Employee(){

           

                    }

           

                    public Employee(int empId,String empName,String mobile,String email,String desig){

                              this.empId= empId;

                              this.empName= empName;

                              this.mobile= mobile;

                              this.email= email;

                              this.desig = desig;

                    }

           

                    public String getEmpName()

                    {

                              return empName;

                    }

                    public void setEmpName(String empName)

                    {

                              this.empName= empName;

                    }

           

                    public String getMobile()

                    {

                              return mobile;

                    }

                    public void setMobile(String mobile)

                    {

                              this.mobile= mobile;

                    }

           

                    public int getEmpId()

                    {

                              return empId;

                    }

                    public void setEmpId(int empId)

                    {

                              this.empId= empId;

                    }

                    public String getDesignation()

                    {

                              return desig;

                    }

                    public void setDesignation(String desig)

                    {

                              this.desig= desig;

                    }

                    public String getEmail()

                    {

                              return email;

                    }

                    public void setEmail(String email)

                    {

                              this.email= email;

                    }

           

          }

                 

          }

           

          Thanks and Regards,

          ------------------------------------------------------------------------------------------ --

          Vibhuti Gosavi | vgosavi@infocepts.com | www.infocepts.com

          ------------------------------------------------------------------------------------------ --

          • 2. Re: blazeds sample application with Db connectivity
            karthik kannan J Level 1

            Hi Vibhuti Gosavi,

             

                                    Thank you so much for your nice response... i m trying to run your code now. while i m running the application i m getting error that  java.sql.SQLException: The url cannot be null" faultCode="Server.Processing  .

              could u plz guide me to overcome this issue..    

             

                                                                     thank u once again.

            • 3. Re: blazeds sample application with Db connectivity
              Vibhuti Gosavi(InfoCepts) Level 3

              Hi,

               

              Please check DB connectivity...check user name, password, DB schema name, DB server name...

              any mistake in these will give an error...so please check them carefully...

               

               

               

              Thanks and Regards,

              ------------------------------------------------------------------------------------------ --

              Vibhuti Gosavi | vgosavi@infocepts.com | www.infocepts.com

              ------------------------------------------------------------------------------------------ --

              • 4. Re: blazeds sample application with Db connectivity
                karthik kannan J Level 1

                Hi,

                 

                      thank you once again vibhuti. i will try with DB as you said and let u know.

                • 5. Re: blazeds sample application with Db connectivity
                  karthik kannan J Level 1

                  HI,

                   

                           thank u so much man..  i did run the application successfully.. it was too nice. i m facing some issue with search. its not working.. except that everything is great yaar..