| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 880 人关注过本帖
标题:登陆表转换javaben求解
只看楼主 加入收藏
chenzhanpeng
Rank: 1
等 级:新手上路
帖 子:271
专家分:0
注 册:2007-1-5
收藏
 问题点数:0 回复次数:2 
登陆表转换javaben求解
我有一个数据库表userinfo,有字段ID为INT型,username,password为字符型,用DW画了一个登陆界面用户名和密码名字分别为username,password,现在要要将那个数据库的表转换成JAVABEAN,我要实现登陆,用户名对了才能登陆,怎么实现,请高手帮忙
搜索更多相关主题的帖子: javaben 求解 登陆 
2008-09-07 20:03
Ethip
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:771
专家分:0
注 册:2008-1-18
收藏
得分:0 
先写连接数据库的代码
2008-09-08 10:05
liuguangzong
Rank: 1
等 级:新手上路
威 望:2
帖 子:46
专家分:0
注 册:2008-9-1
收藏
得分:0 
搞简单一点就:先写数据库连接类,再写实体类JavaBean,再写一个Servlet,就ok!

1.数据库连接类:Dbbase类

public class DBbase {
    
    private static Connection con =null;
    private static ResultSet rs=null;
    
    private static Connection getCon(){
        
        String classStr="com.microsoft.sqlserver.jdbc.SQLServerDriver";

        String conStr="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=newsdb";

        try {
            Class.forName(classStr);
            con=DriverManager.getConnection(conStr,"sa","sasa");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    
    public static CachedRowSet getRs(userBean ubean){

        String sql = "select * from userinfo where username='%s' and userpwd='%s'";
        sql = String.format(sql, ubean.getUname(), ubean.getUpwd());
        try {
            if(con==null || con.isClosed()){
                con=getCon();
            }
        } catch (SQLException e1) {
    
            e1.printStackTrace();
        }
        
        CachedRowSet cr=null;
        try {
            cr = new CachedRowSetImpl();
        } catch (SQLException e) {
    
            e.printStackTrace();
        }
        
        try {
            PreparedStatement pstmt =con.prepareStatement(sql);
            rs=pstmt.executeQuery();
            
            cr.populate(rs);
            
//            rs.close();
//            pstmt.close();
//            con.close();
            
        } catch (Exception e) {

            e.printStackTrace();
        }
        
        
        return cr;
    }
}

2.实体类:
public class userBean {
    private String uname;
    private String upwd;
    
    public userBean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public userBean(String uname, String upwd) {
        super();
        this.uname = uname;
        this.upwd = upwd;
    }

    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}

3.写servlet:

        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");    
                   userBean  ubean=new userBean();
                   String bname=ubean.setUname(uname);
                   String bpwd=ubean.setUname(upwd);

             DBbase dao = new DBbase ();
        boolean f = dao.Checklogin(bname, bpwd);


            if (f) {
                request.getSession().setAttribute("name", uname);
                request.getRequestDispatcher("back.jsp").forward(request,
                        response);
            } else {
                request.getSession().setAttribute("err", "登陆失败,用户名或密码错误");
                response.sendRedirect("err.jsp");
            }
2008-09-08 10:53
快速回复:登陆表转换javaben求解
数据加载中...
 
   



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

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