package com. lw. bean;
public class User {
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;
}
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;
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}' ;
}
private int id;
private String username;
private String password;
}
package com. lw. dao;
import com. lw. bean. User;
import com. lw. util. DBUtil;
import java. sql. *;
import java. util. ArrayList;
import java. util. List;
public class UserDao {
public static void main ( String[ ] args) throws ClassNotFoundException, SQLException {
UserDao userDao= new UserDao ( ) ;
userDao. findAll ( ) ;
}
public List< User> findAll ( ) {
Connection connection= null;
List< User> userList= new ArrayList < > ( ) ;
PreparedStatement statement= null;
ResultSet resultSet= null;
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) ;
}
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
DBUtil. closeAll ( resultSet, null, connection) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
System. out. println ( userList) ;
return userList;
}
public void deleteById ( int id) {
Connection connection= null;
PreparedStatement statement= null;
try {
connection = DBUtil. getConnection ( ) ;
System. out. println ( "创建连接成功" ) ;
String sql= "delete from tb_user where id=?" ;
statement= connection. prepareStatement ( sql) ;
statement. setInt ( 1 , id) ;
statement. executeUpdate ( ) ;
} catch ( ClassNotFoundException | SQLException e) {
e. 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 ( "创建连接成功" ) ;
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 ( ClassNotFoundException | SQLException e) {
e. 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 ( "创建连接成功" ) ;
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 ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
DBUtil. closeAll ( null, statement, connection) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
}
public User findByUsername ( String username) {
Connection connection= null;
PreparedStatement statement= null;
ResultSet resultSet= null;
User user= new User ( ) ;
try {
connection = DBUtil. getConnection ( ) ;
System. out. println ( "创建连接成功" ) ;
String sql= "select * from tb_user where username=?" ;
statement= connection. prepareStatement ( sql) ;
statement. setString ( 1 , username) ;
resultSet = statement. executeQuery ( ) ;
if ( resultSet. next ( ) ) {
user. setId ( resultSet. getInt ( 1 ) ) ;
user. setUsername ( resultSet. getString ( 2 ) ) ;
user. setPassword ( resultSet. getString ( 3 ) ) ;
}
} catch ( ClassNotFoundException | SQLException e) {
e. printStackTrace ( ) ;
} finally {
try {
DBUtil. closeAll ( null, statement, connection) ;
} catch ( SQLException throwables) {
throwables. printStackTrace ( ) ;
}
}
return user;
}
}
package com. lw. 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. lw. servlet;
import com. lw. dao. UserDao;
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 IOException {
String username= req. getParameter ( "username" ) ;
String password= req. getParameter ( "password" ) ;
UserDao userDao= new UserDao ( ) ;
if ( password. equals ( userDao. findByUsername ( username) . getPassword ( ) ) ) {
resp. getWriter ( ) . write ( "success" ) ;
} else {
resp. getWriter ( ) . write ( "failure" ) ;
}
}
}
package com. lw. util;
import java. sql. *;
public class DBUtil {
public static Connection getConnection ( ) throws ClassNotFoundException, SQLException {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
return DriverManager. getConnection ( "jdbc:mysql://localhost:3306/wzbc" , "root" , "root" ) ;
}
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 ( ) ;
}
}
}