EL:部门信息查询:
工具类DBUtil:连接数据库、SqlSession:用来接收一条sql语句,并执行,并把执行结果转为实体类对象,这几个类没写
DBUtil类:工具类:
package com.bjpowernode.util;
import java.sql.*;
/**
* author : 动力节点
* 2019/3/14 0014
*/
public class DBUtil {
private static String className="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/bjpowernode";
private static String username="root";
private static String password="123456";
private static Connection conn=null;
private static PreparedStatement ps=null;
static{
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
try {
conn= DriverManager.getConnection(url, username,password);
//conn.setAutoCommit(false);//设置事务为手动提交
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static PreparedStatement createStatement(String sql){
getConn();
try {
ps= conn.prepareStatement(sql);
}catch (Exception ex){
ex.printStackTrace();
}
return ps;
}
public static void close(ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close( PreparedStatement ps,Connection conn){
close(null, ps, conn);
}
public static void close(ResultSet rs, PreparedStatement ps,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
RequestUtil类:这个工具类好像没用
package com.bjpowernode.util;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.Enumeration;
public class RequestUtil {
/*
* 功能:将请求协议包参数信息,赋值给对应的实体类对象
*
* 要求: 请求参数名称必须与实体类对象属性名【完全一致】
*/
public static Object init(HttpServletRequest request,Class classFile){
Object obj = null;
Enumeration paramNames=null;
//1.创建一个对应的实体类的对象
try {
obj = classFile.newInstance();
//2.得到当前请求协议包中所有的【参数名称】
paramNames =request.getParameterNames();
//3.读取请求参数并赋值
while (paramNames.hasMoreElements()){
String paramName =(String)paramNames.nextElement();//deptNo
String value = request.getParameter(paramName);
if(value!=null && !"".equals(value)){
Field fieldObj = classFile.getDeclaredField(paramName);//private Integer deptNo;
fieldObj.setAccessible(true);
String typeName = fieldObj.getType().getName();
if("java.lang.String".equals(typeName)){
fieldObj.set(obj, value);
}else if("java.lang.Integer".equals(typeName)){
fieldObj.set(obj, Integer.valueOf(value));
}else if("java.lang.Double".equals(typeName)){
fieldObj.set(obj, Double.valueOf(value));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
sqlSession:工具类
package com.bjpowernode.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class SqlSession {
/*
* 作用:
* 1)将接受一条查询语句,并执行
* 2)将得到执行结果数据行,转换为实体类对象
* 3)将实体类对象保存到List
*
*/
public static List selectList(String sql,Class classFile){
PreparedStatement ps = DBUtil.createStatement(sql);
ResultSet rs = null;
Field fieldArray[]=null;
List list = new ArrayList();
//1.推送查询语句得到一个结果集[ResultSet]
try {
rs =ps.executeQuery();
//2.获得实体类相关属性
fieldArray = classFile.getDeclaredFields();
//3.赋值转换
while(rs.next()){
Object obj = classFile.newInstance();
for(int i=0;i5){
setBuffer.append(",");
}
setBuffer.append(fieldName);
setBuffer.append(" = ");
if("java.lang.String".equals(typeName) || "java.util.Date".equals(typeName)){
setBuffer.append("'");
setBuffer.append(value);
setBuffer.append("'");
}else{
setBuffer.append(value);
}
}
}
sql.append(tableName);
sql.append(setBuffer);
sql.append(whereBuffer);
System.out.println("sql : "+sql);
ps = DBUtil.createStatement(sql.toString());
flag = ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
DBUtil.close(null);
}
return flag;
}
}
实体类Dept:
package com.bjpowernode.model;
public class Dept {
private Integer deptNo;
private String dname;
private String loc;
public Dept(Integer deptNo, String dname, String loc) {
this.deptNo = deptNo;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
dao层:
package com.bjpowernode.dao;
import com.bjpowernode.model.Dept;
import com.bjpowernode.util.SqlSession;
import java.util.List;
public class DeptDao {
public Dept findById(String deptNo){
String sql="select * from dept where deptno="+deptNo;
List deptList= SqlSession.selectList(sql,Dept.class);
return (Dept) deptList.get(0);
}
}
控制层DeptFindIdSeervlet:
package com.bjpowernode.controller;
import com.bjpowernode.dao.DeptDao;
import com.bjpowernode.model.Dept;
import java.io.IOException;
public class DeptFindByIdServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
String deptNo=null;
DeptDao dao=new DeptDao();
Dept dept=null;
//1.读取来自浏览器发送的请求协议包中的请求参数【部门编号】
deptNo=request.getParameter("deptno");
//2.JDBC查询当前部门编号关联的详细信息(结果是Dept对象)
dept=dao.findById(deptNo);
//3.将查询到的信息添加到request
request.setAttribute("key",dept);
//4.向tomcat申请调用dept.jsp。将数据写入到响应包
request.getRequestDispatcher("/dept.jsp").forward(request,response);
}
}
dept.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 12:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--需要加jstl的标签--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Title
部门编号
部门名称
部门位置
JSTL:部门查询:
工具类是上方DBUtil类、SqlSession类
Dept类:
package com.bjpowernode.model;
public class Dept {
private Integer deptNo;
private String dname;
private String loc;
public Dept(Integer deptNo, String dname, String loc) {
this.deptNo = deptNo;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptNo() {
return deptNo;
}
public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
dao层:
package com.bjpowernode.dao;
import com.bjpowernode.model.Dept;
import com.bjpowernode.util.SqlSession;
import java.util.List;
public class DeptDap {
public List findAll(){
String sql="select * from dept";
List deptList= SqlSession.selectList(sql, Dept.class);
return deptList;
}
}
controller层:
DeptFindServlet:
package com.bjpowernode.controller;
import com.bjpowernode.dao.DeptDap;
import java.io.IOException;
import java.util.List;
public class DeptFindServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
List deptList=null;
DeptDap dao=new DeptDap();
//1.JDBC查询所有部门的信息
deptList=dao.findAll();
//2.部门信息添加到request
request.setAttribute("key",deptList);
//3.请求转发,向tomcat申请dept.jsp
request.getRequestDispatcher("/dept.jsp").forward(request,response);
}
}
dept.jsp:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/7/20
Time: 12:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--需要加jstl的标签--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Title
部门编号
部门名称
部门位置
${dept.deptNo}
${dept.dname}
${dept.loc}
数据库中的表: