Telson Alva
Telson Alva

Reputation: 862

SQL WHERE LIKE clause in JSF managed bean

Hi i have this managed bean where it makes MySQL queries, the problem here is the SQL statement makes a '=' condition instead of 'LIKE'

Here is the code in my managed bean.

    Connection con = ds.getConnection();
        try{

          if (con == null) {
            throw new SQLException("Can't get database connection");
            }
        } 
        finally {    
        PreparedStatement ps = con.prepareStatement(
                "SELECT * FROM Clients WHERE Machine LIKE '53'");
        //get customer data from database
        ResultSet result = ps.executeQuery();
        con.close();
        List list;
    list = new ArrayList();

    while (result.next()) {
        Customer cust = new Customer();

        cust.setMachine(result.getLong("Machine"));
        cust.setCompany(result.getString("Company"));
        cust.setContact(result.getString("Contact"));
        cust.setPhone(result.getLong("Phone"));
        cust.setEmail(result.getString("Email"));
        //store all data into a List
        list.add(cust);
    }

    return list;

Here the SELECT command does not pull all the numbers in 'Machine' column which is like 53, but if i enter a whole value, such as the complete number (53544) in place of 53 then the result is pulled up. I am confused !!

Also if i replace the above select statement with SELECT * FROM Clients the entire database is stored in list. Any ideas ?

Upvotes: 0

Views: 840

Answers (2)

user1300630
user1300630

Reputation:

Use wildcards:

Like '%53%'

...means everything that contains '53'.

Like '%53' - it ends with 53
LIKE '53%' - it starts with 53

You can also use _ if You want to replace a single character.

You can find a descriptipn HERE

Upvotes: 1

Pawan
Pawan

Reputation: 1075

You sql query should be

"SELECT * FROM Clients WHERE Machine LIKE '%53%'

Upvotes: 1

Related Questions