中软国际实训第三天
servlet
bean
package nhh.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 + '\'' +
'}';
}
}
dao
用户写方法
package nhh.dao;
import nhh.bean.User;
import nhh.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 findAll() {
Connection connection = null;
List userList = new ArrayList<>();
PreparedStatement statement = null;
ResultSet resultSet = null;
//3.写sql语句
//4.获得statement对象
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 e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
DBUtil.closeAll(resultSet, statement, connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return userList;
}
public void deleteById(int id){
Connection connection = null;
PreparedStatement statement=null;
try {
connection =DBUtil.getConnection();
String sql="delete from tb_user where id=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
util
链接数据库
package nhh.util;
import java.sql.*;
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/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){
resultSet.close();
}
if(connection!=null){
resultSet.close();
}
}
}
登录
package nhh;
import nhh.bean.User;
import nhh.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;
import java.util.ArrayList;
import java.util.List;
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");
List<User> userList=new ArrayList<>();
System.out.println(username);
userList=userDao.findAll();
int r=0;
for(User user:userList){
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
resp.getWriter().write("yes");
r=1;
}
}
if(r==0){
resp.getWriter().write("no");
}
}
}
总结:很充实很快乐,老师讲解的很仔细!