SqlException

Reply

Join Date: Dec 2009
Posts: 59
Reputation: JBeginer7891 is an unknown quantity at this point 
Solved Threads: 0
JBeginer7891 JBeginer7891 is offline Offline
Junior Poster in Training

SqlException

 
0
  #1
Dec 17th, 2009
Someone please help!
I'm writing a program that enable a user to search for a patient Info when patient number is given.
here is the search code snippet
  1.  
  2. public ArrayList searchPatient(String patientNum)
  3.  
  4. {
  5. try {
  6. String sql = "SELECT fname, sname, location, dob, dor, race, gender, status, initials, idnum FROM PatientTable WHERE patientNo = " + patientNum;
  7.  
  8. // Create a prepared statement
  9. Statement s = con.createStatement();
  10.  
  11. String pno = "";
  12. String firstname="";
  13. String lastname="";
  14. String locate = "";
  15. String dOB="";
  16. String dOR="";
  17. String Race="";
  18. String Gen="";
  19. String Stat="";
  20. String Initial="";
  21. double id;
  22.  
  23. ResultSet rs = s.executeQuery(sql);
  24.  
  25. while(rs.next())
  26. {
  27. pno = rs.getString(1);
  28. firstname = rs.getString(2);
  29. lastname = rs.getString(3);
  30. locate=rs.getString(4);
  31. dOB = rs.getString(5);
  32. dOR = rs.getString(6);
  33. Race=rs.getString(7);
  34. Gen=rs.getString(8);
  35. Stat=rs.getString(9);
  36. Initial=rs.getString(10);
  37. id = rs.getDouble(11);
  38.  
  39. //Create a PatientInfo object
  40. PatientInfo patient = new PatientInfo(id, pno, firstname, lastname, locate,dOB, dOR, Race, Gen, Stat, Initial);
  41.  
  42. //Add the patient object to array list
  43. patientList.add(patient);
  44. }
  45. }

don't get it the Sql statement looks fine but when I run it gives me this exception:
  1. java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Thamks for your support!
Last edited by masijade; Dec 17th, 2009 at 7:35 am. Reason: fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,761
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 491
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
0
  #2
Dec 17th, 2009
Seems to be a problem with the way your query is created; what's the JDBC type of `patientNo'? Is it a VARCHAR? If yes, then you need to wrap the passed in patient number in single quotes when constructing the query. If you don't, your database engine considers the passed in patient number as some kind of identifier or parameter and hence the given error.

BTW, your code is vulnerable to SQL Injection. Try passing in "xxx' or 1=1--" as patient number and watch all the rows being fetched instead of the one you requested. Use PreparedStatement instead of normal statements to save yourself from the trouble of escaping and quoting your input as well as SQL Injection.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 301 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC