在搜索时打印消息,用户的输入与数据库值不匹配

时间:2016-05-02 21:28:11

标签: javascript java html jsp

我有这个代码,我需要在没有找到这样的国家或城市时打印

" if(!rs.next()){
           out.println("NOT FOUND <a href= \"Choises Code 1.jsp\">Try Again</a>" );"

上面的代码,在两种情况下都打印出“未找到....”的消息要么找不到国家或城市,我只需要在找不到匹配时打印消息...替代ac代码将是这样的:if(“select ....”!= varialbe)printf(未找到)我的主要代码:

<%@page import="java.sql.*" %> 
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>destcode</title>
    </head>
    <style> Stable, th, td {
    border: 1px solid black;
    border-collapse: collapse;

    }
th, td {
    padding: 5px;
    text-align: left;
}
</style>
         <%

   Class.forName("com.mysql.jdbc.Driver"); 
   String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
   Connection myConnection = DriverManager.getConnection(myDatabase);
   Statement myStatement = myConnection.createStatement();  
   String search=request.getParameter("search");
  ResultSet rs = myStatement.executeQuery("SELECT * FROM DEST WHERE COUNTRY='"+search+"'");     
  if(!rs.next()){
       out.println("NOT FOUND <a href= \"Choises Code 1.jsp\">Try Again</a>" );

  }
 while(rs.next()){ %>
   <table style="width:100%">
  <tr>
    <th>Country</th>
    <th>City</th> 
    <th>Url of Destination</th>
    <th>Category</th>
  </tr>
  <tr>
    <td> <%=rs.getString("COUNTRY") %></td>
    <td> <%=rs.getString("CITY") %> </td> 
    <td> <a href=<%=rs.getString("URL") %> > <%=rs.getString("URL") %> </a>
        <% } %>
  </td>
  </tr>
</table>




  <%  
   myStatement.close(); 
   myConnection.close(); %>
    </body>
</html>

0 个答案:

没有答案