Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need some help in MVC

 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just joined in a small company they are using some java code in jsp but i don't like to use java code in jsp. So, i ask permission to my M.D to develop an application M.V.C architecture Using only Servlets,java beans and JSP (No frame work) i am facing some problems like each time opening connection in servlet to connect the database and "setting primary key available to entire/part of some pages" in my application.
Please help me to develop the application and suggest some books and sites need to develop M.V.C pattern
Thanks
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you let us know what is the problem?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several books and articles on he MVC pattern on the internet. I am not exactly clear on your question though, but it sounds like you are opening a connection to the database on each request, and you are concerned right?
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is opening Connection in every Sevlet for each table and when i them close at end of servlet, in some jsp it's throwing some exception like statement is closed
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a connection in the JSP? The JSP shouldn't be doing any database access at all. If it is, your code is not following best practices.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not opening connection in jsp. I am opening connection in servlet and when i close the connection it is throwing exception like statement is closed
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't say anything about opening a connection in the JSP. You said:
, in some jsp it's throwing some exception like statement is closed

Why would your JSPs care about a connection? Database access should be over and done with before any JSP is forwarded to. Do you have Java code in your JSPs accessing a result set?

In fact, if you are doing any database access even at the servlet level, you are violating the precepts of MVC. All database access and information should be handled by the Model -- not the Controller or the View.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
model means i am using a java class like

//connecting to database


//connecting servlet

But it is always going to else statement even i enter the correct values in html page. wat is wrong
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your DAO compile like that? How can you return a result set as a boolean? Where do you close the connection? Why aren't you using a connection pool?
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then how to return result-set? I don't have any idea about connection pooling
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun don wrote:Then how to return result-set?

You never want to return a result set. Results sets should be closed and the statements and connections reclaimed as soon as possible. What data do you need from your DAO? It looks like all you want is a boolean indicating if the record exists or not, but that's not what your code is doing (and your SQL is highly inefficient for that).

I don't have any idea about connection pooling

Time to learn: connection pooling.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and your SQL is highly inefficient for that.


What is this means i can not understand please tell me
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point this has become much more about JDBC than Servlets, so this topic has been moved to the JDBC forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun don wrote:
and your SQL is highly inefficient for that.


What is this means i can not understand please tell me


If you only care whether the record exists or not, why fetch all the data with a "select *"? To check for existence a "select count(*)" will be more efficient.
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rs.next() What it returns if value is correct?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64845
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easy enough to check... give it a try.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mallikarjun don wrote:rs.next() What it returns if value is correct?

When you use count(*), there is no such thing as correct/not correct. rs.next() will always return true and then rs.getInt(1) gives you the number of records the count found. Possibly zero.
 
Rudy Gireyev
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
If you only care whether the record exists or not, why fetch all the data with a "select *"? To check for existence a "select count(*)" will be more efficient.
Hi Bear. I'm wondering if select count(*) would really be more efficient than say select username. I'm thinking that for select count(*) it has to read every row in the table no matter what, whereas for select username it will stop searching when the row is found.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic