• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access connection object in user defined function inside a servlet  RSS feed

 
Bhardwaj Shweta
Ranch Hand
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
I have a servlet with one standard function and some user defined functions.

in protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {} i want to call some user defined functions. like add(which will add data in database)



now in add () it cannot find symbol pst. how can i use these objects in my user defined functions ???
please help

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhardwaj Shweta wrote:now in add () it cannot find symbol pst. how can i use these objects in my user defined functions ???

Either pass 'pst' variable to add function OR best way to create it locally in add method.
 
Devaka Cooray
Marshal
Posts: 5284
552
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that connecting to the database right from the servlet is not a good practice. You need to centralize all the database stuff into a separate set of java classes.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you've fixed pst you're going to hit the same problem with con. Variables defined in a method are only visible within that method (that's their 'scope'). I would suggest declaring pst locally in add as Sagar says, and passing con into the method as an argment.

(Edit: Devaka's right, of course, if you want to design it properly)
 
Bhardwaj Shweta
Ranch Hand
Posts: 36
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. As I am new to jsp and servlets i dont know the correct way.let me try this and in case of any problem i will get back to you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!