Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
tangara goh
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Dave Tolls
Ranch Hand
Posts: 2551
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for ER-Diagram.png]
 
Dave Tolls
Ranch Hand
Posts: 2551
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2551
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!