File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JDBC
Reply Bookmark "SQL" Watch "SQL" New topic
Author

SQL

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I am having a couple of problems with SQL.
First Problem:
Here's my SQL statement:


Why can't this SQL statement bring in a record with an apostrophe in it??(i.e- Mervyn's). Keeps telling me "Incorrect Syntax".
Second Problem:

There is a record I have in SQL that has an "S" in this column. But it doesn't seem to recognize it. I printed to standard out to verify that the value is an "S" and it is. The datatype in SQL for this column is a char.

Am I going crazy?? I must be missing something.
Any help on these issues would be much appreciated!
Thanks in advance!
[ April 04, 2002: Message edited by: Jennifer Sohl ]
[ April 04, 2002: Message edited by: Jennifer Sohl ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The easiest way to solve your first problem is to use a PreparedStatement rather than a Statement - it will take care of the special characters for you (sample code available by request)
Your second problem is a common Java mistake, you are testing equality of the java objects, not their values. You need rs.getString(5).equals("S") rather than rs.getString(5) == "S".
Dave


[ JavaRanch FAQ ][ Book Promotions ][ DbTamer ][ BumperStickers ][ JavaRanch Badges ]
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the reply! Well, DUH!! I can't believe that I didn't notice I was trying to compare objects rather than values. Thanks for pointing that out.
If it's not too much trouble, could you post some sample code to show me how to do the Prepared Statements? I am not yet familiar with them. Thanks again for your help!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Hi Jennifer
Prepared statemtns are basically precompiled SQL statements that can be used to speed up processing of the queries when just a few of the parameters of the statement are going to change.
You create them the same as you do a regular Statment object except that you have to include the SQL text in the constructor:
PreparedStatement pStmt = conn.prepareStatement(“SELECT * FROM employees WHERE lname = ?” ;) ;
The question mark represents the parameter to be supplied later. You can have multiple parameters and then later, when you start working with them, they are all referenced with an index starting at 1 (the first parameter is 1 the second is 2 and so on).
Now when you go to use it you have to:
1 clear any existing parameters
--- pStmt.clearParameters( );
2 set the values of any parameters in the statement
--- pStmt.setString(1, “O'Connor” ;) ;
3 execute the statement as normal
--- pStmt.executeQuery();
There are multiple setXXX() methods that all take a parameter index as their first argument and the value being assigned as the second. To assign null, use the setNull() method the first arg would be the parameter index the second is the type of the argument that is being set to null, like this:
pStmt.setNull(2, Types.INTEGER);
Then you just process the results as normal.
hope that helps you out, if you need more details let me know...
[ April 05, 2002: Message edited by: Dave Vick ]

Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Dave Vick:
You can have multiple parameters and then later, when you start working with them, they are all referenced with an index starting at 1 (the first parameter is 1 the second is 2 and so on).

This is definitely worth stating twice...
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hello. Me again. I am having another problem with this darn database stuff.
Here is my code:

(mu is a JComboBox);
I have printed 'mString' to std out, and it shows exactly what is in the table. I can't figure out why I keep getting a NullPointerException on the line "while(rs.next)"?
I am stumped.
Could someone help me out... Again?
Sorry for being a pest. Thanks in advance!
Braj Prasad
Greenhorn

Joined: Apr 08, 2002
Posts: 16
You aren't fetching the executeQuery into the result Set. See below,
ResultSet rs = stmt.executeQuery("SELECT UNITID FROM MARKETINGUNITS WHERE DESCRIPTION = '" + mString + "'");
And you will be fine.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
AHA. I see that very clearly now. I must be having a bad week to make such stupid mistakes.
Thanks for helping me out!!
 
 
subject: SQL
 
Threads others viewed
JComboBox and limitations
Updating a result set
problem while insert date data in mysql
getDate() returning odd value
NoClassDefFound error
IntelliJ Java IDE