SQL LIKE无法正常工作

时间:2015-09-24 05:22:28

标签: java sql xml jsp

我知道以前曾经问过,但我无法理解我的工作是怎么回事。

我试过了

" WHERE REFERENCE_NUM LIKE '%'"+ whereClause+"'%'";
" WHERE REFERENCE_NUM LIKE '%1%'";
" WHERE REFERENCE_NUM LIKE '1%'";

和其他几个人。现在我知道这部分是问题所在,因为如果我将其改为

" WHERE REFERENCE_NUM = " + whereClause;

" WHERE REFERENCE_NUM = 1000";

它返回正确的数据。我完全失去了。

这样设置的原因是因为我遵循UNI的David parsons所着的“使用XML和Java进行动态Web应用程序开发”一书中的说明。但是我是一名外部学生。

    String bigWhereClause = "WHERE REFERENCE_NUM LIKE '1%'";
        Collection<Accom> accomAll = ModelFacade.getAccoms(bigWhereClause);

        numbeOfResults = accomAll.size();
        return accomAll;



   public static Collection<Accom> getAccoms(String whereClause){
    //if no 'where' clause is given create an empty string
    if(whereClause == null)
    {
        whereClause = new String();
    }
    AccomDAO adao = new AccomDAO();
    Collection<Accom> accomCollection = adao.readAccoms(whereClause);
    return accomCollection;
}



public Collection<Accom> readAccoms(String whereClaws){
    Collection<Accom> accoms = new ArrayList<Accom>();

    try{
        //connect to DB
        getConnection();
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery("SELECT * FROM ACCOM" + whereClaws);
        //setup for the accom
        int reference = 0;
        String type = null;
        String walkDistance = null;
        String descrip = null;
        String contact = "defult";

        Accom accom = null;
        //repeat this while there is data in the results.
        while(results.next()){
            //resets accom item to new.
            accom = new Accom();

            reference = results.getInt("REFERENCE_NUM");
            type = results.getString("ACCOM_TYPE");
            walkDistance = results.getString("WALKING_DISTANCE");
            descrip = results.getString("DESCRIPTION");
            contact = results.getString("CONTACT");

            //sets the accom item with found details
            accom.setContact(contact);
            accom.setDescrip(descrip);
            accom.setReference(reference);
            accom.setType(type);
            accom.setWalkDistance(walkDistance);



            //adds accom item to list of accoms
            accoms.add(accom);
        }
        //bit of clean up on databse connection and results.
        results.close();
        results = null;
        statement.close();
        statement = null;
        connection.close();
        connection = null;

    }catch(SQLException e){
        e.printStackTrace();
    }
    finally{
        cleanUp();
    }

    //returns what ever is found
    return accoms;

1 个答案:

答案 0 :(得分:1)

我不太确定,但你的SQL语句可能有空格。看一下声明:

  ResultSet results = statement.executeQuery("SELECT * FROM ACCOM" + whereClaws);

想象一下,在ACCOM之后你添加where条件就像你的例子那样可能就像

  SELECT * FROM ACCOM WHERE REFERENCE_NUM LIKE '1%'

正如我所说,已经不太确定这是原因,但你可以给出一个像

这样的傻瓜
  ResultSet results = statement.executeQuery("SELECT * FROM ACCOM " + whereClaws);
祝你好运