In: Computer Science
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
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))); }