我是菜鸟,怎样才能让程序自动检测是否越界并给出提示,有具体的实现代码吗?

解决方案 »

  1.   

    你要知道越界的异常一般都指运行时异常。
    你可以对你的程序有可能出错的地方进行try catch捕获!
      

  2.   

    String t1 = request.getParameter("T1");
    String t2 = request.getParameter("T2");
      String t3 = request.getParameter("T3");
      Class.forName("org.gjt.mm.mysql.Driver");
      String url = "jdbc:mysql://localhost/mobile";
      Connection conn = DriverManager.getConnection(url,"root","123");
      String sql = "INSERT INTO user(id,name,psw) VALUES(?,?,?)";
      PreparedStatement pstmt = conn.prepareStatement(sql);
      pstmt.setString(1,t1);
      pstmt.setString(2,t2);
      pstmt.setString(3,t3);
      
      int flag = pstmt.executeUpdate();
      if (flag != 0){
          response.sendRedirect("lookup.jsp");
      }else{System.out.println("执行失败");}
    我要检查t1是否越界,try catch应该怎么写啊?谢谢啦,我自己写的不对啊
      

  3.   

    public int getRecordCount(String sql) { Connection localConnection = DataSource.getConnection();
    Statement localstmt = null;
    ResultSet rs = null;

    int count = 0;
    try {
    localstmt = localConnection.createStatement();
    rs = localstmt.executeQuery(sql);

    while(rs.next()) {
    count = rs.getInt(1);
    }
    } catch (SQLException e) {
    // 捕获异常
    e.printStackTrace();
    } finally {
    DataSource.close(rs);
    DataSource.close(localstmt);
    DataSource.close(localConnection);
    }
    return count;
    }
      

  4.   

    我写进去tomcat服务器报错了:org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 32 in the jsp file: /operator.jsp
    Illegal modifier for the variable getRecordCount; only final is permitted
    29:   if (flag != 0){
    30:       response.sendRedirect("lookup.jsp");
    31:   }else{System.out.println("执行失败");}
    32:   public int getRecordCount(String sql) {
    33: 
    34:         Connection localConnection = DataSource.getConnection();
    35:         Statement localstmt = null;
    An error occurred at line: 32 in the jsp file: /operator.jsp
    Syntax error on token "(", ; expected
    29:   if (flag != 0){
    30:       response.sendRedirect("lookup.jsp");
    31:   }else{System.out.println("执行失败");}
    32:   public int getRecordCount(String sql) {
    33: 
    34:         Connection localConnection = DataSource.getConnection();
    35:         Statement localstmt = null;
    后面还有一大堆啊
      

  5.   

    楼主“sendRedirect”后没有return。
    另外,你的主键"id"是数值还是字符串?要插入字符串?
      

  6.   

    An error occurred at line: 32 in the jsp file: /operator.jsp
    Illegal modifier for the variable getRecordCount; only final is permitted
    重点关注这句:报错
    第32行,你把你的这个方法的代码贴出来吧
    另外,使用try   catch去捕获异常,并在catche模块e.printStackTrace();
    例如:
    try{
    //你的SQL相关处理
    }catch(Exception e){
       e.printStackTrace();
       //你的异常处理,比如提示信息给页面展示
       
    }