2

I'm working on creating a Java interface for an SQL database. I'm using prepared statements to perform the search queries, however they only work with "=" statements and not with LIKE statements.

The first sample code using "=" works successfully. The second sample, using a LIKE statement, does not work and just returns empty.

WORKS:

    PreparedStatement SearchQuery = con.prepareStatement("Select * From Alumnus_A Where aFirstName = ? ORDER BY aLastName asc");  
    SearchQuery.setObject(1, FirstName); 
    ResultSet rs=SearchQuery.executeQuery();

DOESN'T WORK:

    PreparedStatement SearchQuery = con.prepareStatement("Select * From Alumnus_A Where aFirstName LIKE ? ORDER BY aLastName asc");  
    SearchQuery.setObject(1, FirstName); 
    ResultSet rs=SearchQuery.executeQuery();

Any help is much appreciated, also if you could explain it to me keeping in mind I'm very new to Java and don't have a lot of programming experience yet.

The rest of my search button's code:

private void button_SearchActionPerformed(java.awt.event.ActionEvent evt) {

    String FirstName = textField_FirstName.getText();
    String LastName = textField_LastName.getText();


    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:Alumni_DB");
        Statement st=con.createStatement();
        con.commit();


       System.out.print(FirstName + " ");


       PreparedStatement SearchQuery = con.prepareStatement("Select * From Alumnus_A Where        aFirstName LIKE ? ORDER BY aLastName asc");


       SearchQuery.setString(1, FirstName);


       ResultSet rs=SearchQuery.executeQuery();
       String aUID="",aFName="",aLName="",aMInitial="",aHomePhone="",aCellPhone="";

       while(rs.next())
        {
            aUID=rs.getString(1);
            aFName=rs.getString(2);
            aLName=rs.getString(3);
            aMInitial=rs.getString(4);
            aHomePhone=rs.getString(5);
            aCellPhone=rs.getString(6);
            System.out.println("UID " + aUID + "  First Name " + aFName + "   Last Name     " + aLName + "   Middle Initial " + aMInitial + "   Home Phone   " + aHomePhone
                    + "   Cell Phone " + aCellPhone);
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }

}

Note: Anything with the prefix 'a' is referring to a database column

MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • 1
    What is the type and value of `FirstName`? Have you tried `setString` instead of `setObject`? Have you tried the corresponding "like" SQL directly in your database? – rgettman Feb 07 '14 at 23:55
  • Maybe you mean to use `setString(int parameterIndex, String x)` for setting the `FirstName` since *maybe* the Type is String? Just like @rgettman was asking... – Alvin Bunk Feb 07 '14 at 23:59
  • I'm going to try that now! FirstName is a string I should have mentioned that, and the value is taken from a text box. – user3285901 Feb 08 '14 at 02:08
  • it didn't work :( I tried even just commenting out the 2nd line, and hard-coding the query for the name that worked previously and that doesn't go through either! – user3285901 Feb 08 '14 at 02:15
  • Does the `System.out.println(e);` give an error ? – Rida BENHAMMANE Feb 08 '14 at 17:47
  • Consider respecting the Java naming conventions: no underscore in variables and methods, variables and methods starting with a lowercase letter. And use the JDBC driver for your database, rather than the obsolete, buggy JDBC/ODBC bridge. – JB Nizet Feb 09 '14 at 07:55
  • @RidaBENHAMMANE No error message – user3285901 Feb 10 '14 at 15:50

0 Answers0