• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set Arrays in prepared Statement and read in more than one checked box values ?

 
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:This is why I've been trying to get your to think about your database model.
I still don't know what this is modelling.

Anyway,
the general batching, assuming a SQL like:


Note, that assumes an auto-increment subject_id field.

Anyway, using a PreparedStatement called ps:

That assumes you have an array of subjects and an id for the tutor.



Hi Dave,

Can I know in this case, there is no need for me to implement the DAO and DAOImpl already right ?

I have also been trying to study other others are doing and it seems that they can just do it all together in the controller, without the trouble of creating interfaces and then write out all the methods before pulling out the methods blah blah blah...

And it seems that having DAO and DAOImpl in my case just makes the code more difficult to write, especially the controller part when you create a new instance of the manager class where the insertMethod() is being called.

Hope to have your comments on this ...
 
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
You can do all your code in one method.
That does not mean it is a good idea.

I would create a DAO etc, and this would be an assignSubjectsToTutor method, with a String[] and int as parameters (subjects and tutor id).
That way you can test it in isolation.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:This is why I've been trying to get your to think about your database model.
I still don't know what this is modelling.

Anyway,
the general batching, assuming a SQL like:


Note, that assumes an auto-increment subject_id field.

Anyway, using a PreparedStatement called ps:

That assumes you have an array of subjects and an id for the tutor.



Hi Dave,

Here's the ER Diagram.

Could you advice how I can write my insert statement and how to handle the insertion of list of subjects selected by users into the table ?

Furthermore, I have used List<subject>subjects in my model subject.

I have further googled and found that addBatch() does not seem to apply in my design.

I am having problem in the controller where I want to insert both the tutor table and the subjects table data together.  How should I go about achieving that ?
ER-Diagram.png
 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
In what way does batching a load of related INSERTs not apply?

About your model.  So you can have multiple entries in your Subject table for, say, "Chemistry"?
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:In what way does batching a load of related INSERTs not apply?

About your model.  So you can have multiple entries in your Subject table for, say, "Chemistry"?



Yes.  I can have more than one subject entered into the Subject table.  So, the person can choose Chemistry, Physics etc etc and it will all entered into the table.

Here's how I write my sql and controller but it doesn't seem to work....I am thinking of using JPA annotation which will be easier..what do you think ?







There is no error after I submitted the form but the data is not inserted into the database.

What has gone wrong ?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
I can't follow that code easily, since the indentation is a bit out.

Anyway, so you would expect the output in your Subject table to be something like:

just so I know what you're aiming at.

As for the SQL,
Have you tried using that SQL directly against your database?
Because it looks like a mix between an INSERT and an UPDATE to me.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:I can't follow that code easily, since the indentation is a bit out.

Anyway, so you would expect the output in your Subject table to be something like:

just so I know what you're aiming at.

As for the SQL,
Have you tried using that SQL directly against your database?
Because it looks like a mix between an INSERT and an UPDATE to me.



Hi Dave,

Sorry I only see this message now while looking for a solution in my latest 404 problem in this site.

Bingo.  You are right.

So, you are saying I tried out using the INSERT and follows by Update for the above table to appear like that in my WebApp/database ?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
That's not a very good data structure for the table.
You have multiple 'Maths' subjects.

This is what I've been driving at.
Having multiple Subjects that are the same.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:That's not a very good data structure for the table.
You have multiple 'Maths' subjects.

This is what I've been driving at.
Having multiple Subjects that are the same.



Hi Dave,

First, I would like to thank you for being so patient to me and teaching me something I guess easy for other people but really I am completely lost.  

However, based on your latest reply, I still have no idea how should my bridging table should be like in my case.

Could you kindly let me know how the data structure is ?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
If it were me, the first model that comes to mind for a Tutor Subject relationship is:

Tutor > - < Subject

That is Many-to-Many.

This supposes that a single subject is represented by a single row in the Subject table, so only one Maths subject, or one Physics, etc.

So Subject, at a minimum, would have name as the basic start point, which would be the Primary Key.  You might prefer to have an id field as well, but it's not actually necessary if we're ensuring the names are unique.
Tutor would have an id and name, with id as the Primary Key.
Finally the Many-to-Many would be mapped as a table (Tutor_Subject is fine) which would have a tutor_id and a subject_name.  The Primary Key would be both columns.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:If it were me, the first model that comes to mind for a Tutor Subject relationship is:

