利用dbutils实现登录实例

利用dbutils实现登录实例

  • 思路:html网页—-登录(用户名,密码)—–servlet中getparamer方法得到用户输入的用户名和密码——-用户名和密码在dbutils中验证是否正确,

  • 添加三个包:c3p0-0.9.1.2.jar(c3p0的包) commons-dbutils-1.4.jar(dbutils包)
    mysql-connector-java-5.0.8-bin.jar(MySQL驱动的包),不需要添加配置。

  • web.xml中配置servlet,后面讲。

  • 代码

    1. html的代码:若只要登录效果,,界面越简单越好,注意粗体加黑的地方

      • form class=”form-horizontal” action=”http://localhost:8081/03.1Login/servletLogin* method=”post”*

      • input type=”text” class=”form-control” id=”username” placeholder=”请输入用户名” name=”username”

      • input type=”password” class=”form-control” id=”inputPassword3” placeholder=”请输入密码” name=”password”

      • action表示要提交的地方,这里是web.xml配置的servlet的地方,因为网站输入网址的访问有两种方式,1.localhost:8081/03.1Login/webcontent目录下的html文件 2.localhost:8081/03.1Login/web.xml中配置的servlet名称

    2.servlet代码:建立servlet包–Servlet类

package servlet;

``import java.io.IOException;
``import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.Users;
import Service.Service;

public class Servlet extends HttpServlet {
    //设置编码
    //得到用户名和密码
    //调用userservice中的login()方法,返回user对象
    //判断user是否为空。
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String username=req.getParameter("username");//从html文件中获取输入的用户名
        String password=req.getParameter("password");
        resp.setContentType("text/html;charset=utf-8");//设置读取的格式
        Service sv=new Service();
        Users user = null;
        try {
            user = sv.login(username, password);//在service类中的login方法判断用户名或者密码是否正确
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            System.out.println("网络异常!");
        }
        if(user==null){
            //3.1若为空 写"用户名和密码不匹配"
            resp.getWriter().print("用户名和密码不匹配,3秒之后跳转");
            //案例2-定时跳转
            resp.setHeader("refresh","3;url=/03.1Login/login.htm");
        }else{
            //3.2若不为空 写"xxx:欢迎回来"
            resp.getWriter().print(user.getUsername()+":欢迎回来");
        }
    }

}
  1. 导入dbutils文件和c3p0.properties

    public class JavaUtils {
    public static ComboPooledDataSource ds=new ComboPooledDataSource();
    
    //连接池
    public static DataSource getPool(){
        return ds;
    }
    //获取连接
    public static Connection name() throws SQLException {
        return ds.getConnection();
    }
    
    //释放资源
    public static void closeResource(Connection conn, Statement st, ResultSet rs) {
        closeResultSet(rs);
        closeStatement(st);
        closeConn(conn);
    }
    
    /**
     * 释放连接
     * 
     * @param conn
     *连接
     */
    public static void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    
    }
    
    /**
     * 释放语句执行者
     * 
     * @param st
     *语句执行者
     */
    public static void closeStatement(Statement st) {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            st = null;
        }
    
    }
    
    /**
     * 释放结果集
     * 
     * @param rs
     *结果集
     */
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
    
    }
    

4.domain包—–User类, 建立username和password的javabean;

package domain;

public class Users {
  private String username;
  private String password;
  public String getUsername() {
      System.out.println("user"+username);
      return username;
  }
  public void setUsername(String username) {
      this.username = username;
  }
  public String getPassword() {
      return password;
  }
  public void setPassword(String password) {
      this.password = password;
  }

}

5.services实现验证

 package Service;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.JavaUtils;
import domain.Users;

public class Service {
    public Users login(String username,String password) throws SQLException {
        QueryRunner qr=new QueryRunner(JavaUtils.getPool());//获取连接

        //String sql="select * from user WHERE username=? and password=?";
        //编写sql
        String sql="select * from user where username = ? and password = ?";
        //执行sql
        //将username和password传入到User.java中
        Users user = (Users) qr.query(sql, new BeanHandler<>(Users.class),username,password);
        System.out.println("1"+user);
        System.out.println("2"+user.getPassword());
        return user;
    }
}

源码下载:http://download.csdn.net/detail/u012510757/9762390

你可能感兴趣的:(j2ee)