package com.ljx.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 + '\'' +
'}';
}
}
package com.ljx.DAO;
import com.ljx.bean.User;
import com.ljx.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 {
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
int iflogin=0;
public List<User> findAll(){
List<User> userList=new ArrayList<>();
try{
connection= DButil.getConnection();
String sql="select * from 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));
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
try{
DButil.closeAll(resultSet,statement,connection);
}
catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return userList;
}
public int login(String username,String password){
try{
connection= DButil.getConnection();
String sql="SELECT id,username,PASSWORD FROM 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 insert(User user){
try{
connection= DButil.getConnection();
String sql="insert into User (username,password) value (?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,"123");
statement.setString(2,"123");
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){
try{
connection= DButil.getConnection();
String sql="update User set username=?,password=? where id=1";
statement=connection.prepareStatement(sql);
statement.setString(1,"123");
statement.setString(2,"123");
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 delete(int i){
try{
connection= DButil.getConnection();
String sql="delete from User where id=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,i);
statement.executeUpdate();
} catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
try{
DButil.closeAll(null,statement,connection);
}
catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package com.ljx.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.getWriter().write("hello");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
package com.ljx.servlet;
import com.ljx.DAO.UserDAO;
import com.ljx.bean.User;
import javax.servlet.*;
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");
}
}
}
package com.ljx.util;
import java.sql.*;
public class DButil {
private static Connection connection;
public static Connection getConnection() throws ClassCastException, SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
System.out.println("数据库连接成功");
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();
}
}
}
package com.ljx.util;
import com.ljx.DAO.UserDAO;
import com.ljx.bean.User;
public class Test {
public static void main(String[] args) {
UserDAO userDAO=new UserDAO();
String a="123";
System.out.println(userDAO.login(a,a));
}
}
<?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>HelloServlet</servlet-name>
<servlet-class>com.ljx.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.ljx.servlet.Loginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>