servlet实现网页的登录功能
1.DBUtil.java
package com.zr.Util;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wzbc","root","123456");
return connection;
}
public static void closeAll(ResultSet resultSet,Statement statement,Connection connection) throws SQLException {
if (resultSet!=null){
resultSet.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
}
}
2.UserDao.java
package com.zr.Dao;
import com.zr.Util.DBUtil;
import com.zr.bean.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
// public static void main(String[] args) {
//
// }
ResultSet resultSet=null;
int iflogin=0;
//查询
public List<User> findAll() {
Connection connection = null;
List<User> userList=new ArrayList<>();
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection = DBUtil.getConnection();
//3.写SQL语句
String sql = "select * from tb_user";
//4.获得statement对象
statement = connection.prepareStatement(sql);
//5.执行sql得到结果
resultSet = statement.executeQuery();
//6.得到结果集
while (resultSet.next()) {
User user=new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
userList.add(user);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
//7.关闭资源
DBUtil.closeAll(resultSet, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return userList;
}
public int login(String username,String password){
Connection connection= null;
PreparedStatement statement=null;
try{
connection = DBUtil.getConnection();
String sql="select * from tb_user where username=? and password=?";
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet=statement.executeQuery();
if (resultSet.next()){
iflogin=1;
}else {
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
try{
DBUtil.closeAll(null,statement,connection);
}
catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return iflogin;
}
//删除
public void deleteById(int id){
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DBUtil.getConnection();
System.out.println("创建连接成功");
//3.写sql语句
String sql="delete from tb_user where id=?";
statement.setInt(1,id);
// String sql="delete from tb_user where id= "+id;
//4.获得statement对象
statement=connection.prepareStatement(sql);
//5.执行sql 得到结果集
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void add(User user){
Connection connection= null;
PreparedStatement statement= null;
try {
connection = DBUtil.getConnection();
System.out.println("创建连接成功");
//写sql语句
String sql="insert into tb_user(username,password) values(?,?)";
//4.获得statement对象
statement = connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void update(User user){
Connection connection= null;
PreparedStatement statement=null;
try {
connection = DBUtil.getConnection();
System.out.println("创建连接成功");
//3.写sql语句
String sql="UPDATE tb_user SET username=?,PASSWORD=? WHERE id=?";
//4.获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.setInt(3,user.getId());
//5.执行sql 得到结果集
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
3.LoginServlet.java
package com.zr.servlet;
import com.zr.Dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
UserDao userDao=new UserDao();
String username=req.getParameter("username");
String password=req.getParameter("password");
int i =userDao.login(username,password);
if (i==1){
resp.getWriter().write("success");
}else {
resp.getWriter().write("failure");
}
}
}
- User.java
package com.zr.bean;
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}