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 =...
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...
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
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...
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.
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets. Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations. The program understands the following arguments: -i insertion sort -s selection sort (default) -q quicksort -a (already) sorted dataset -v reverse-sorted dataset -r random dataset (default) -n no sorting x generate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT