• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc code where i have to write in best programming way

 
ajay atthuluri
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know java upto servlets and jsp, i have a problem that where i have to write the "jbdc logic" in the project in "good programming way"......
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All database access should be as far away as possible from the View and Controller layers in a layer know as the Model.

Your Model should know nothing of servlets or any UI layer.

Please read this article.
 
jits Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is always best to write database connectivity code in a separate java file. It may be a servlet or a simple java class as per MVC standard.Database connectivity code may exist in jsp file but thats really ugly way of programming as jsp file is meant for the view component of MVC pattern which is responsible for generating the interface to the user
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting database code in a servlet does not follow best MVC practices. That places the database code in the controller, not the model.
 
jits Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not saying or insisting that database code should always be in a servlet but just in case when the application size is not large it can be placed in a servlet which is far much better than putting the code in a jsp file and yes putting java code in a separate java file is always the best way
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jits Singh wrote:I am not saying or insisting that database code should always be in a servlet but just in case when the application size is not large it can be placed in a servlet which is far much better than putting the code in a jsp file and yes putting java code in a separate java file is always the best way

While it is "better" to put JDBC in a servlet that a JSP -- which is completely egregious -- I'll disagree that it's ever ok, even in small apps. Small apps have a way of becoming big apps. And, it's a good idea to always do things the right way; it develops good habits
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^+1
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ ajay -- your database will be your "Model" part of your applicaion.

So just create a seperate package structure in which defined classes for making jdbc connection, calling your SQL queries etc.
And then use these classes in your "Controller" part of your application (Action classes in case of Struts , Controller classes in case of Spring MVC, servlets in case of simple Servlet/JSP base web application).

~ abhay

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote:@ ajay -- your database will be your "Model" part of your applicaion.

So just create a seperate package structure in which defined classes for making jdbc connection, calling your SQL queries etc.
And then use these classes in your "Controller" part of your application


Again, no!

The Controller and the View are part of the user interface. The Model consists of the classes that control data access, not just the DB. The data access classes should not be part of the Controller.

The Model should be agnostic of the UI. The View and Controller should be agnostic of the data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic