Question

In: Computer Science

Write a Java program (use JDBC to connect to the database) that implements the following function...

Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C ; EXEC SQL FETCH C INTO :LOWER_P# ; EXEC SQL CLOSE C ; IF no "lower P#" retrieved THEN RETURN ; END IF ; IF "lower P#" retrieved THEN CALL RECURSION ( LOWER_P# ) ; END IF ; END DO ; END PROC ; Given the value of the input parameter ‘P1’, it should print out the following sequence (in the exact same order) for the table in Q1: P1 P2 P3 P5 P6 P4 P5 P6 P3 P5 P6

Solutions

Expert Solution

I have a question regarding Java when fetching data from, lets say MySQL database. As of now I need to write quite a lot of redundant code when fetching data. And I wonder if there is a better way to do that.

E.g. I have an method which fetch data from a table A. The method for that will look something like this then

public void readDataBase() throws Exception {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager
                    .getConnection("jdbc:mysql://localhost/feedback?"
                            + "user=sqluser&password=sqluserpw");

            statement = connect.createStatement();
            resultSet = statement
                    .executeQuery("select * from FEEDBACK.COMMENTS");
            writeResultSet(resultSet);              

        } catch (Exception e) {
            throw e;
        } finally {
            close();
        }

CALL RECURSION (GIVENP#);

N: PROC ( UPPER_P# ) RECURSIVE ;

DCL UPPER_P# ... ;

DCL LOWER_P# ...

INITIAL ( ' ' ) ;

EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P#

FROM PART_STRUCTURE WHERE MAJOR_P# =

:UPPER_P# AND MINOR_P# > :LOWER_P#

ORDER BY MINOR_P# ;

print UPPER_P# ;

DO "forever" ;

EXEC SQL OPEN C ;

EXEC SQL FETCH C INTO :

LOWER_P# ; EXEC SQL CLOSE C ;

 class template FunctorImpl
 Specialization for 1 parameter

    template <typename R, typename P1, template <class, class> class ThreadingModel>
     class FunctorImpl<R, LOKI_TYPELIST_1(P1), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>
     {
     public:
        typedef R ResultType;
         typedef typename TypeTraits<P1>::ParameterType Parm1;
        virtual R operator()(Parm1) = 0;
    };

 class template FunctorImpl
 Specialization for 2 parameters

  template <typename R, typename P1, typename P2, 
       template <class, class> class ThreadingModel>
    class FunctorImpl<R, LOKI_TYPELIST_2(P1, P2), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>
     {
     public:
        typedef R ResultType;
        typedef typename TypeTraits<P1>::ParameterType Parm1;
        typedef typename TypeTraits<P2>::ParameterType Parm2;
         virtual R operator()(Parm1, Parm2) = 0;
    };
 
 class template FunctorImpl
 Specialization for 3 parameters

    template <typename R, typename P1, typename P2, typename P3,
        template <class, class> class ThreadingModel>
    class FunctorImpl<R, LOKI_TYPELIST_3(P1, P2, P3), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>
     {
     public:
         typedef R ResultType;
         typedef typename TypeTraits<P1>::ParameterType Parm1;
         typedef typename TypeTraits<P2>::ParameterType Parm2;
        typedef typename TypeTraits<P3>::ParameterType Parm3;
         virtual R operator()(Parm1, Parm2, Parm3) = 0;
     }; 
 class template FunctorImpl
 Specialization for 4 parameters
 
     template <typename R, typename P1, typename P2, typename P3, typename P4,
         template <class, class> class ThreadingModel>
     class FunctorImpl<R, LOKI_TYPELIST_4(P1, P2, P3, P4), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>
     {
     public:
       typedef R ResultType;
         typedef typename TypeTraits<P1>::ParameterType Parm1;
         typedef typename TypeTraits<P2>::ParameterType Parm2;
         typedef typename TypeTraits<P3>::ParameterType Parm3;
        typedef typename TypeTraits<P4>::ParameterType Parm4;
         virtual R operator()(Parm1, Parm2, Parm3, Parm4) = 0;
     };
 
 class template FunctorImpl
 Specialization for 5 parameters
     template <typename R, typename P1, typename P2, typename P3, typename P4,
         typename P5,
         template <class, class> class ThreadingModel>
     class FunctorImpl<R, LOKI_TYPELIST_5(P1, P2, P3, P4, P5), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>     {
    public:
         typedef R ResultType;
         typedef typename TypeTraits<P1>::ParameterType Parm1;
         typedef typename TypeTraits<P2>::ParameterType Parm2;
         typedef typename TypeTraits<P3>::ParameterType Parm3;
         typedef typename TypeTraits<P4>::ParameterType Parm4;
        typedef typename TypeTraits<P5>::ParameterType Parm5;
         virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5) = 0;
    }; 
 class template FunctorImpl2 Specialization for 6 parameters
 
    template <typename R, typename P1, typename P2, typename P3, typename P4,
         typename P5, typename P6,         template <class, class> class ThreadingModel>
    class FunctorImpl<R, LOKI_TYPELIST_6(P1, P2, P3, P4, P5, P6), ThreadingModel>
         : public Private::FunctorImplBase<R, ThreadingModel>
     {
     public:
         typedef R ResultType;
         typedef typename TypeTraits<P1> :ParameterType Parm1;
         typedef typename TypeTraits<P2>::ParameterType Parm2;
         typedef typename TypeTraits<P3>::ParameterType Parm3;
        typedef typename TypeTraits<P4>::ParameterType Parm4
        typedef typename TypeTraits<P5>::ParameterType Parm5;
        typedef typename TypeTraits<P6>::ParameterType Parm6;
         virtual R operator()(Parm1, Parm2, Parm3, Parm4, Parm5, Parm6) = 0;
    };
}
 #endif
         // operator() implementations for up to 15 arguments
                 
         ResultType operator()()
         { return f_(); }
 
         ResultType operator()(Parm1 p1)
         { return f_(p1); }
         
         ResultType operator()(Parm1 p1, Parm2 p2)
         { return f_(p1, p2); }
         
        ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3)
        { return f_(p1, p2, p3); }         
         ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4)
         { return f_(p1, p2, p3, p4); }
         ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5)
        { 
return f_(p1, p2, p3, p4, p5); 
}
        
 ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
            Parm6 p6)
        { return f_(p1, p2, p3, p4, p5, p6); }
         
