目录
笔记
jdbc:
1.加载驱动
2.创建连接
3.写sq|
4.得到statement对象
5.执行sq|得到结果集
6.处理结果集
7.关闭资源
分层:
1.bean实体层实体类==》属性,get set,toString ,构造方法
2.dao 持久层==》数据库的增删改查
3.service 业务层
4.controller 控制层
servlet:
servlet 生命周期==》初始化 实例化 服务 销毁
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 + '\'' +
'}';
}
}
dao包下UserDao类
public class UserDao {
public User findUserByUserNameAndPassword(String username,String password){
Connection connection = null;
PreparedStatement statement=null;
ResultSet resultSet=null;
User user=null;
try {
connection = DBUtil.getConnection();
String sql = "select * from tb_user where username=? and password=?";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
resultSet = statement.executeQuery();
while(resultSet.next()){
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 user;
}
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 e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void Insert(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 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){
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 e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
service包下UserServiceImpl类和UserService接口
public class UserServiceImpl implements UserService {
UserDao userDao=new UserDao();
@Override
public boolean login(String username, String password) {
User user=userDao.findUserByUserNameAndPassword(username,password);
if (user==null){
return false;
}else {
return true;
}
}
}
public interface UserService {
boolean login(String username,String password);
}
servlet包下HelloServlet类和LoginServlet类
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() {
}
}
public class LoginServlet extends HttpServlet {
UserService userService =new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
if (userService.login(username,password)){
resp.getWriter().write("success");
}else{
resp.getWriter().write("failure");
}
}
}
util包下DBUtil类
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
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();
}
}
}
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>HelloServlet</servlet-name>
<servlet-class>com.xsj.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.xsj.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>