Black_key_law 2015-09-26 10:53 采纳率: 100%
浏览 1862
已采纳

初学jsp遇到数据库连接处理问题

这个是注册插入数据代码,在网页中可以运行成功,说明我的数据库连接是没问题的

 <%String uid=request.getParameter("uid");
  String pwd=request.getParameter("pwd");
  Object[]paramss={uid,pwd};
  String sqll="insert into tb_vip values(?,?)";

  if(DBHelper.runupdatesql(sqll, paramss))
  { 
  out.print("已经成功注册");
  }
  else
  out.print("服务器问题请与客服联系");

   %>

下面这段是判断数据库中用户名是否存在,不存在才可以用上面代码注册,但是运行后不管数据库中是否存在用户名,网页中只打印“注册失败,用户名不存在”

 <%
  String uid=request.getParameter("uid");
  String pwd=request.getParameter("pwd");
  Object[] params={uid};
  String sql ="select count(*)from tb_vip where userid=?";
  Result result=DBHelper.runselectsql(sql, params);
  if(result.getRowCount()==0){
  Object[]paramss={uid,pwd};
  String sqll="insert into tb_vip values(?,?)";
  if(DBHelper.runupdatesql(sqll, paramss)){ 
  out.print("已经成功注册");
  }else{out.print("服务器问题请与客服联系");}}
 else{
  out.print("注册失败,用户名已存在");
  }
   %>

下面是java数据库处理代码部分

package jspex;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

public class DBHelper {

    private static final String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String url = "jdbc:sqlserver://localhost:1433;databasename=studentm";
    private static final String uname = "sa";
    private static final String upass = "c111111";

    public static Connection getConn(){
        Connection conn = null;
        try{
            Class.forName(className);
            conn = DriverManager.getConnection(url,uname, upass);
        } catch(Exception e){
            e.printStackTrace();
        }

        return conn;
    }

    public static void closeConn(Connection conn){
        try{
            if(conn!=null){
                conn.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }   
    public static void closePstmt(PreparedStatement pstmt){
        try{
            if(pstmt!=null){
                pstmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }   
    public static void closeRs(ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    public static boolean runupdatesql(String sql,Object[]params){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con=getConn();
            ps=con.prepareStatement(sql);
            for(int i=0;i<params.length;i++){
                ps.setObject(i+1, params[i]);
            }
            ps.executeUpdate();
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    finally{
        try{
            ps.close();
            con.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    }
    public static Result runselectsql(String sql,Object[]params){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet res=null;
        Result result=null;
        try{
            con=getConn();
            ps=con.prepareStatement(sql);
            for(int i=0;i<params.length;i++){
                ps.setObject(i+1, params[i]);
            }
            res=ps.executeQuery();
            result=ResultSupport.toResult(res);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                res.close();
                ps.close();
                con.close();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
        return result;
    }
}


下面是注册页面,我为了好找错误,还把相关js功能注释掉了

 <html>
  <head>
    <title>reg</title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <style type="text/css">
 #loginform{
 width:300px;
 height:auto;
 }
 label{
 width:60px;
 height:30px;
 text-align:left;
 font-size:14px;
 font-weight:hold;
 vertical-align:bottom;
 }
 .inpute{
 width:160px;
 height:20px;
 }
 .row{
 width:300px;
 height:30px;
 }

 </style>
 <script type="text/javascript">
 function checkform(){
 var name=document.getElementById("uid");
 var pass=document.getElementById("pwd");
 /*var rpass=document.getElementById("rpwd");*/
 if(name.value.length<=0){
 alert("用户名不能为空");
 name.focus();
 return false;
 }

 if(pass.value.length>=8){
 alert("密码长度不对");
 pass.focus();
 return false;
 }

 /**if(pass!=rpass){
 alert("密码不一致");
 return false;
 }**/
 return true;
 }
 </script>
  </head>

  <body>
  <div id="loginform">
  <form  onsubmit="return checkform();"action="doreg.jsp" method="post">
  <div class="row">
  <label for="uid">用户名: </label>
  <input name="uid" type="text" class="inpute" id="uid">
  </div>
  <div class="row">
  <label for="pwd">密码:  </label>
  <input name="pwd" type="password" class="inpute" id="pwd">
  </div>
   </div>
  <!--  <div class="row">
  <label for="rpwd">确认密码:</label>
  <input name="rpwd" type="password" class="inpute" id="rpwd">
  </div> --> 
  <div class="row">
  <input type="submit" value="注册"/><input type="reset" value="重新填写"/>
  </div>
  </form>

  </div>
  </body>
</html>

分析了很久,不知道出现了什么问题,希望大家可以帮帮我

  • 写回答

5条回答

  • Black_key_law 2015-09-28 07:24
    关注

    把判断方法关于result部分改写成如下部分就可以运行了,谢谢大家了:

     public static int runselectsql(String uid){
            Connection con=null;
            PreparedStatement ps=null;
            ResultSet res=null;
            try{
                con=getConn();
                String sql ="select  count(*)  from tb_vip where userid=?";
                ps=con.prepareStatement(sql);
                ps.setString(1,uid);
                res=ps.executeQuery();
                while(res.next())
                {
    
                 x=res.getInt(1);
                }
    
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                try{
                    res.close();
                    ps.close();
                    con.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
    
            }
    
            return x;
        }
    

    jsp代码改写成这样

     <%
      String uid=request.getParameter("uid");
      String pwd=request.getParameter("pwd");
      if(DBHelper.runselectsql(uid)==0){
        Object[]params={uid,pwd};
        String sql="insert into tb_vip values(?,?)";
        if(DBHelper.runupdatesql(sql, params)){ 
         out.print("已经成功注册");
       } 
       }
         else{
         out.print("注册失败,用户名已存在");
      }
    %>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?