Tutor > - < Subject

That is Many-to-Many.

This supposes that a single subject is represented by a single row in the Subject table, so only one Maths subject, or one Physics, etc.

So Subject, at a minimum, would have name as the basic start point, which would be the Primary Key.  You might prefer to have an id field as well, but it's not actually necessary if we're ensuring the names are unique.
Tutor would have an id and name, with id as the Primary Key.
Finally the Many-to-Many would be mapped as a table (Tutor_Subject is fine) which would have a tutor_id and a subject_name.  The Primary Key would be both columns.



But, that is what I have been saying all along that is my Model and why did you say it is wrong ?

Subject table
---------------
Sub_ Id         Sub_Name

1                  Maths
2                  Physics
3                  this could be Maths again because 1 and 2 is entered into this Subject tutor because the tutor has chosen Maths and Physics so the 3rd one which is Maths again is by the 2nd tutor, not the first

Isn't that is what my Subject table suppose to be ?

Hope to have your clarification and confirmation that my Subject table design is correct.



 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
In which case it is not the same model, as my definition of Subject above prevents you creating two Maths Subjects.

Why is the Maths taught by Tutor 1 different to the one taught by Tutor 2?
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:In which case it is not the same model, as my definition of Subject above prevents you creating two Maths Subjects.

Why is the Maths taught by Tutor 1 different to the one taught by Tutor 2?



I think I am roughly getting what you are/were saying now :

Kindly confirm.

You want me to put all the Subject Names  like Maths, Physcis, Chemistry(all unique) like that into a table is that right ?

In that case, my third table teaches subject kindly confirm is it like this ?

tutor Id         Subject Name
---------        -----------------
1                 Math
1                 Physics
2                 Math (So this will be same Math but different tutor Id)
2                 Chemistry

Kindly let me know if the data structure is correct.

Tks.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
If tutor_id and subject_name make the primary key of that table then yes, that would make more sense to me.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:If tutor_id and subject_name make the primary key of that table then yes, that would make more sense to me.



One more thing, then do I need a separate table to contain the subjects that the tutor have ticked in the checkbox ?

Or I just include the subjectName(per the ticked in the checkbox) that the tutor will teach all together in the tutor table ?

The subject table will just contain the subjects as per the check boxes for maintenance purpose, since this you said there should be a table like this.

 
Dave Tolls
Rancher
Posts: 4801
50
  • Number of slices to send:
    Optional 'thank-you' note:
You need a table to map a Tutor to multiple Subjects, and also a Subject to multiple Tutors.

That's the Tutor_Subject table above, with a tutor_id and subject_name.

Remember, this is all presupposing your subject checkboxes are driven from a Subject table.
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:You need a table to map a Tutor to multiple Subjects, and also a Subject to multiple Tutors.

That's the Tutor_Subject table above, with a tutor_id and subject_name.

Remember, this is all presupposing your subject checkboxes are driven from a Subject table.



Hi Dave,

I roughly get your point but what about the implementation ?

After I get the data of tutor information and the subjects they would like to teach to go into table 1.

I'll then have to implement method to get the infor from the table 1 and insert it into table 3 with the tutor_id and subject_name is that right ?
 
tangara goh
Ranch Hand
Posts: 1037
1
  • Number of slices to send:
    Optional 'thank-you' note:

Dave Tolls wrote:Well, I used 'name' since I was guessing you might be sending the subject name (which I would assume was unique).
You could always send a subject id.  That's up to you.

For the IN, the most secure way is to build the list of '?' placeholders and the do a load of setString (or setInt if using ids) on the PreparedStatement.

The initial answer on this SO thread is the usual way to build up the IN, using String.join and Collections.nCopies...it's a lot easier than it used to be!



Hi Dave,

I am writing to you again cos I need more explanation regarding the stackoverflow reference.
The thing is I have problem in understanding the methods explained there. The thing that puzzled me most is the Select sql statement.
Why use select if the data is not inserted in as yet?

The thing is that I am going to insert the List<String> together with other strings like tutorName, email etc into the table Tutor so it is not inserted yet and so why use Select ?

Hope you can explain to me.

Tks!

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |