用IDEA开发工具和MySql实现登录和增删改查的功能。
功能分析:
1.登录
2.增加
3.删除
4.修改
5.查询
效果演示
登陆页面
信息显示页面
一 :数据库设计
MySql数据库名为 school
登录表名为 login
信息表名为 student
登录表插入合适的数据
信息表自己插入
二 : 代码展示
目录结构
注意:我们需要引入相关架包
完整代码
信息类
Student
package com.zsh.bean;
public class Student {
private int id;
private String name;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String sex, String address) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.address = address;
}
}
增删改查登录
AddServlet
package com.zsh.servlet;
import com.zsh.util.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet(name = "AddServlet")
public class AddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
String sql = "insert into student(name,sex,address) values(?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,sex);
ps.setString(3,address);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeConn(conn,ps,null);
}
response.sendRedirect("select");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
DeleteServlet
package com.zsh.servlet;
import com.zsh.util.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;