I am running into an error when I try to execute my sql code when I try to find a Section by the CourseNumber and the SectionNumber. It says I have a datatype mismatch in my SQL criteria.
Problems with query/update : [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
Here is my SQL code that I am trying to use to get information from an access database.
Code:
String sql = "SELECT * FROM SECTIONS WHERE course_number= '" + s.getCourseNum()+"' AND section_number=" + s.getSectionNum();
I have been looking at this for about an hour and just cant see what I am doing wrong. My code is quitting when I try to execute my SQL Code which leads me to believe that I have a problem with my code. Any suggestions... If you need to see the full program or where I am calling the method that I wrote, post and I will zip it or post it and let you see it.
The error is in your SQL statement - you are trying to compare two different data types (eg: a String against a numeric field).
What data types are your course_number and section_number fields?
For String/Text fields, you need single quotes around the value (as you have for course_number), and for numeric data types you dont (as you have for section_number).
Course_Num is a text field. Section_Num I believe is an int. I dont have access to check though. I will post the database in a zip file here so if anyone has it they can check.
The way that it should work is that I send a section object to the method than use the coursenumber and the sectionnumber of that object to find the rest of the information for that section.
A course number looks like this
INFO1311
And a section number is just how many sections of that course are currently there.
I got it to work, but I just wish I could figure out what was wrong with my first SQL statement. I went ahead and used a Prepared statement and just set each of the variables that way.
I am getting another error though in my update method, but I will try to troubleshoot. I will try to use another prepared statement. I dont have alot of experience with them, but I do like their uses though. Do you have any websites that show in depth how to use them. I have googled it and found a few but most werent the indepth that I like. I have read about them in the docs though.
Here was the code that worked.
Code:
String sql = "SELECT * FROM SECTIONS WHERE course_number=? AND section_number=?";
ResultSet rs = null;
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1,s.getCourseNum());
statement.setInt(2,s.getSectionNum());
rs = statement.executeQuery();
I just looked at the table in Design view - and both of those fields are Text.
This means that for a "normal" SQL statement you should be using single quotes for both fields, and for your prepared statement you should be using setString for both.
Prepared statements have a few advantages (such as no problems with quotes inside data), and are a good way to go. I'm afraid I dont have any articles I can link to that explain them in detail tho.. hopefully one of the Java guys will know something useful and post it!
There's nothing in-detail to know about prepared queries. They contain simple unnamed positional parameters that you replace with your own values, without caring for escaping.
Also, some database systems can optimize the query when you prepare it, so you can execute it multiple times with different parameters, which will be faster than repeatedly executing a new query.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
There's nothing in-detail to know about prepared queries. They contain simple unnamed positional parameters that you replace with your own values, without caring for escaping.
Also, some database systems can optimize the query when you prepare it, so you can execute it multiple times with different parameters, which will be faster than repeatedly executing a new query.
The book that I am reading doesnt mention anything of great detail about prepared statements. It just uses Statement objects so thats why I wanted something a little more indepth. Can you guys recommend any books that may be of help?
Perhaps a book specializing on JDBC? As I said, there's nothing special about prepared statements. You couldn't fill more than 15 pages with it, and that's if you supply a lot of code examples.
Here's the Sun online tutorial about it: http://java.sun.com/docs/books/tutor.../prepared.html
In my opinion, though, they focus too much on the repeated execution feature and too little on the parameter feature. If people would always use parameters for everything, the problem known as SQL injection wouldn't exist!
I just looked at the table in Design view - and both of those fields are Text.
This means that for a "normal" SQL statement you should be using single quotes for both fields, and for your prepared statement you should be using setString for both.
I see now that they are both text, but my question is why did it work then, since I am setting an int and not a string for section_num. Does it do some sort of auto cast in the background?
It's a bad thing, as it means your code will appear to work until you use a value that isn't valid (with non numerics in, or just a blank), at which point you will get an error in your code where you didnt before.