This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes jdbc code where i have to write in best programming way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jdbc code where i have to write in best programming way" Watch "jdbc code where i have to write in best programming way" New topic
Author

jdbc code where i have to write in best programming way

ajay atthuluri
Greenhorn

Joined: Jul 18, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jits Singh
Greenhorn

Joined: Jul 21, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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

Joined: Jul 21, 2011
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

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

Joined: Jan 05, 2011
Posts: 477

^+1


Learning and Learning!-- Java all the way!
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1086
    
    1

@ 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


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jdbc code where i have to write in best programming way