Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I check for record not found on my database 1

Status
Not open for further replies.

CdnRebel

Programmer
Apr 5, 2003
39
CA
I had this posted in the javascript forum because it's part of my javascript program, but someone replied to say that it's java code(?). I know response.sendRedirect is javascript, but I'm new at this so any help will do.

This is the code I have, but I haven't incorporated the record not found check in properly. How should it be done?

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:eek:dbc:DoctorLookUp","","");
Statement stat = conn.createStatement();
rs = stat.executeQuery("Select first_name, middle_initial, last_name, phone_number from Doctors Where attending_physician_id='"+physician_id+"'");
if not found then raise exception '' doctor id not found, re-enter id '', doctors;
msg = "Physician%20Id%20not%20found%20in%20the%20doctors%20database,%20please%20re-enter";
response.sendRedirect("/examples/jsp/DocDelete1.jsp?msg="+msg);
end if;

while(rs.next()){

att_physician_id=physician_id;
firstname=rs.getString("first_name");
middleinitial=rs.getString("middle_initial");
lastname=rs.getString("last_name");
phonenumber=rs.getString("phone_number");
 
There isn't a 'record-not-found' feature in JDBC like other data access methodologies where you might do an if rs == EOF type of thing.

You have two options that I know of:

1.) SELECT COUNT(*) as matches from Doctors Where attending_physician_id='"+physician_id+"'"
rsMatches.next();
if(rsMatches.getInt(1)==0) System.out.println("Oops, no match");

2.) Use a counter variable:
long recCount = 0;

while(rs.next()){
recCount++;
...
}
if (recCount==0) System.out.println("Oops, no records.");

Just an idea.
Oh, yeah, you might check out the PreparedStatement method
instead of using dynamic sql w/ embedded variables, you can
do something like:

PreparedStatment psDocs = conn.prepareStatement(
"Select first_name, middle_initial, last_name, phone_number from Doctors Where attending_physician_id=?");
psDocs.setString(physican_id);
rsDocs = psDocs.executeQuery();...
 
Thanks, dlbeyl. It's much more helpful when someone tells you how to go about doing what you want to do rather than have some one tell you that you what you are doing is wrong and that you can't do it that way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top