Welcome to Dream.In.Code
Become a Java Expert!

Join 149,491 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,287 people online right now. Registration is fast and FREE... Join Now!




Java and SQL Like Statement

 
Reply to this topicStart new topic

Java and SQL Like Statement, Correct syntax for a LIKE with java variables.

Deeko
20 Mar, 2007 - 02:42 AM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2005
Posts: 30


My Contributions
Hi there,

Im having a problem with an SQL query with regards to a LIKE operation in the statement in my Java program. Basically I have a search function in my stock control program to search for a part either by part number or description and output the results to a table. The SQL statement pulls in the data from two string variables. The trouble is that only the description part of the search works as searching for a part number just outputs the whole database content of all the parts. I just require that both searching by either part number OR description works. Ive tried for many hours on this now by adding in brackets and such and enclosing the OR part of the SQL statement but nothing works. I belive my syntax must be wrong or maybe I cant execute an SQL statement with WHERE, OR and LIKE in the one query. Any help would be much appreciated. Thank you.

Heres is the Search GUI to help you see what I mean:

IPB Image

Here is the code:
CODE

Connection conn = connection;
        String Inumber = this.txtInumber.getText();
        String Description= this.txtDescription.getText();
        
        //create the table and data initialisation
        try {
                //read data from table   this.connection = conn;
                ResultSet  res = executeQuery( "SELECT * FROM mainpage where partno = '"+Inumber+"' OR description LIKE '%"+Description+"%'  ");
    
                // count the number of records in the result set
                int counter = 0;
            
                //put data into object 2d array
                while( res.next() ){
                    counter++;
                }
            
                data = new Object[ 120][ 6 ];
                counter = 0;
               res = executeQuery( "SELECT * FROM mainpage where partno = '"+Inumber+"' OR description LIKE '%"+Description+"%'  ");
            
                while( res.next() ){
                
                    data[ counter ][ 0 ] = res.getString( "partno" );
                    data[ counter ][ 1 ] = res.getString( "description" );
                    data[ counter ][ 2 ] = res.getInt( "minstocklevel" );
                    data[ counter ][ 3 ] = res.getInt( "count" );
                    data[ counter ][ 4 ] = res.getInt( "quantity" );
                    data[ counter ][ 5 ] = res.getInt( "onorder" );
                
                    counter++;
                }
            
        }
        
        catch(Exception ex) {
                System.err.println( "Error in constructor -> " + ex );
        }
        
        tabStockItem.setModel(new javax.swing.table.DefaultTableModel( data, columnNames ) );
    }  

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Java And SQL Like Statement
20 Mar, 2007 - 03:03 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I think the problem is that when you search for a part number, your description string is empty, thus it matches everything (it becomes '%%'). Try to rethink the logic behind the SQL statement. Also, using PreparedStatement to inject variables into an SQL is usually preferable.
User is offlineProfile CardPM
+Quote Post

Deeko
RE: Java And SQL Like Statement
20 Mar, 2007 - 03:36 AM
Post #3

New D.I.C Head
*

Joined: 29 Nov, 2005
Posts: 30


My Contributions
Thanks for the quick reply 1lacca smile.gif I can see where Im going wrong , unfortunately Im out of time and cant add any extra lines of code, was hoping it was just my syntax that required an in-line fix. Thanks for the heads-up.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Java And SQL Like Statement
20 Mar, 2007 - 03:59 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
No problem.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month