连接数据库类
public class DbHelper {
private static final String URL="jdbc:mysql://localhost:3306/数据库名";
private static final String USERNAME="root";
private static final String PASSWORD="密码";
private static final String DRIVERNAME="com.mysql.jdbc.Driver";
static{
try {
////加载驱动
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
//关闭资源
public void close(Connection conn, Statement st, ResultSet rs){
if (conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (st!=null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public void closes(Connection con ,Statement st){
close(con,st,null);
}
//测试
public static void main(String[] args){
DbHelper db=new DbHelper();
Connection connection = db.getConnection();
System.out.println(connection);
}
}
entity实体类
public class Dept {
private int id;
private String name;
@Override
public String toString() {
return "Dept{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
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 class Emp {
private int id;
private String name;
private String password;
private String birthday;
private String sex;
private int did;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", birthday='" + birthday + '\'' +
", sex='" + sex + '\'' +
", did=" + did +
", dept=" + dept +
'}';
}
}
JSP页面
<body>
//${pageContext.request.contextPath}绝对路径
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
<table>
<tr>
<td>
账号:<input type="text" name="name">
</td>
</tr>
<tr>
<td>
密码:<input type="text" name="password">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登录">
</td>
</tr>
</table>
</form>
</body>
LoginServlet类
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("UTF-8");
//获取jsp页面输入的值
String name1=request.getParameter("name");
String password1=request.getParameter("password");
//调用 业务层 EmpService -- 完成登录
EmpService empService=new EmpServiceImpl();
//调用登录方法 如果想判断也可以判断账号密码是否正确
//如果想存用户到 session ,一般返回 用户对象
Emp emp=empService.login(name1,password1);
//存入session
request.getSession().setAttribute("emp",emp);
request.getRequestDispatcher("/EmpServlet?method=list").forward(request,response);
}
}
EmpServlet类
@WebServlet("/EmpServlet")
public class EmpServlet extends HttpServlet {
//调用 业务层 EmpService
EmpService empService=new EmpServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method=request.getParameter("method");
if (method.equals("list")){
list(request,response);
}
}
//查询全部
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用查询方法
List<Emp> emps=empService.findAll();
//把数据查到的放到request里面
request.setAttribute("emp",emps);
System.out.println("集合数据-----"+emps);
request.getRequestDispatcher("WEB-INF/jsp/findAll.jsp").forward(request,response);
}
}
EmpService类
public interface EmpService {
//登录
Emp login(String name, String password);
//查询全部
List<Emp> findAll();
}
EmpService实现类 ----EmpServiceImpl
public class EmpServiceImpl implements EmpService {
//调用Dao层
EmpDao empDao=new EmpDaoImpl();
@Override
public Emp login(String name, String password) {//登录方法
return empDao.login(name,password);
}
@Override
public List<Emp> findAll() {//查询全部
return empDao.findAll();
}}
**EmpDao类 **
public interface EmpDao {
//登录
Emp login(String name, String password);
//查询全部
List<Emp> findAll();
}
EmpDao实现类 ----EmpDaoImpl
public class EmpDaoImpl implements EmpDao {
DbHelper db=new DbHelper();
@Override
public Emp login(String name, String password) {
Connection conn = db.getConnection();
Emp emp=new Emp();
String sql="select * from emp where name=? and password=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1,name);
ps.setString(2,password);
//执行SQL语句
ResultSet rs = ps.executeQuery();
if (rs.next()){
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setBirthday(rs.getString("brithday"));
emp.setSex(rs.getString("sex"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return emp;
}
@Override
public List<Emp> findAll() {
Connection conn = db.getConnection();
List<Emp> emps=new ArrayList<>();
String sql="SELECT emp.*,dept.name dname FROM emp,dept WHERE emp.did=dept.id";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
Emp emp=new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setBirthday(rs.getString("birthday"));
emp.setSex(rs.getString("sex"));
Dept dept=new Dept();
dept.setId(rs.getInt("id"));
dept.setName(rs.getString("dname"));
emp.setDept(dept);
emps.add(emp);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return emps;
}}
findAll.jsp页面
<body>
<table id="tb">
<tr>
<td align="center">编号</td>
<td align="center">名称</td>
<td align="center">性别</td>
<td align="center">生日</td>
<td align="center">所属部门</td>
<td align="center">操作</td>
</tr>
<c:forEach items="${emp}" var="e">
<tr>
<td align="center">${e.id}</td>
<td align="center">${e.name}</td>
<td align="center">${e.sex}</td>
<td align="center">${e.birthday}</td>
<td align="center">${e.dept.name}</td>
<td align="center">
<a href="javascript:del(${e.id})">删除</a>|
<a href="${pageContext.request.contextPath}/EmpServlet?method=edit&id=${e.id}">编辑</a></td>
</tr>
</c:forEach>
</table>
</body>
本文地址:https://blog.csdn.net/weixin_49368462/article/details/107592329
如对本文有疑问, 点击进行留言回复!!
使用纯前端JavaScript实现Excel导入导出方法过程详解
微信小程序完美解决scroll-view高度自适应问题的方法
bootstrap-closable-tab可实现关闭的tab标签页插件
网友评论