| | |
SqlException
![]() |
•
•
Join Date: Dec 2009
Posts: 59
Reputation:
Solved Threads: 0
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
don't get it the Sql statement looks fine but when I run it gives me this exception:
Thamks for your support!
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
Java Syntax (Toggle Plain Text)
public ArrayList searchPatient(String patientNum) { try { String sql = "SELECT fname, sname, location, dob, dor, race, gender, status, initials, idnum FROM PatientTable WHERE patientNo = " + patientNum; // Create a prepared statement Statement s = con.createStatement(); String pno = ""; String firstname=""; String lastname=""; String locate = ""; String dOB=""; String dOR=""; String Race=""; String Gen=""; String Stat=""; String Initial=""; double id; ResultSet rs = s.executeQuery(sql); while(rs.next()) { pno = rs.getString(1); firstname = rs.getString(2); lastname = rs.getString(3); locate=rs.getString(4); dOB = rs.getString(5); dOR = rs.getString(6); Race=rs.getString(7); Gen=rs.getString(8); Stat=rs.getString(9); Initial=rs.getString(10); id = rs.getDouble(11); //Create a PatientInfo object PatientInfo patient = new PatientInfo(id, pno, firstname, lastname, locate,dOB, dOR, Race, Gen, Stat, Initial); //Add the patient object to array list patientList.add(patient); } }
don't get it the Sql statement looks fine but when I run it gives me this exception:
Java Syntax (Toggle Plain Text)
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
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.
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?!
Sacrifice is a painful, pure and beautiful thing.
Dammit, Jones, What the Hell Are Knoll Pointers?!
![]() |
Similar Threads
- java.sql.SQLException: Column 'XXXXXX' not found. (JSP)
- java.sql.SQLException: Column not found (Java)
- JSP :java.sql.SQLException: ORA-01722: invalid number (JSP)
- MySQL error java.sql.SQLException: Parameter index out of range (0 < 1 ). (Java)
- java.sql.SQLException: Invalid column type: 1111 (Java)
- System.Data.Sqlclient.Sqlexception: Line1 incorrect syntax at ']'. (ASP.NET)
- "System.Data.SqlClient.SqlException: Login failed for user 'VASANT\ASPNET'." (ASP.NET)
- java.sql.SQLException: Invalid column index (Java)
- System.Data.SqlClient.SqlException (ASP.NET)
- Error java.sql.SQLException: [Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle (Java)
Other Threads in the Java Forum
- Previous Thread: add and remove components...
- Next Thread: Java Applet Animated Gifs
Views: 301 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
access android applet application arguments array arraylist arrays binary build c++ chat class classes client code combobox compare compile component convert data database design detection draw eclipse error event exception file filei/o game givemetehcodez graphics gui helpwithhomework html ide image images input interface j2me java javafx jframe jpanel julia key linked linked-list list loop main method methods mobile netbeans newbie number object oracle os output parameter pattern phone pixel printing problem program programming project read recursion remote remove return robot scanner screen search server service set sms socket sort source sql string swing system test text thread time tree user






