• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone please correct my method ?  RSS feed

 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts,

I have a number of problems in writing the following method which will return an array of ids.   Hope someone can help me correct my method so that the return is an array of Ids.

First, for the sql query.  Since, I am inserting a generated return key into my table - subject-teach, which has 2 columns - tutorId and subjectId. 

I am doing the method below for the subjectId which is in an array since there are more than one subject.





 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not see a quickfix solution for this. you need to try this on paper first.

methods will work out if you set the sequence correctly. basically for a group of values, you would need something like IN, BETWEEN etc to execute your statement.

your problem doesn't look to be code, it is applying your sql query to this code.

resolve the query first and then come to code
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your 'subject' class (ought to be Subject) have an array of ids?
Surely a subject object should have just the one id?

Is this adding a new subject?
In which case why are you getting the id from an array?
The database (if I remember correctly this is an Oracle db) will have an auto generated key, so you don't need it at all.

Can you describe, in steps, exactly what you are trying to do here?
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Why does your 'subject' class (ought to be Subject) have an array of ids?
Surely a subject object should have just the one id?

Is this adding a new subject?
In which case why are you getting the id from an array?
The database (if I remember correctly this is an Oracle db) will have an auto generated key, so you don't need it at all.

Can you describe, in steps, exactly what you are trying to do here?


I am using the Subject (the object) to call the array of ids cos ids for this object is an array.

thus I have used s is an instance variable of Subject which is my model class.

Is there anything wrong here ?
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would expect Subject to map to your Subject table, which I would expect to have an ID and a NAME (for example).
So Subject would be:


An array of ids doesn't make much sense, unless this is modelling something else.
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, I would expect Subject to map to your Subject table, which I would expect to have an ID and a NAME (for example).
So Subject would be:


An array of ids doesn't make much sense, unless this is modelling something else.


Hi Dave,

Initially, I had the same Subject class as what you have written.

But, I realised that my id for Subject in my method should be an array (since a tutor can enter in the form that he submitted a few subjects at a time )so I am stuck.

If I need the method to return an array, should I still keep my class as POJO ; meaning without int[]id and getter and setter for int[]id ?

 
Mohammed Sardar.
Ranch Hand
Posts: 194
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String qry = INSERT_QRY1; // not very sure what this qry should be
 

Hi

We can use CallableStatement and pass the same array of id to the Stored procedure/Function to your in sql package. The array of id that we can pass from Java can be fetched in Oracle as a VARCHAR2_ARRAY which is same array object.

I'm not sure if there is any possibility for a single INSERT Query to insert a multiple values at the same time.



Once you receive the array of subject id in sql it can looped in any looping statement and can be inserted using INSERT Query based on the count of subjects.

Hope this helps.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tangara goh wrote:
But, I realised that my id for Subject in my method should be an array (since a tutor can enter in the form that he submitted a few subjects at a time )so I am stuck.

If I need the method to return an array, should I still keep my class as POJO ; meaning without int[]id and getter and setter for int[]id ?



That's the issue.
A Subject should be just that, a single Subject.
If the Tutor selects multiple Subjects then the Tutor object should have multiple Subjects...that is, it's the Tutor that has the Subject array (or List, preferably).
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
tangara goh wrote:
But, I realised that my id for Subject in my method should be an array (since a tutor can enter in the form that he submitted a few subjects at a time )so I am stuck.

If I need the method to return an array, should I still keep my class as POJO ; meaning without int[]id and getter and setter for int[]id ?



That's the issue.
A Subject should be just that, a single Subject.
If the Tutor selects multiple Subjects then the Tutor object should have multiple Subjects...that is, it's the Tutor that has the Subject array (or List, preferably).


Hi Dave,

Thanks for highlighting that to me.  Really appreciate it.

So, I will have to add Subject subject to the class tutor is that what you meant?

And then add List<tutor>subjects and getter setter in the class tutor ?

Because I think I have messed up my code by adding List<Subject>subj in the class Subject follows by getter and setter there....
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the List<Subject> would be in the Tutor.



Is the list of Subjects fixed, on the page that Subjects are assigned to a Tutor?
If so, then you should already have a populated Subject table, with ids, so your GUI would be sending those Id's, and not the Subject name.
So the Tutor model might simply be:
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Yes, the List<Subject> would be in the Tutor.



Is the list of Subjects fixed, on the page that Subjects are assigned to a Tutor?
If so, then you should already have a populated Subject table, with ids, so your GUI would be sending those Id's, and not the Subject name.
So the Tutor model might simply be:


the Subjects are not fixed.  That is the problem. 

I am now using
private static List<Integer> subj_id in my class Subject though.

The reason is that I need to iterate over the subj_id and get the generated Keys which will be different size each time.

Not sure if it is ok to do it this way ?
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends how the rest of it works.

So on the page where Subjects are assigned to a Tutor the subject field(s) are free text?
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:It all depends how the rest of it works.

So on the page where Subjects are assigned to a Tutor the subject field(s) are free text?


I am using a controller to do it


It did went in but I am doubtful about my code here cos it is not making use of the class Subject....

Really messy....Hope to have your guidance on this. 

 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I asked, though, where do the subject names come from that get assigned to a tutor?
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:As I asked, though, where do the subject names come from that get assigned to a tutor?


Hi Dave,

I thought my last reply is obvious that the subject names come from a HTML form ?

 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But where does the form get them?
Are they hardcoded checkboxes (for example) or do they come from free text fields?

Because I would expect Subjects to be a limited number of predefined things to select, and would come from a database table.
Which would solve most of your current issues.
 
tangara goh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:But where does the form get them?
Are they hardcoded checkboxes (for example) or do they come from free text fields?

Because I would expect Subjects to be a limited number of predefined things to select, and would come from a database table.
Which would solve most of your current issues.


Obviously, the form must have given a list of subjects whether is in radio button or drop down list for them to choose from.

And the user can choose as many as they like.

Hope that it is now very very clear to you already.
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.
So if there's a list of subjects then is that list from the database?

That way you get a nice id back, and all your current code has to do is create a Tutor object, and then create the set of tutor/subject links based on the tutor_id and all the subject_ids in the request.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!