首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编写一个Java Web项目,实现从properties文件读取数据存储到数据库,并从数据库中读取数据,将结果显示在页面上。启动mysql数据库服务器端,并且创建一个名为studentinfo的数据库

编写一个Java Web项目,实现从properties文件读取数据存储到数据库,并从数据库中读取数据,将结果显示在页面上。启动mysql数据库服务器端,并且创建一个名为studentinfo的数据库

作者头像
CaesarChang张旭
发布2021-04-28 11:35:50
6.9K1
发布2021-04-28 11:35:50
举报
文章被收录于专栏:悟道悟道

现在是 2021年04月26日11:05:08 ,趁今天有空把它搞完. 这里呢采用德鲁伊连接池哈 记得导包

1先上效果图

效果
效果

2上截图

点击删除2

3代码共享

dataOperation.jsp

<%--
  Created by IntelliJ IDEA.
  User: zhangxu
  Date: 2021/4/26
  Time: 10:32 上午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
</head>
<body>
 
 
<form action="/modify?id=${stu.id}" method="post">
    <input value="${stu.name}" type="text" name="name">姓名 <br>
    <input value="${stu.sex}" type="text"  name="sex">性别 <br>
    <input value="${stu.major}" type="text"  name="major">职业 <br>
    <input value="${stu.hometown}" type="text"  name="hometown">故乡 <br>
    <input type="submit"  value="确认修改">
</form>
</body>
</html>

StudentDao

import zx.bean.Student;
 
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
public interface StudentDao {
 
     List<Student> getStudentsFromDB() throws SQLException, IOException;
 
     void delete(int id) throws SQLException;
 
     void insertDB(ArrayList students);
     void insert(Student student);
 
 
     Student findById(Integer id);
 
     void update(int id, Student newStudent);
}

StudentdaoImpl(这个不写,但是Dao层主要是靠这个跟数据库打交道,所以挂一下)

package zx.dao.impl;
 
import zx.bean.Student;
import zx.dao.StudentDao;
import zx.util.DruidUtil;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
 
