<%
request.setCharacterEncoding("gbk");
String telnum = (String)request.getParameter("numcheck");if(telnum == null && telnum.trim() == "") {
response.sendRedirect("checkok.jsp");
out.println("请输入号码");
} else {
Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://localhost:3306/clientmanage?user=root&password=absolute";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from clientlist where telnum = " + telnum);
}
%>判断传过来的号码是否为空,如果为空就重定向,如果不为空就连接数据库。

解决方案 »

  1.   

    if(null == telnum.trim() || telnum.trim()=="")  //试试这个
      

  2.   


    还是报错,不知道咋回事
    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
    <%@ page import="java.sql.*"  %><%
    request.setCharacterEncoding("gbk");
    String telnum = (String)request.getParameter("numcheck");Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    if(telnum == null && telnum.trim() == "") {
    response.sendRedirect("checkok.jsp");
    out.println("请输入号码");
    } else {
    Class.forName("com.mysql.jdbc.Driver");
    String url= "jdbc:mysql://localhost:3306/clientmanage?user=root&password=absolute";
    conn = DriverManager.getConnection(url);
    stmt = conn.createStatement();
    rs = stmt.executeQuery("select * from clientlist where telnum = " + telnum);
    }
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>    
        <title>用户信息查询</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
      <form name="checkclient" action="checkok.jsp" method="post">
      <div align="center">
      手机号码:
      <input type="text" name="numcheck">
      <input type="submit" name="submit" value="查询">
      </div>
      </form>
      <br><br>
        <table width="700"align="center" border="5" bordercolor="#99CCFF" cellspacing="3" cellpadding="5">
        <caption><h1 >用户信息</h1></caption>
        
         <th bgcolor="#99CCFF">用户姓名</th>
         <th bgcolor="#99CCFF">用户车牌</th>
         <th bgcolor="#99CCFF">用户号码</th>
         <th bgcolor="#99CCFF">服务地点</th>
         <th bgcolor="#99CCFF">备注</th>
         <th bgcolor="#99CCFF">性别</th>
         <th bgcolor="#99CCFF">时间</th>
         <th bgcolor="#99CCFF">次数</th>    
        
        
        <% while(rs.next()) { %>
    <tr>
    <td><%= rs.getString("name") %></td>
    <td><%= rs.getString("carnum") %></td>
    <td><%= rs.getString("telnum") %></td>
    <td><%= rs.getString("location") %></td>
    <td><%= rs.getString("re") %></td>
    <td><%= rs.getString("sex") %></td>
    <td><%= rs.getDate("date") %></td>
    <td><%= rs.getString("count") %></td>

    </tr>    
        <% } %>
        </table><br><br>
        <div align="center">
         <a href="main.jsp" target="_parent">返回首页</a>
        </div>   <% rs.close();
          stmt.close();
          conn.close();
       %> 
      </body>
    </html>这个是我完整的代码
      

  3.   

    main页面在提交的时候就应该检查参数是否为空了,如果为空则提示不能为空
      

  4.   

    你应该使用if嵌套先判断telnum是否为空,为空则不进行telnum.trim() == ""则不操作,直接提示空,!=null再进行下个if
      

  5.   

    问题已解决,不过我是我用js 的onsubmit在提交表单前看是否为空,为空则不提交。谢谢楼上各位