2014-01-27 95 views
0

我試圖在多個表上進行搜索,但用戶並沒有給出每一個輸入。我想搜索用戶輸入的所有內容,而忽略用戶是否不想搜索名稱。 我試過下面的代碼,但它不會工作,除非提供了所有的字符串。有什麼方法可以忽略這一點,並繼續使用用戶提供的信息進行搜索?Java MYSQL,忽略空白字符串的搜索

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); 
    } 
} 
+0

檢查答案,如果這就是你想要的,將問題標記爲已解決 –

回答

0

你可以做一些這樣的事:

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

則執行查詢

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); 
    } 
}