//接口名+Impl=当前类名  表示一个实现类
public class StudentDaoimpl extends DruidUtil implements StudentDao {
    @Override
    public List<Student> getStudentsFromDB() {
        ArrayList<Student> list=new ArrayList<>();
        Connection connection =null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet =null;
 
        try {
 
            connection =  getConnection();
 
            preparedStatement= connection.prepareStatement("select * from student");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setSex(resultSet.getString("Sex"));
                student.setMajor(resultSet.getString("major"));
                student.setHometown(resultSet.getString("hometown"));
 
                list.add(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return list;
    }
 
    @Override
    public void delete(int id) throws SQLException {
 
        Connection connection =null;
        PreparedStatement preparedStatement=null;
 
        try{
            connection =  getConnection();
            preparedStatement= connection.prepareStatement("delete  from student where id="+id);
            preparedStatement.executeUpdate();
        }catch (Exception e){
            throw  e;
        }finally {
            close(connection,preparedStatement,null);
        }
 
    }
 
    @Override
    public void insertDB(ArrayList students) {
        //首先我们我们要解析文件
        ResourceBundle resource = ResourceBundle.getBundle("/Student");
 
        //解析文件以后我们将文件内容存入数据库
        Connection connection =null;
        PreparedStatement preparedStatement=null;
 
 
        List list = new ArrayList();
 
        int id = Integer.parseInt(resource.getString("id"));
        String name=resource.getString("name");
        String sex=resource.getString("sex");
        String major=resource.getString("major");
        String hometown=resource.getString("hometown");
 
        try{
            connection =  getConnection();
            preparedStatement=connection.prepareStatement("insert into student values("+id+",'"+name+"','"+sex+"','"+major+"','"+hometown+"')"+";");
            preparedStatement.executeUpdate();
        }catch (Exception e){
            System.out.println(e);
        }finally {
            if(connection==null){
                close(null,preparedStatement,null);
                return ;
            }
            close(connection,preparedStatement,null);
        }
    }
 
    @Override
    public void insert(Student student) {
 
        //解析文件以后我们将文件内容存入数据库
        Connection connection =null;
        PreparedStatement preparedStatement=null;
 
 
 
        Integer id=student.getId();
        String name=student.getName();
        String sex=student.getSex();
        String major=student.getMajor();
        String hometown=student.getHometown();
 
        try{
            connection =  getConnection();
            preparedStatement=connection.prepareStatement("insert into student values("+id+",'"+name+"','"+sex+"','"+major+"','"+hometown+"')"+";");
 
            preparedStatement.executeUpdate();
        }catch (Exception e){
            System.out.println(e);
        }finally {
            if(connection==null){
                close(null,preparedStatement,null);
                return ;
            }
            close(connection,preparedStatement,null);
        }
    }
 
    @Override
    public Student findById(Integer id) {
        Connection connection =null;
        PreparedStatement preparedStatement=null;
        ResultSet  set=null;
        Student student=new Student();
 
        try{
            connection =  getConnection();
            preparedStatement= connection.prepareStatement("select  * from student where id="+id);
            set= preparedStatement.executeQuery();
 
            while(set.next()){
 
                student.setId(id);
                student.setName(set.getString(2));
                student.setSex(set.getString(3));
                student.setMajor(set.getString(4));
                student.setHometown(set.getString(5));
                return student;
            }
 
 
        }catch (Exception e){
            System.out.println(e);
        }finally {
            close(connection,preparedStatement,set);
        }
 
        return null;
 
    }
 
    @Override
    public void update(int id, Student newStudent) {
        Connection connection =null;
        PreparedStatement preparedStatement=null;
        String newName=newStudent.getName();
        String newSex=newStudent.getSex();
        String newMajor=newStudent.getMajor();
        String newHometown=newStudent.getHometown();
 
 
 
        try{
            connection =  getConnection();
            preparedStatement= connection.prepareStatement("update student set name= '"+newName+"', sex='"+newSex+"' ,major='"+newMajor+"', hometown='"+newHometown+"' where id="+id);
             preparedStatement.executeUpdate();
 
 
        }catch (Exception e){
            System.out.println(e);
        }finally {
            close(connection,preparedStatement,null);
        }
 
    }
}

addStudentServlet

package zx.servlets;
 
import zx.bean.Student;
import zx.service.StudentService;
import zx.service.impl.StudentServiceimpl;
 
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.SQLException;
import java.util.List;
 
/**
 * @Author CaesarChang张旭
 * @Date 2021/4/26  10:01 上午
 * @Version 1.0
 */
@WebServlet("/addStudent")
public class addStudentServlet  extends HttpServlet {
 
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService service=new StudentServiceimpl();
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String major = req.getParameter("major");
        String hometown = req.getParameter("hometown");
        Student student=new Student(Integer.parseInt(id),name,sex,major,hometown);
        service.insert(student);
 
        List<Student> getAll = null;
        try {
            getAll = service.getStudentFromDB();
            req.setAttribute("getAll",getAll);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
 
 
 
        req.getRequestDispatcher("show.jsp").forward(req,resp);
    }
}

findModifyStudent

package zx.servlets;
 
import zx.bean.Student;
import zx.service.StudentService;
import zx.service.impl.StudentServiceimpl;
 
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;
 
/**
 * @Author CaesarChang张旭
 * @Date 2021/4/26  10:25 上午
 * @Version 1.0
 */
@WebServlet("/findStu")
public class findModifyStudent extends HttpServlet {
 
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService service=new StudentServiceimpl();
        Integer id = Integer.parseInt(req.getParameter("id"));
        Student byId = service.findById(id);
        req.setAttribute("stu",byId);
        req.getRequestDispatcher("dataOperation.jsp").forward(req,resp);
    }
}

4结

当然其他部分还有很多,但是只要求写这几个,都给你们了哈 记得关注下 拜了个拜

打一波我自己课程的广告哈

数据库系统概论速成: https://www.bilibili.com/video/BV1jf4y147jz javaWeb课设: https://www.bilibili.com/video/BV1bh411D7Wb

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1先上效果图
  • 2上截图
  • 3代码共享
  • 4结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档