Java MYSQL,在忽略空字符串的同时进行搜索

时间:2014-01-27 02:12:15

标签: java mysql search jdbc boolean

我正在尝试搜索多个表,但用户没有提供每一个输入。我想搜索用户输入的所有内容,而忽略用户是否不想搜索名称。 我尝试了以下代码,但除非提供了所有字符串,否则它将无效。有没有办法忽略它并继续搜索用户提供的信息?

    public void BuscarCita(String aiDee, String namey, String lastNamey,String date, String horay, String miny) {
    try{
    String query=("SELECT citas.FECHA,citas.HORA,citas.MIN,citas.NOMBRE,citas.APELLIDO,citas.ID,citas.MOTIVO,user.TELEFONO,user.COMENTARIO  "
                + "from dentista.citas, dentista.user  WHERE citas.FECHA='"+date+"'"+"AND user.ID='" + aiDee+"'"+"AND citas.NOMBRE='" + namey+"'"+"AND citas.APELLIDO='" + 
                lastNamey+"'"+"AND citas.HORA='" + horay+"'"+"AND citas.MIN='" + miny+"'"+"AND citas.ID='" + aiDee+"'");
    Statement st=conn.createStatement();
    ResultSet rs=st.executeQuery(query);
    Object[] rows=null;
    while(rs.next()){
        String nomme=rs.getString("NOMBRE");
        String lnomme=rs.getString("APELLIDO");
        String datey=rs.getString("FECHA");
        String timeyH=rs.getString("HORA");
        String timeyM=rs.getString("MIN");
        String ID=rs.getString("ID");
        String reason=rs.getString("MOTIVO");
        String tel=rs.getString("TELEFONO");
        String comment=rs.getString("COMENTARIO");      
                rows=new Object[]{datey,timeyH+":"+timeyM,nomme +" "+ lnomme, tel, reason,comment};
                MainWindow.mainTableModel.addRow(rows);
    }



    }catch(Exception ex){
        System.out.println(ex);
    }
}

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情:

String query =" Select * from table where ";
if (!name.isEmpty()){
query+= "where name='"+name+"'";}
if (!lastName.isEmpty()){
query+= "AND lastName='"+lastName+"'";
}
...

然后执行查询

答案 1 :(得分:0)

您可以使用我的代码重试。

public String trim(String input) {
    if(input == null || input.trim().length()<1) {
        return "%";
    } else {
        return input.trim();
    }
}

public void BuscarCita(String aiDee, String namey, String lastNamey,
        String date, String horay, String miny) {
    try {
        String query = "SELECT citas.FECHA,citas.HORA,citas.MIN,citas.NOMBRE,citas.APELLIDO,citas.ID,citas.MOTIVO,user.TELEFONO,user.COMENTARIO "
                + "FROM dentista.citas, dentista.user "
                + "WHERE citas.FECHA= ? AND user.ID= ? AND citas.NOMBRE= ? AND citas.APELLIDO= ? AND citas.HORA= ? AND citas.MIN= ? AND citas.ID=?";
        PreparedStatement st = conn.prepareStatement(query);
        st.setString(1, trim(date));
        st.setString(2, trim(aiDee));
        st.setString(3, trim(namey));
        st.setString(4, trim(lastNamey));
        st.setString(5, trim(horay));
        st.setString(6, trim(miny));
        st.setString(7, trim(aiDee));

        ResultSet rs = st.executeQuery(query);
        Object[] rows = null;
        while (rs.next()) {
            String nomme = rs.getString("NOMBRE");
            String lnomme = rs.getString("APELLIDO");
            String datey = rs.getString("FECHA");
            String timeyH = rs.getString("HORA");
            String timeyM = rs.getString("MIN");
            String ID = rs.getString("ID");
            String reason = rs.getString("MOTIVO");
            String tel = rs.getString("TELEFONO");
            String comment = rs.getString("COMENTARIO");
            rows = new Object[] { datey, timeyH + ":" + timeyM,
                    nomme + " " + lnomme, tel, reason, comment };
            MainWindow.mainTableModel.addRow(rows);
        }

    } catch (Exception ex) {
        System.out.println(ex);
    }
}