创建User类
package com.bean;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = 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 + '\'' +
'}';
}
}
构造Mysql查询方法且使用到登陆
package com.dao;
import com.bean.User;
import com.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public List<User> findAll(){
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet = null;
List<User>userList=new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select * from tb_user";
statement =connection.prepareStatement(sql);
resultSet = statement.executeQuery();
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);
// System.out.println(resultSet.getInt(1));
// System.out.println(resultSet.getString(2));
// System.out.println(resultSet.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(resultSet,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return userList;
}
public String findpassword(String username){
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet = null;
String getpassword="";
try {
connection = DBUtil.getConnection();
String sql = "select password from tb_user where username=?";
statement =connection.prepareStatement(sql);
statement.setString(1,username);
resultSet = statement.executeQuery();
while(resultSet.next()){
// System.out.println(resultSet.getString(1));
getpassword = resultSet.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(resultSet,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return getpassword;
}
public void Delete(int id){
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet = null;
List<User>userList=new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "DELETE from tb_user where id=?";
statement =connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(resultSet,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void Add(User user){
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet = null;
List<User>userList=new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "insert into tb_user(?,?) values('qmy','123')";
statement =connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(resultSet,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<User> Update(User user){
Connection connection = null;
PreparedStatement statement =null;
ResultSet resultSet = null;
List<User>userList=new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "update tb_user set username=? where id=?";
statement =connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setInt(2,user.getId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
DBUtil.closeAll(resultSet,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
return userList;
}
}
实现上面写的方法且在数据库中判定
package com.servlet;
import com.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");
// System.out.println(userDao.findpassword(username));
if (password.equals(userDao.findpassword(username))){
resp.getWriter().write("success");
}else{
resp.getWriter().write("failure");
}
}
}
连入到数据库和关闭数据库
由于这段代码是基本都有的所以写一块方便调用!!!
package com.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/wzsxy","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();
}
}
}