标题:jsp访问数据库
取消只看楼主
userWJX
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2019-2-27
 问题点数:0 回复次数:0 
jsp访问数据库
public class AccessDB {
    public int read(String name , String pwd){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count=0;
        try {
            //注册驱动、建立连接
            conn = JdbcUtils.getConnection();
            //创建语句
            String sql = "select name,pwd from userconfig where name=? and pwd=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            //执行语句
            rs = ps.executeQuery();
            while(rs.next()) {
                count++;
            }
        }catch(Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            JdbcUtils.free(rs, conn, ps);
            
        }
        return count;
    }
    public static void main(String[] args) {
        AccessDB a = new AccessDB();
        int i = a.read("wjx", "123");
        System.out.println(i);
    }
}
//此时read方法返回了1(i的值为1)
//用jsp调用却出错了
<body>
    <%
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("uname");//name="wjx"
        String pwd = request.getParameter("upwd");//pwd="123"
        //out.print(name + pwd);
        AccessDB adb = new AccessDB();
        
        int i = adb.read(name,pwd);
        out.print(i);//输出-1
        if(i>0){
            request.getRequestDispatcher("A.jsp").forward(request, response);
        }else if(i==0){
            response.sendRedirect("login.jsp");
        }else{
            out.print("系统出现异常");
        }
    %>
</body>
//此时i的值却为-1
//这是为什么?
搜索更多相关主题的帖子: jsp read String name request 
2019-02-27 15:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-493257-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014779 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved