Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets using JDBC

 
anubha goel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have made a servlet program in whch im trying to enter and search data in the database.my problem is that my search and insert buttons are not working.the data is getting inserted eachtime i press enter...so please tell me the proper code...my source code is:


Thanks in advance.

Regards,
Anubha.

[ August 08, 2007: Message edited by: David O'Meara ]
[ August 08, 2007: Message edited by: Bear Bibeault ]
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your logic always does an insert and then select each time you submit the form
So naturally your record count will increase on each submit.

If you have two buttons, one for insert and one for viewing the data,
you can use a hidden form field, and use that field to determine whether
that operation is an insert or select.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bye the way,

//} ResultSet rs = stmt.executeQuery ("select * from pubs.anubha.data1");


This is the line where you declare the ResultSet. Its commented..
How come you are compiling it?
 
anubha goel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am able to compile it,actually that error ocured while i was posting
my code i guess.how should i use the hidden form field?
please explain me in detail.

Thanks & Regards,
Anubha.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am able to compile it,actually that error ocured while i was posting
my code i guess.how should i use the hidden form field?
please explain me in detail.

You can use javascript for that.
1. Define a hidden field in the form as say,

2. Register an on click event for both the buttons.
3. In both the methods registered for both the buttons
say document.<your form name goes here>.operation.value = '1'; in one
and
document.<your form name goes here>.operation.value = '2';
4> write document.<your form name goes here>.submit();

In the servlet get the hidden parameter using request.getParameter()
and do the operation according to the value you get.

This should solve your problem.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity..
Why are you doing all of this from a servlet?

These days it is common practice to create a plain old Java object (POJO) to handle all of the JDBC stuff and then forward to a JSP for formatting the output.
 
Tony McClay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know some of the terms we use are new to some. The reference to POJO is a reference to a managed Object from a Java Persistence Provider. Most J2EE environments have one. The most common is Hibernate (Free Open Source). Sun includes Oracle Essentials JPA provider with Sun's Application Server. Weblogic calls it's JPA Provider KODO.

The point is, it makes this type of logic/code very simplistic.

It is worth the 3 days to understand Java Persistence Architecture JPA. It will cut your development time in 1/2 or more (I think 80% in my case), once you have your database model fully modeled.

Best of luck

Tony

Sun Certified Web Business Component Developer

Sun Certified Web Components Developer

Sun Certified Programmer for the Java 2 Platform
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, POJO mean Plain Old Java Object. It doesn't have to be managed by anything in particular. That's the entire point. You can use an existing persistance API, or you can build your own.

Don't think that you have to buy in to all the APIs published as part of J2EE, some of which are patently ridiculous.
 
Tony McClay
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addition:

You are correct in the term of POJO Plan Old Java Object. In today's world, Java classes annotated with persistence annotations along with a object framework to manage it for you in JPA Terms- a Persistence Context/Manager.


I just wanted to correct you on the JPA. Yes you do not have to buy into the Sun Specifications if you choose, but it is easily available, and confirmed to be a tested framework based on a common standard. Like choosing not to use JDBC, it's a choice. JPA is part of J2SE and J2EE and becoming more of a standard.

Also, Java Persistence Architecture (Which is one of many POJO Solutions available) is part of Java 2 Standard Edition starting in version 5.0. This means it is easily available to anyone as a base API like JDBC. [ kind-of javax.persistence.... ]

actually - J2SE 5.0 + .....

The Reference Installation is based on Oracle Essentials( JPA Provider), but many use Hibernate, which I believe is the most popular. I prefer Oracle Essential myself.

I hope this helps you make your coding/design decisions.

Tony
Sun Certified Web Business Component Developer
Sun Certified Web Components Developer for J2EE
Sun Certified Programmer for the Java 2 Platform
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony McClay:
I just wanted to correct you on the JPA.


Firstly, I never mentioned JPA specifically. Secondly, you cannot "correct" someone's opinion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic