473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

A simple doubt in Servlets related to ResultSet

157 100+
My requirement is such that any user must not be allowed to save the same record twice.

For this what I do is as soon as some user fills up the page and clicks 'Save' I invoke a Servlet which checks the information filled in that page with that of the existing records and if a match is found then a message is printed back saying 'record already exists,cannot save twice!'.

I am using PreparedStatement to execute a MySql query in my Servlet and an if-else block to distinguish between an existing record and a fresh record,roughly my code is something like this:-

Expand|Select|Wrap|Line Numbers
  1. String s = request.getParameter("StudentID");
  2. String s1 = request.getParameter("SubjectID");
  3.  
  4. PreparedStatement pst = con.prepareStatement(select * from abc where (studid=? && subid=?);); //Note: I dont want my table abc to have a duplicate studid-subid combination, one student can study only one subject at a time.
  5.  
  6. pst.setInt(1,Integer.parseInt(s));
  7. pst.setInt(2,Integer.parseInt(s1));
  8.  
  9. ResultSet rs = null;
  10. rs = pst.executeQuery();
  11.  
  12. if(rs != null)
  13. {
  14.     //means the record already exists in table 'abc' so dont save this record twice,instead alert the user'This Record already exists!'    
  15. }
  16. else
  17. {
  18.    //since ResultSet is null that means no matching record was found in table'abc' indicating that the user has entered a fresh record so carry on the  processing...
  19. }
Now I'm guessing that if the user-entered-studid+subid combination is a fresh one then 'rs' should be null since the query isn't fetching any matching combinations..but still the control executes the if-part.

My question is if some query doesnt yield a result then shouldnt the Resultset associated with it be null or perhaps empty?Is there a way of checking that?

..or should I rather check in a while-loop[while(rs.next())] instead of saying if(rs!=null)?

Please guide ..Thanks in advance!
Feb 3 '07 #1
6 1959
r035198x
13,262 8TB
My requirement is such that any user must not be allowed to save the same record twice.

For this what I do is as soon as some user fills up the page and clicks 'Save' I invoke a Servlet which checks the information filled in that page with that of the existing records and if a match is found then a message is printed back saying 'record already exists,cannot save twice!'.

I am using PreparedStatement to execute a MySql query in my Servlet and an if-else block to distinguish between an existing record and a fresh record,roughly my code is something like this:-

Expand|Select|Wrap|Line Numbers
  1. String s = request.getParameter("StudentID");
  2. String s1 = request.getParameter("SubjectID");
  3.  
  4. PreparedStatement pst = con.prepareStatement(select * from abc where (studid=? && subid=?);); //Note: I dont want my table abc to have a duplicate studid-subid combination, one student can study only one subject at a time.
  5.  
  6. pst.setInt(1,Integer.parseInt(s));
  7. pst.setInt(2,Integer.parseInt(s1));
  8.  
  9. ResultSet rs = null;
  10. rs = pst.executeQuery();
  11.  
  12. if(rs != null)
  13. {
  14. //means the record already exists in table 'abc' so dont save this record twice,instead alert the user'This Record already exists!'    
  15. }
  16. else
  17. {
  18. //since ResultSet is null that means no matching record was found in table'abc' indicating that the user has entered a fresh record so carry on the processing...
  19. }
Now I'm guessing that if the user-entered-studid+subid combination is a fresh one then 'rs' should be null since the query isn't fetching any matching combinations..but still the control executes the if-part.

My question is if some query doesnt yield a result then shouldnt the Resultset associated with it be null or perhaps empty?Is there a way of checking that?

..or should I rather check in a while-loop[while(rs.next())] instead of saying if(rs!=null)?

Please guide ..Thanks in advance!
Resultset returned is never null. See here.
Feb 5 '07 #2
abctech
157 100+
Resultset returned is never null. See here.
Yes r0, I got it now, Thanks!
I should have gone through the documentation properly!
Feb 5 '07 #3
r035198x
13,262 8TB
Yes r0, I got it now, Thanks!

I should have gone through the documentation properly!


Sometimes it is easy to overlook the docs. Most of the problems posted here are solved by a check on the docs.
Feb 5 '07 #4
abctech
157 100+
Sometimes it is easy to overlook the docs. Most of the problems posted here are solved by a check on the docs.
Yeah,thts true :)
Feb 5 '07 #5
dmjpro
2,476 2GB
u should use resultset.next() if it return false...then no records exist
else match found...................
Feb 5 '07 #6
abctech
157 100+
u should use resultset.next() if it return false...then no records exist
else match found...................
Yes,I referred the docs for the methods that I could apply here,its working now,but thanks for your response,

Cheers!
Feb 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: _link98 | last post by:
Problem: java ResultSet cursor from SQL/PL stored-procedure is FORWARD_ONLY. Is it possible to have ResultSet cursors from SQL/PL procedures to scroll forward and backwards? Perhaps I am missing...
3
by: Bit Byte | last post by:
I have written a custom servlet engine (and "wrapper" servlets) for some legacy code (C/C++) that I have. The servlets contain the bulk of my 1st 2 layers in a 3 tier architecture - i.e. data...
14
by: abctech | last post by:
Helo experts, I'm working on my first ever web application and I have some basic questions related to servlets if I may, I have 5-6 Java statements and preparedstatements in my servlet to...
1
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
6
by: dev24 | last post by:
Hi all, I am writing a simple method which when entered with an Int parameter performs an sql query, creates a result set, uses that resultset to get values from the object created by another...
8
by: cygsoft | last post by:
Hi, I have got the codings for inserting and displaying the records in the database. But i couldn't able to delete a specific row, which is created dynamically. i.e, if i give any one of the...
1
by: Kedar Kachare | last post by:
How to use Log4j to create log files using file appender in a Servlet? Also, how to make that Servlet read the log4j.properties file? I have tried following: My web.xml( I use Eclipse europa)...
0
by: ank99 | last post by:
hello...i m trying to run servlets(using GET) from wml page..... using apache tomcat 5.5 server and WinWap for Windows(version 3.2.1.28.)....its working fine as far as just to display wml form...
0
by: divyac | last post by:
I am creating an address book using php and mysql in which i need to create mailing labels from the list of address in my database.i need to group the addresses in a such a way that only 5 addresses...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.