mysql/servlet用户名密码判断是否存在数据库

来源:互联网 发布:淘宝号怎么申请注册 编辑:程序博客网 时间:2024/06/04 17:44

数据库连接类:

package com.xasmall.test;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;/** * 数据库连接类 * @author 26248 * */public class DBUTtil {    public static Connection jdbaload() {        Properties prop=new Properties();        Connection conn=null;        try {            prop.load(DBUTtil.class.getClassLoader().getResourceAsStream("config.properties"));        } catch (IOException e1) {            System.out.println("文件为读取成功!");        }        String url=prop.getProperty("url");        String driver=prop.getProperty("driver");        String user=prop.getProperty("user");        String password=prop.getProperty("password");        try {             Class.forName(driver);             conn=DriverManager.getConnection(url, user, password);        } catch (ClassNotFoundException e) {            System.out.println("未加载mysql驱动!");        } catch (SQLException e) {            System.out.println("未连接mysql!");        }        if(conn!=null) {            return conn;        }        else {            throw new Error("数据库连接错误!");        }    }}

用户属性类:

package com.xasmall.test;/** * 用户属性类 * @author 26248 * */public class User {    private int id;    private String name;    private String password;    /**     * @return the id     */    public int getId() {        return id;    }    /**     * @param id the id to set     */    public void setId(int id) {        this.id = id;    }    /**     * @return the name     */    public String getName() {        return name;    }    /**     * @param name the name to set     */    public void setName(String name) {        this.name = name;    }    /**     * @return the password     */    public String getPassword() {        return password;    }    /**     * @param password the password to set     */    public void setPassword(String password) {        this.password = password;    }}

抽象接口类(方法):

package com.xasmall.test;/** * @author 26248 * */public interface UserDAO {    //创建一个login方法    public User login(String name,String password);}

实现接口类:

package com.xasmall.test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/**实现UserDAO接口 * @author 26248 * */public class UserDAOImpl implements UserDAO{    @Override    public User login(String name, String password) {        User user=new User();        Connection co=DBUTtil.jdbaload();        String sql="select id,username,password from student where username=? and password=?";        try {            PreparedStatement pstm=co.prepareStatement(sql);            pstm.setString(1, name);            pstm.setString(2, password);            ResultSet rs=pstm.executeQuery();            if(rs.next()) {                user.setId(rs.getInt(1));                user.setName(name);                user.setPassword(password);                return user;            }            else                return null;        } catch (SQLException e) {            System.out.println("sql语句错误!");        }        return null;    }}

表单(jsp):

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><p>欢迎来到登陆页面<p><br><form action="MyLogin" method="post">Username:<input type="text" name="username"><br>Password:<input type="password" name="password"><br><input type="submit" value="提交"></form></body></html>

数据库属性文件(properties):
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/?
user=root
password=?

判断操作类:

package com.xasmall.test;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(urlPatterns= {"/MyLogin"})public class MyLogin extends HttpServlet {    private static final long serialVersionUID = 1L;    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        UserDAO user=new UserDAOImpl();        User userLogin=user.login(request.getParameter("username"), request.getParameter("password"));        if(userLogin==null) {            System.out.println("数据库中不存在用户名密码!");        }        else {            System.out.println("username:"+userLogin.getName()+"password"+userLogin.getPassword());        }    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        doGet(request, response);    }}