工具:Idea,mysql,Jdk9
1、数据库表 _ user
CREATE TABLE user(
id INT,
username VARCHAR(10),
password VARCHAR(10)
);
ALTER TABLE user MODIFY id INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE user MODIFY username VARCHAR(10) NOT NULL;
ALTER TABLE user MODIFY password VARCHAR(10) NOT NULL;
2、JDBC工具类 _ JDBCUtils
package com.mollen.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* @ClassName: JDBCUtils
* @Auther: Mollen
* @CreateTime: 2018-09-23 10:18:14
* @Description: JDBCUtils 数据库连接工具类
*/
public class JDBCUtils {
/**
* 1.定义成员变量datasource
*/
private static DataSource ds;
/**
* 2.加载数据源
*/
static {
try {
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("Druid.properties"));
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 3.获取连接池对象
* @return
*/
public static DataSource getDatasource(){
return ds;
}
/**
* 4.获取连接池Connection对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 5.释放资源
* @param st
* @param conn
*/
public static void close(PreparedStatement st, Connection conn) {
close(null, st, conn);
}
/**
* 5.释放资源重载方法
* @param rs
* @param st
* @param conn
*/
public static void close(ResultSet rs, PreparedStatement st, Connection conn) {
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、实体类 _ User
package com.mollen.domain;
/**
* @ClassName: User
* @Auther: Mollen
* @CreateTime: 2018-09-23 10:12:41
* @Description:
* User实体类
*/
public class User {
private String userName;
private String password;
public User() {
}
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
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;
}
}
4、用户操作类 _ UserDao
package com.mollen.dao;
import com.mollen.domain.User;
import com.mollen.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* @ClassName: UserDao
* @Auther: Mollen
* @CreateTime: 2018-09-23 10:17:31
* @Description: UserDao类操作user数据
*/
public class UserDao {
private JdbcTemplate jtl = new JdbcTemplate(JDBCUtils.getDatasource());
public User login(User loginUser) {
try {
String sql = "SELECT * FROM user WHERE username=? and password = ?";
User user = jtl.queryForObject(
sql, new BeanPropertyRowMapper(User.class),
loginUser.getUserName(), loginUser.getPassword());
return user;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
5、Servlet类
1.登陆Serevlet _ LoginServlet
package com.mollen.web;
import com.mollen.dao.UserDao;
import com.mollen.domain.User;
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 java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User loginUser = new User(username,password);
UserDao userDao = new UserDao();
User user = userDao.login(loginUser);
if(user==null){
request.getRequestDispatcher("/failServlet").forward(request,response);
}else{
request.setAttribute("user",user);
request.getRequestDispatcher("/successServlet").forward(request,response);
}
}
}
2.失败跳转Servlet类 _ FailServlet
package com.mollen.web;
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 java.io.IOException;
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登陆失败! 用户名或者密码错误!");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
3.成功跳转 _ ServletSuccessServlet
package com.mollen.web;
import com.mollen.domain.User;
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 java.io.IOException;
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = (User) request.getAttribute("user");
if (user != null) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登陆成功!" + user.getUserName() + ",欢迎你!");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
原文:https://blog.csdn.net/mollen/article/details/82822801