精华内容
下载资源
问答
  • 在store_db数据库中创建一个t_user表;在domain包中创建一个User的类:package com.xxf.domain;public class User {private Integer id;private String username;private String password;private String email;...

    98a5e993fe79df4c2969af3317b503ac.png

    目录结构图:

    1.在store_db数据库中创建一个t_user表;

    在domain包中创建一个User的类:

    package com.xxf.domain;

    public class User {

    private Integer id;

    private String username;

    private String password;

    private String email;

    private String phone;

    @Override

    public String toString() {

    return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", phone="

    + phone + "]";

    }

    public Integer getId() {

    return id;

    }

    public void setId(Integer id) {

    this.id = id;

    }

    public String getUsername() {

    return username;

    }

    public void setUsername(String username) {

    this.username = username;

    }

    public String getPassword() {

    return password;

    }

    public void setPassword(String password) {

    this.password = password;

    }

    public String getEmail() {

    return email;

    }

    public void setEmail(String email) {

    this.email = email;

    }

    public String getPhone() {

    return phone;

    }

    public void setPhone(String phone) {

    this.phone = phone;

    }

    }

    2.使用dbutils工具类连接数据库;

    在util包创建一个JdbcUtil.javad的工具类,要导入dbutils的jar包。

    package com.xxf.util;

    import java.io.FileInputStream;

    import java.sql.Connection;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import java.util.Properties;

    import javax.sql.DataSource;

    import com.alibaba.druid.pool.DruidDataSourceFactory;

    public class JdbcUtil {

    public static DataSource ds = null;

    static {

    try {

    //1.加载配置文件

    Properties p = new Properties();

    //获取字节码目录

    String path = JdbcUtil.class.getClassLoader().getResource("db.properties").getPath();

    FileInputStream in = new FileInputStream(path);

    p.load(in);

    //ds = BasicDataSourceFactory.createDataSource(p);

    ds = DruidDataSourceFactory.createDataSource(p);

    }catch(Exception e) {

    e.printStackTrace();

    }

    }

    public static DataSource getDataSource() {

    return ds;

    }

    public static Connection getConn() {

    try {

    // 2.连接数据

    return ds.getConnection();

    } catch (Exception e) {

    e.printStackTrace();

    }

    return null;

    }

    /**

    * 关闭资源

    */

    public static void close(Connection conn,Statement st,ResultSet rs) {

    if (rs != null) {

    try {

    rs.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    if (st != null) {

    try {

    st.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    if (conn != null) {

    try {

    conn.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }

    }

    3.写一个servlet里面实现service的方法;获取请求参数username和password;并实现sql语句的查询功能,进行判断是否有正确。

    package com.xxf.servlet;

    import java.io.IOException;

    import java.sql.SQLException;

    import javax.servlet.ServletException;

    import javax.servlet.annotation.WebServlet;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.commons.dbutils.QueryRunner;

    import org.apache.commons.dbutils.handlers.BeanHandler;

    import com.xxf.domain.User;

    import com.xxf.util.JdbcUtil;

    @WebServlet("/login")

    public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    // 获取请求参数

    String username = request.getParameter("username");

    String password = request.getParameter("password");

    System.out.println("username"+username+"password"+password);

    // 连接到数据库,验证用户和密码是否正确

    QueryRunner qr = new QueryRunner(JdbcUtil.getDataSource());

    String sql="select * from t_user where username=? and password=?";

    User user=null;

    try {

    user=qr.query(sql, new BeanHandler(User.class),username,password);

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    if(user!=null){

    response.getWriter().write("login.success!");

    }else{

    response.getWriter().write("login.failed");

    }

    }

    }

    4.db.properties的配置文件在src下面创建一个Floader的resource的文件夹。

    数据库的名字、密码、用户名要更改,不要乱加空格,容易出错

    driverClassName=com.mysql.jdbc.Driver

    url=jdbc:mysql://localhost:3306/store_db?rewriteBatchedStatements=true

    username=root

    password=123456

    maxActive=8

    5.lib的下面放三个jar并且buildpath

    链接:https://pan.baidu.com/s/1QPUxvmFTtQeCp4uWDYOfTA

    提取码:35mq

    6.web.xml的内容,要是创建动态的web项目可有可无。这里面不放了

    7.html页面

    Insert title here

    登录界面

    用 户:

    密 码:

    展开全文
  • java 连接数据库实现用户登录功能 能运行成功,适合初学者借鉴学习。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,138
精华内容 455
关键字:

java用户登录连接数据库

java 订阅