1.Bean目录下的User类
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 + '\'' +
'}';
}
}
2.Dao目录下的JdbcUtil工具类和UserDao类
public class JdbcUtil {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
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();
}
}
}
public class UserDao {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
}
public List<User> findAll(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
List<User> userList=new ArrayList<>();
/*try语句中出现异常时,会执行catch中的语句,printStackTrace()方法的意思是:在命令行打印异常信息在程序中出错的位置及原因*/
try {
//1.加载驱动
//2.创建连接
connection= JdbcUtil.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 {
//7.关闭资源
try {
JdbcUtil.closeAll(resultSet, statement, connection);
}catch (SQLException throwables){
throwables.printStackTrace();
}
System.out.println("查询结束,退出");
}
return userList;
}
public void update(User user){
Connection connection=null;
PreparedStatement statement=null;
try {
connection= JdbcUtil.getConnection();
String sql = "update tb_user set username=?,password=? where id=? ";
statement= connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
statement.setString(2,user.getPassword());
statement.setInt(3,user.getId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
JdbcUtil.closeAll(null,statement,connection);
System.out.println("更新成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void add(User user){
Connection connection=null;
PreparedStatement statement=null;
try {
connection= JdbcUtil.getConnection();
String sql = "insert into tb_user(username,password) values(?,?)";
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 {
JdbcUtil.closeAll(null,statement,connection);
System.out.println("添加成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void deleteById(int id){
Connection connection=null;
PreparedStatement statement=null;
try{
connection= JdbcUtil.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 {
JdbcUtil.closeAll(null,statement,connection);
System.out.println("删除成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.servlet类目录下的LoginServlet类
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*接收username*/
String username=req.getParameter("username");
String password=req.getParameter("password");
UserDao userDao=new UserDao();
List<User> userlist=userDao.findAll();
System.out.println(userlist);
int count=0;
for(int i=0;i<userlist.size();i++) {
System.out.println(userlist.get(i).getUsername());
if (username.equals(userlist.get(i).getUsername()) && password.equals(userlist.get(i).getPassword())){
count++;
}
}
if(count>0){
resp.getWriter().write("success");
}else
resp.getWriter().write("failure");
}
}
4.xml配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.wzbc.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>