- 1、创建数据库
- 2、创建用户表和果树表
- 3、搭建项目
- 4、放入连接池和数据库连接文件
- 5、创建实体类
- 6、编写用户的dao层
- 7、编写用户的service
- 8、编写测试类
- 9、创建登录页面
- 10、编写login的servlet
- 11、编写果树的dao
- 12、编写果树的service
- 13、编果树的servlet
- 14、创建字符编码过滤器
- 15、编写show页面
- 16、编写添加页面
//数据库连接池
public class MyConnectionUtils {
//1、创建linkedList集合(存入线程
//Connection,数据库操作对象
static linkedList linkedList = new linkedList<>();
static {
//2、通过流的形式,读取是数据库的配置文件
InputStream resourceAsStream = MyConnectionUtils.class.getClassLoader().getResourceAsStream("db.properties");
//3、调用Properties();类
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
String mysqlDriver = (String) properties.get("mysqlDriver");
String mysqlUrl = (String) properties.get("mysqlUrl");
String mysqlName = (String) properties.get("mysqlName");
String mysqlPassword = (String) properties.get("mysqlPassword");
//4、加载驱动
Class.forName(mysqlDriver);
//5、使用循环获取10个连接,并把连接数放到连接池内
for (int i = 0; i < 10; i++) {
//6、使用内置对象DriverManager调用getConnection方法,把数据掺入里面
Connection connection = DriverManager.getConnection(mysqlUrl, mysqlName, mysqlPassword);
//7、放入集合
linkedList.add(connection);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//6、创建连接方法,好让实体类调用(用于传入sql语句以及执行其他几部)
//使用同步关键字实现线程同步,
//因为涉及等待,所以synchronized是同步锁
public synchronized Connection getConnection() throws InterruptedException {
//如果内部线程等于0 .代表线程被拿完了
if (linkedList.size()==0){
this.wait();
}
//如果有人来拿连接,那么连接池就会移除一个连接,代表打走了
Connection remove = linkedList.remove(0);
return remove;
}
//7、将连接放回连接处
public synchronized void closeConnection(Connection connection){
linkedList.add(connection);
//唤醒等待的线程
this.notifyAll();
}
}
5、创建实体类
6、编写用户的dao层
public class UserDaoImpl implements UserDao {
@Override
public User selectByUnameAndPwd(String uname, String pwd) throws InterruptedException {
//调用连接池类
MyConnectionUtils myConnectionUtils = new MyConnectionUtils();
//调用连接池中连接
Connection connection = myConnectionUtils.getConnection();
//获取预处理对象
String sql="select * from user where uname=? and pwd = ? ";
PreparedStatement ps = null;
User user = null;
try {
ps= connection.prepareStatement(sql);
//占位符的赋值
ps.setString(1,uname);
ps.setString(2,pwd);
//返回结果集
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()){
int uid = rs.getInt("uid");
String uname1 = rs.getString("uname");
String pwd1 = rs.getString("pwd");
//将查询的结果储存在实体类内
user = new User(uid,uname1,pwd1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭,释放资源
try {
if (ps!=null){
ps.close();
}
if(connection!=null){
myConnectionUtils.closeConnection(connection);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//返回用户对象
return user;
}
}
7、编写用户的service
修改页面局部,把他们放到表格中
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
登录页面
<%--使用post请求--%>
也可以输入空格,修改位置
设置表单登录的地址为:
//完成三件事,接收参数,调用service,完成跳转
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收前台参数
String uname = req.getParameter("uname");
String pwd = req.getParameter("pwd");
//调用service
UserServiceImpl userService = new UserServiceImpl();
User user = userService.selectByUnameAndPwd(uname, pwd);
//获得session对象
HttpSession session = req.getSession();
//跳转,如果发生错误,就给一个提示信息,并重定向到登录页面
if (user==null){
session.setAttribute("msg","账号密码错误");
resp.sendRedirect(req.getContextPath()+"/login.jsp");
}else {
//登录成功,存储用户信息到session中
session.setAttribute("user",user);
resp.sendRedirect(req.getContextPath()+"/show.jsp");
}
}
}
11、编写果树的dao
public class FruiterDaoImpl implements FruiterDao {
@Override
public List selectAllFruiter() {
//获取连接池
MyConnectionUtils myConnectionUtils = new MyConnectionUtils();
Connection connection=null;
PreparedStatement ps = null;
ArrayList fruiters = new ArrayList<>();
//获取连接
try {
connection= myConnectionUtils.getConnection();
String sql="select * from fruiter";
ps= connection.prepareStatement(sql);
//执行sql
ResultSet rs = ps.executeQuery();
//处理结果集
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
String production = rs.getString("Production");
Fruiter fruiter = new Fruiter(id, name, price, production);
//因为返回的是一个集合,所以要把遍历的数据存储到集合中
fruiters.add(fruiter);
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}finally {
try {
if (ps!=null){
ps.close();
}
if (connection!=null){
myConnectionUtils.closeConnection(connection);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return fruiters;
}
}
12、编写果树的service
//查询果树信息
@WebServlet("/QueryFruiterServlet")
public class QueryFruiterServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收参数
//调用service
FruiterServiceImpl fruiterService = new FruiterServiceImpl();
List list = fruiterService.selectAllFruiter();
//将信息放入 request域对象中
req.setAttribute("list",list);
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/show.jsp");
requestDispatcher.forward(req,resp);
}
}
另外让user的servlet重定向到果树的servlet
@WebFilter("/*")
public class CharacterFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//设置请求编码
servletRequest.setCharacterEncoding("utf-8");
//设置响应编码
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
System.out.println("字符编码启动完成");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
}
15、编写show页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
果树信息展示
当前在线人数:1000人
<%--设置标签的显示方式,防止找不到报错--%>
欢迎:${empty user?"":user.uname} 登出
添加花卉信息
| 编号: | 树名: | 价格: | 产地: | 操作: |
|---|---|---|---|---|
| ${i.id} | ${i.name} | ${i.price} | ${i.production} | 删除 修改 |
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
果树添加