3 // class template FunctorHandler
 // Wraps pointers to member functions

    template <class ParentFunctor, typename PointerToObj,
         typename PointerToMemFn>
    class MemFunHandler : public ParentFunctor::Impl
     {
         typedef typename ParentFunctor::Impl Base;
 
   public:
         typedef typename Base::ResultType ResultType;
         typedef typename Base::Parm1 Parm1;
        tyedetypename Base::Parm2 Parm2;
      typedef typename Base::Parm3 Parm3;
        typedef typename Base::Parm4 Parm4;
         typedef typename Base::Parm5 Parm5;
        typedef typename Base::Parm6 Parm6;
   MemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn) 
01072         : pObj_(pObj), pMemFn_(pMemFn)
01073         {}
01074         
01075         LOKI_DEFINE_CLONE_FUNCTORIMPL(MemFunHandler)
01076 

 #ifdef LOKI_FUNCTORS_ARE_COMPARABLE
         bool operator==(const typename Base::FunctorImplBaseType& rhs) const
       {
            if(typeid(*this) != typeid(rhs))
                 return false; // cannot be equal 
            const MemFunHandler& mfh = static_cast<const MemFunHandler&>(rhs);
             // if this line gives a compiler error, you are using a function object.
             // you need to implement bool MyFnObj::operator == (const MyFnObj&) const;
            return  pObj_==mfh.pObj_ && pMemFn_==mfh.pMemFn_;
         }
 #endif   
         ResultType operator()
       { return ((*pObj_).*pMemFn_)(); }
 
         ResultType operator()(Parm1 p1)
         { return ((*pObj_).*pMemFn_)(p1); }
       ResultType operator()(Parm1 p1, Parm2 p2)
         { return ((*pObj_).*pMemFn_)(p1, p2); }
          ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3)
         { return ((*pObj_).*pMemFn_)(p1, p2, p3); }
       
         ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4)
        { return ((*pObj_).*pMemFn_)(p1, p2, p3, p4); }
         ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5)
       { return ((*pObj_).*pMemFn_)(p1, p2, p3, p4, p5); }
         
         ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5,
            Parm6 p6)
        { return ((*pObj_).*pMemFn_)(p1, p2, p3, p4, p5, p6); }        
        // operator() implementations for up to 15 arguments

        ResultType operator()() const
        {
             LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
            return (*spImpl_)(); 
         }

        ResultType operator()(Parm1 p1) const
         { 
             LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
             return (*spImpl_)(p1); 
        }
         
         ResultType operator()(Parm1 p1, Parm2 p2) const
       {    
             LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
            return (*spImpl_)(p1, p2); 
        }
                 ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3) const
{    
             LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
          }
     return (*spImpl_)(p1, p2, p3);     
       ResultType operator()(Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4) const
        { 
            LOKI_FUNCTION_THROW_BAD_FUNCTION_CALL
             return (*spImpl_)(p1, p2, p3, p4); 
         }
 private:
         Fun1 f1_;
        Fun2 f2_;
    };
      template <class Fun1, class Fun2>
     Fun2 Chain(
       const Fun1& fun1,
         const Fun2& fun2)
     {
         return Fun2(std::auto_ptr<typename Fun2::Impl>(
            new Chainer<Fun1, Fun2>(fun1, fun2)));
    }
 


Related Solutions

Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
Write a JAVA program that implements the following disk-scheduling algorithms: FCFS
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
How to Connect an Android Application to Firebase Realtime Database and write a simple database in...
How to Connect an Android Application to Firebase Realtime Database and write a simple database in Java. Please attach screenshots of the activity performed.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a python program. The function protocol implements the functionality of the diagnostic protocol. The main...
Write a python program. The function protocol implements the functionality of the diagnostic protocol. The main idea of this function is to call the functions that you have already implemented in the previous questions. See below for an explanation of exactly what is expected. def protocol(my_symptoms: Tuple[Set], all_patients_symptoms: Dict[str, Tuple[Set]], all_patients_diagnostics: Dict[int, str]) -> str: '''Return the diagnostic for my_symptoms based on the dictionary of patients symptoms (all_patients_symptoms) and the dictionary of patients diagnostics (all_patients_diagnostics). Hint: This function selects the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT