精华内容
下载资源
问答
  • jsp增删改查

    2013-10-20 21:10:37
    jsp+servlet+mysql实现的增删改查功能,带mysql数据库
  • jsp 增删改查

    2018-05-11 15:47:00
    request.getRequestDispatcher("userList.jsp").forward(request,response); } 配置web.xml文件 <servlet-name>UserList <servlet-class>com.david.web.UserList</servlet-class> <servlet-name>...
    使用Idea创建项目
    1.新建web application项目 Idea 选择 Java Enterprise -> web application
    2.新版本没有web-inf文件夹 解决方法 进入project structure ->facets->点击小加号,添加web.xml
    3.在web/WEB-INF目录下新建两个文件夹,classes用于存放servlet的字节码文件(.class),lib用于存放项目引用的包。
    4.进入Project Structure,进入Modules(IDEA的工程)选项卡,将Paths的两个输出路径均改成第2步新建的classes。
    5.然后点击Dependencies,选择右边+号,新建JARS路径,选择第2步创建的lib文件夹 - >Jar Directory
    6.进入Artifacts选项卡,将输出目录定为Tomcat安装位置的webapps下新建的该工程文件夹
    7.Run->Edit Configurations配置Tomcat,设置tomcat端口等信息。
    8.设置默认启动路径http://localhost:9999/web/ 或手动输入
     
    导入servlet.jar包
    file->project structure ->libraries->加号->tomcat下的lib 即可。
     
    数据结构
    CREATE TABLE `david2018_db`.`User` (
    `UserId` INT NOT NULL AUTO_INCREMENT,
    `UserName` NVARCHAR(30) NULL,
    `PassWord` NVARCHAR(30) NULL,
    `Age` INT NULL,
    `Sex` NVARCHAR(30) NULL,
    PRIMARY KEY (`UserId`));
     
    测试数据
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('武大郎','123456',35,'男');
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('潘金莲','123456',18,'女');
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('西门庆','123456',24,'男');
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('武松','123456',22,'男');
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('鲁智深','123456',30,'男');
    insert into david2018_db.User (UserName,PassWord,Age,Sex) values ('林冲','123456',32,'男');
     
    创建项目结构
    src/com.david.dao 数据访问层
    src/com.david.domain 模型层
    src/com.david.service 服务层
    src/com.david.utils 工具层
    src/com.david.web servlet层
     
    创建数据库配置文件src新建 database.properties
    driverName=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/david2018_db
    user=root
    password=1234
     
    连接池的jar包 dbcp、pool、logging
     
    如果报错,java.lang.NoClassDefFoundError: org/apache/commons/dbutils/ResultSetHandler
    Artifacts->点击右下角fix即可
     
    编写domain 实体类
    public class User {
        private int UserId;
        private String UserName;
        private String PassWord;
        private int Age;
        private String Sex;
    
    
        public int getUserId() {
            return UserId;
        }
        public void setUserId(int UserId) {
            this.UserId = UserId;
        }
        public String getUserName() {
            return UserName;
        }
        public void setUserName(String UserName) {
            this.UserName = UserName;
        }
        public String getPassWord() {
            return PassWord;
        }
        public void setPassWord(String PassWord) {
            this.PassWord = PassWord;
        }
        public int getAge() {
            return Age;
        }
        public void setAge(int Age) {
            this.Age = Age;
        }
        public String getSex() {
            return Sex;
        }
        public void setSex(String Sex) {
            this.Sex = Sex;
        }
    }
    

    编写工具类 获得数据库连接 utils包下创建DBUtil类

    public class DBUtil {
        public static BasicDataSource dataSource = new BasicDataSource();
    
        static {
            try {
                Properties pp = new Properties();
                InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("database.properties");
                pp.load(is);
                String driver = pp.getProperty("driverName");
                String url = pp.getProperty("url");
                String username = pp.getProperty("user");
                String password = pp.getProperty("password");
    
                dataSource.setDriverClassName(driver);
                dataSource.setUrl(url);
                dataSource.setUsername(username);
                dataSource.setPassword(password);
                dataSource.setInitialSize(10);//初始化连接数
                dataSource.setMaxIdle(5); //最大空闲
                dataSource.setMinIdle(1); //最小空闲
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static BasicDataSource GetDataSource(){
            return dataSource;
        }
    
    
    }
    

     

    编写获得User列表接口 dao创建UserDao类
    //获取全部列表
    public List<User> GetAllUser() throws SQLException {
        String sql = "select * from User";
        QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
        List<User> users = qr.query(sql,new BeanListHandler<User>(User.class));
        return users;
    }
    编写User服务 调用dao中的 GetAllUser方法
    public List<User> GetAllService() throws SQLException {
        UserDao dao = new UserDao();
        return dao.GetAllUser();
    }
    编写Servlet 将列表数据转发到jsp页面
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService service = new UserService();
        List<User> userList = null;
        try {
            userList = service.GetAllService();
    
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("userList", userList);
        request.getRequestDispatcher("userList.jsp").forward(request,response);
    }
    配置web.xml文件
    <servlet>
        <servlet-name>UserList</servlet-name>
        <servlet-class>com.david.web.UserList</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserList</servlet-name>
        <url-pattern>/userList</url-pattern>
    </servlet-mapping>
    编写userList.jsp 渲染页面
    <%@ page import="com.david.domain.User" %>
    <%@ page import="java.util.List" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>用户列表</title>
    </head>
    <body>
    <a href="AddUser.jsp">添加用户</a>
    <table>
        <tr>
            <th>用户ID</th>
            <th>用户名</th>
            <th>用户密码</th>
            <th>用户年龄</th>
            <th>用户性别</th>
            <th>操作</th>
        </tr>
        <%
            if (request.getAttribute("userList") != null) {
        %>
        <%for (User u : (List<User>) request.getAttribute("userList")) {%>
        <tr>
            <td><%=u.getUserId()%>
            </td>
            <td><%=u.getUserName()%>
            </td>
            <td><%=u.getPassWord()%>
            </td>
            <td><%=u.getAge()%>
            </td>
            <td><%=u.getSex()%>
            </td>
            <td>
                <a href="GetUserById?userId=<%=u.getUserId()%>">修改</a>
                <a href="DeleteUser?userId=<%=u.getUserId()%>">删除</a>
            </td>
        </tr>
        <%}%>
        <%}%>
    </table>
    </body>
    </html>

     

    页面中可以使用el jstl语法 我就不写了
    列表完成,下面编写添加用户方法
    UserDao输入如下方法
    //添加用户
    public boolean AddUser(User user) throws SQLException {
        String sql = "insert into User (UserName,PassWord,Age,Sex) values (?,?,?,?)";
        QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
        Object[] params = {user.getUserName(),user.getPassWord(),user.getAge(),user.getSex()};
        int result = qr.update(sql,params);
        return result > 0;
    }

     

    UserService
    public boolean AddUser(User user) throws SQLException {
        UserDao dao = new UserDao();
        return dao.AddUser(user);
    }

     

    servlet
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
    
        User u = new User();
        u.setUserName(userName);
        u.setPassWord(passWord);
        u.setAge(Integer.parseInt(age));
        u.setSex(sex);
    
        UserService users = new UserService();
        try {
            boolean result  = users.AddUser(u);
            if(result){
                response.sendRedirect("userList");
            }else{
    
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
     

     

    AddUser.jsp页面如下
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>添加用户</title>
    </head>
    <body>
    <form method="post" action="AddUser">
        <p>用户名:<input name="userName"></p>
        <p>密码:<input name="passWord"></p>
        <p>性别:
            <input type="radio" checked value="男" name="sex"><input type="radio" value="女" name="sex"></p>
        <p>年龄:<select name="age">
            <%for (int i = 18; i < 50; i++) {%>
            <option value="<%=i%>"><%=i%></option>
            <%}%>
        </select></p>
        <p>
            <button type="submit">添加</button>
        </p>
        <a href="userList">用户列表</a>
    </form>
    
    </body>
    </html>

     

     
    xml文件
    <servlet>
        <servlet-name>AddUser</servlet-name>
        <servlet-class>com.david.web.AddUser</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AddUser</servlet-name>
        <url-pattern>/AddUser</url-pattern>
    </servlet-mapping>

     

     
    要设置mysql的字符集编码是utf8的 否则会乱码
     
    编写删除方法
    dao中
        //删除用户
        public boolean DeleteUser(int userId) throws SQLException {
            String sql = "delete from User where UserId = ?";
            QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
            return qr.update(sql,userId) > 0;
        }
    

     

    service
        public boolean DeleteUser(int userId) throws SQLException {
            UserDao dao = new UserDao();
            return dao.DeleteUser(userId);
        }
    

     

    servlet
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String userId = request.getParameter("userId");
            UserService service = new UserService();
            int id = Integer.parseInt(userId);
            try {
                boolean result = service.DeleteUser(id);
                if(result){
                    response.sendRedirect("userList");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    

     

    web.xml
        <servlet>
            <servlet-name>DeleteUser</servlet-name>
            <servlet-class>com.david.web.DeleteUser</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>DeleteUser</servlet-name>
            <url-pattern>/DeleteUser</url-pattern>
        </servlet-mapping>
    userList.jsp加入路径即可
    <a href="DeleteUser?userId=<%=u.getUserId()%>">删除</a> 
    修改
    dao
        //查找一条User通过UserId
        public User GetUserByUserId(int userId) throws SQLException {
            String sql = "select * from User where UserId = ?";
            QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
            User user = qr.query(sql, userId, new BeanHandler<User>(User.class));
            return user;
        }
        //修改user
        public boolean UpdateUser(User user) throws SQLException {
            String sql = "update User set UserName = ?,PassWord = ?,Age = ?, Sex = ? where UserId = ?";
            Object [] params = {user.getUserName(),user.getPassWord(),user.getAge(),user.getSex(),user.getUserId()};
            QueryRunner qr = new QueryRunner(DBUtil.GetDataSource());
            int result = qr.update(sql,params);
            return result > 0;
        }
    

     

    service
        public User GetUserByUserId(int userId) throws SQLException {
            UserDao dao = new UserDao();
            return dao.GetUserByUserId(userId);
        }
        //修改user
        public boolean UpdateUser(User user) throws SQLException {
            UserDao dao = new UserDao();
            return dao.UpdateUser(user);
        }
    servlet
    GetUserById
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String userId = request.getParameter("userId");
            UserService service = new UserService();
            try {
                User u =service.GetUserByUserId(Integer.parseInt(userId));
                request.setAttribute("user",u);
                request.getRequestDispatcher("Update.jsp").forward(request,response);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    updateUser
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            String userId = request.getParameter("userId");
            String userName = request.getParameter("userName");
            String passWord = request.getParameter("passWord");
            String age = request.getParameter("age");
            String sex = request.getParameter("sex");
    
            User u = new User();
            u.setUserName(userName);
            u.setPassWord(passWord);
            u.setAge(Integer.parseInt(age));
            u.setSex(sex);
            u.setUserId(Integer.parseInt(userId));
    
            UserService users = new UserService();
            try {
                boolean result = users.UpdateUser(u);
                if (result) {
                    response.sendRedirect("userList");
                } else {
    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    

     

    userList.jsp 加入修改入口
    <a href="GetUserById?userId=<%=u.getUserId()%>">修改</a>
    updateuser.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>修改用户</title>
    </head>
    <body>
    <%
    User u = (User)request.getAttribute("user");
    %>
    <form method="post" action="UpdateUser">
        <p>用户Id:<input name="userId" value="<%=u.getUserId()%>" readonly></p>
        <p>用户名:<input name="userName" value="<%=u.getUserName()%>"></p>
        <p>密码:<input name="passWord" value="<%=u.getPassWord()%>"></p>
        <p>性别:
            <input type="radio" <%if(u.getSex().equals("男")){%> checked <%}%> value="男" name="sex">男
            <input type="radio" <%if(u.getSex().equals("女")){%> checked <%}%> value="女" name="sex">女
        </p>
        <p>年龄:<select name="age">
            <%for (int i = 18; i < 50; i++) {%>
            <option value="<%=i%>"><%=i%></option>
            <%}%>
        </select>
        <script>
            for(var i = 0;i<document.getElementsByTagName("option").length;i++){
                var option = document.getElementsByTagName("option")[i];
                if(option.value == "<%=u.getAge()%>"){
                    option.selected = true;
                }
            }
        </script>
        </p>
        <p>
            <button type="submit">确认修改</button>
        </p>
        <a href="userList">用户列表</a>
    </form>
    
    </body>
    </html>
    

     

    xml.web
        <servlet>
            <servlet-name>UpdateUser</servlet-name>
            <servlet-class>com.david.web.UpdateUser</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>UpdateUser</servlet-name>
            <url-pattern>/UpdateUser</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>GetUserById</servlet-name>
            <servlet-class>com.david.web.GetUserById</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>GetUserById</servlet-name>
            <url-pattern>/GetUserById</url-pattern>
        </servlet-mapping>
    

     

     

    转载于:https://www.cnblogs.com/baidawei/p/9025063.html

    展开全文
  • 简单的jsp增删改查

    2018-12-12 18:02:58
    jsp增删改查,,,写得比较简单,只是简单的进行了一下示范。
  • Servlet+JSP增删改查

    2018-04-20 16:01:35
    Servlet+JSP增删改查是框架最底层的写法,学习框之前建议先学jsp
  • servlet+jsp增删改查

    2018-07-30 10:45:49
    myeclipes+servlet+jsp增删改查文件 myeclipes+servlet+jsp增删改查文件 myeclipes+servlet+jsp增删改查文件
  • Jsp增删改查

    2010-09-02 23:10:16
    @Jsp中Java实现增删改查@ /////////////////////////////////////////////////查询///////////////////////////////// //查询本班全部信息 public String listStuAll(){ Session session =new Configuration()....

    @Jsp中Java实现增删改查@

    /查询/
     //查询本班全部信息
     public String listStuAll(){
      Session session =new Configuration().configure().buildSessionFactory().openSession();
      try{
        Query query = session.createQuery("from StudentInfo where grade='"+grade_hidden+"'");
        stu_info = query.list();
        ActionContext.getContext().getSession().put("stu_list",stu_info);
      }
      catch(Exception e){
       e.printStackTrace();
      }
      session.close();
      return "stu_success";
     }

     
     //查询本班全部成绩
     public String listScoreAll(){
      Session session =new Configuration().configure().buildSessionFactory().openSession();
      try{
        score = session.createQuery("from Score where grade='"+grade_hidden+"'").list();
        ActionContext.getContext().getSession().put("score_list",score);
      }
      catch(Exception e){
       e.printStackTrace();
      }
      session.close();
      return "score_success";
     }
     
     
     //按学号查询学生信息
     public String stuFindBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      try{
       stu_info = session.createQuery("from StudentInfo where no='"+sno+"' and grade='"+grade_hidden+"'").list();
       if(stu_info.size() == 0){
        return "no_id";
       }
      }catch(RuntimeException e){
       throw e;
      }
      finally{
       session.close();
      }
      return "stu_success";
     }

     
     //按学号查询成绩
     public String ScoreFindBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      try{
       score = session.createQuery("from Score where no='"+sno+"' and grade='"+grade_hidden+"'").list();
       if(score.size() == 0){
        return "no_id";
       }
      }catch(RuntimeException e){
       throw e;
      }
      finally{
       session.close();
      }
      return "score_success";
     }
     
    /添加// 
     //增加学生信息
     public String addStuInfo(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      try{
       if(grade_hidden == "0")
       {
        session.save(stu_info.get(0));
        tx.commit();
       }
       else
       {
        stu_info.get(0).setGrade(grade_hidden);
        session.save(stu_info.get(0));
        tx.commit();
       }
      }catch(RuntimeException e){
       if(tx != null)
        tx.rollback();
       return "error_throw";
      }
      finally{
       session.close();
      }
      return "stu_success";
     }
     
     //增加学生成绩
     public String addStuScore(){
      System.out.println("grade:~~~~~~~~~~~grade:"+grade_hidden);
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction tx = session.beginTransaction();
      try{
       stu_info = session.createQuery("from StudentInfo where no='"+score.get(0).getNo()+"' and grade='"+grade_hidden+"'").list();
       if(stu_info.size() == 0){
        return "no_id";
       }
       if(session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list().size() == 0)
       {
        score.get(0).setGrade(grade_hidden);
        session.save(score.get(0));
        tx.commit();
       }
       else
        return "no_repeat";
      }catch(RuntimeException e){
       if(tx != null)
        tx.rollback();
       throw e;
      }
      finally{
       session.close();
      }
      return "score_success";
     }

    ///删除/ 
     //按学号删除学生信息
     public String stuDelBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction ts = session.beginTransaction();
      try{
       //ts = session.beginTransaction();
       stu_info = session.createQuery("from StudentInfo where no='"+sno+"' and grade='"+grade_hidden+"'").list();
       if(stu_info.size() == 0){
        return "no_id";
       }
       else
       {
        session.delete(stu_info.get(0));
        ts.commit();
       }
      }catch(RuntimeException e){
       if(ts != null)
        ts.rollback();
       throw e;
      }
      finally{
       session.close();
      }
      return "stu_del_success";
     }

     
     //按学号删除成绩
     public String ScoreDelBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction ts = session.beginTransaction();
      try{
       ts = session.beginTransaction();
       score = session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list();
       if(score.size() == 0){
        return "no_id";
       }
       else
       {
        session.delete(score.get(0));
        ts.commit();
       }
      }catch(RuntimeException e){
       if(ts != null)
        ts.rollback();
       throw e;
      }
      finally{
       session.close();
      }
      return "score_del_success";
     }
     
    ///修改学生信息/ 
     //按学号修改学生信息(先查询)
     public String StuModBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      try{
       stu_info = session.createQuery("from StudentInfo where no='"+sno+"' and grade='"+grade_hidden+"'").list();
       if(stu_info.size() == 0){
        return "no_id_no";
       }
       else
       {
        no_mod_query = stu_info.get(0).getNo();
        grade_mod_query = stu_info.get(0).getGrade(); 
        return "stu_mod_query_success";
       }
      }catch(RuntimeException e){
       throw e;
      }
      finally{
       session.close();
      }
     }
      
     //按学号修改学生信息(修改)
     public String StuModBySnoY(){
      System.out.println("cccccccccccccccccccccccccccccc________________________ccccccccccccccccccccccccccccccccccccccc");
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction ts = session.beginTransaction();
      try{
       System.out.println("dddddddddddddddddddddddd________________________dddddddddddddddddd");
       ts = session.beginTransaction();
       session.createQuery("update StudentInfo set password='"+stu_info.get(0).getPassword()+"' ,name='"+stu_info.get(0).getName()+"' ,phone='"+stu_info.get(0).getPhone()+"' ,address='"+stu_info.get(0).getAddress()+"' where no='"+no_mod_query+"' and grade='"+grade_mod_query+"'").executeUpdate();
       ts.commit();    
      }catch(RuntimeException e){
       if(ts != null)
        ts.rollback();
       throw e;
      }
      finally{
       session.close();
      }
      return "stu_mod_success";
     }
       
    ///修改成绩/ 
     //按学号修改成绩(先查询)
     public String ScoreModBySno(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      try{
       score = session.createQuery("from Score where no='"+score.get(0).getNo()+"' and c_name='"+score.get(0).getC_name()+"' and grade='"+grade_hidden+"'").list();
       if(score.size() == 0){
        return "no_id";
       }
       else
       {
        no_mod_query = score.get(0).getNo();
        grade_mod_query = score.get(0).getGrade();
        c_name_mod_query = score.get(0).getC_name();
       }
      }catch(RuntimeException e){
       throw e;
      }
      finally{
       session.close();
      }
      return "score_mod_query_success";
     }
      
     //按学号修改成绩(修改)
     public String ScoreModBySnoY(){
      Configuration config = new Configuration().configure();
      SessionFactory sessionFactory = config.buildSessionFactory();
      Session session = sessionFactory.openSession();
      Transaction ts = session.beginTransaction();
      try{
       ts = session.beginTransaction();
       session.createQuery("update Score set score='"+score.get(0).getScore()+"' where no='"+no_mod_query+"' and c_name='"+c_name_mod_query+"' and grade='"+grade_mod_query+"'").executeUpdate();
       ts.commit();    
      }catch(RuntimeException e){
       if(ts != null)
        ts.rollback();
       throw e;
      }
      finally{
       session.close();
      }
      return "score_mod_success";
     }
     

    展开全文
  • JSP增删改查 -简易通讯录 JSP增删改查 -简易通讯录
  • servlet+JSTL+JSP增删改查

    2015-06-19 09:47:14
    servlet+JSTL+JSP增删改查,简单的,很详细,初学者适合
  • jsp增删改查.zip

    2021-01-22 13:10:48
    mvc设计模式,简单的对数据库的增删改查
  • Jsp 增删改查 程序

    2009-11-18 17:51:59
    Jsp 增删改查 程序 完全如题,呵呵。
  • JSP增删改查项目(mysql) JSP增删改查项目(mysql) JSP增删改查项目(mysql)
  • jsp增删改查 mysql

    2019-03-05 11:00:08
    JSP连接Mysql数据库——增删改查基本操作 创建项目 导入jar包 建立数据库 建立表 输入数据 创建包结构 创建info实体类 . 获取数据库连接
  • jsp增删改查及XML解析

    2020-05-22 00:46:19
    一、jsp增删改查 1、数据库的连接 2、三层架构 3、servlet界面 4、界面 二、xml解析 1、Java中配置文件的三种配置位置及读取方式 2. XML的作用 3. dom4j+xpath解析xml文件 JSP增删改查 一、准备工作: 1、...

    目录:

    一、jsp增删改查

    1、数据库的连接

    2、三层架构

    3、servlet界面

    4、界面

    二、xml解析

    1、Java中配置文件的三种配置位置及读取方式

    2. XML的作用
    3. dom4j+xpath解析xml文件

    JSP增删改查

    一、准备工作:

    1、导入jar包

    2、导入两个类,连接mysql数据库

    3、查看并修改config.properties,检查 主机、端口号、密码、用户名、库名是否正确

    二、建五个包

    三、写好实体类,实现Serializable接口

     

    四、三层架构,dao包,biz包,接口

    1、dao包接口

    2、实现接口,增删改查大同小异,其中以查询所有为例,查询所有和模糊查询返回的是集合,单个查询返回对象,增删改返回受影响行数

    sql语句中有列名需添加`符号,不然会报错

    3、biz包的接口以及实现

    五、servlet类

    需要继承HttpServlet

    为防止乱码,需要设置字符编码

     req.setCharacterEncoding("utf-8");

     resp.setContentType("text/html; charset=UTF-8");

    存值有两种方式,使用session或request

    使用session需要获取session

    语句:HttpSession session = req.getSession();

    存值:session.setAttribute()/request.setAttribute()

    在界面弹出框首先需要获取out

    PrintWriter out = resp.getWriter();

    获取集合时可以先作判断,如果集合有值,则跳转到主界面,没有值就可以提示一下,方便自己寻找错误

    六、界面

    使用c标签需要在界面上方导入c标签

    语句:<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

    成果:

    点击查看详情将获取到id调用单个查询的方法跳转到详情页面,可以进行删除和修改

    总结:config.properties一定要检查清楚,dao方法中使用mysql数据库的sql语句与oracle数据库中的sql语句有一定的差别,配置web.xml时要检查是否与页面上的一致,出现端口占用可以启动任务管理器结束进程或关机重启

    XML解析

    一、Java中配置文件的三种配置位置及读取方式(getResourceAsStream这个方法具有容错性)

    1、读取同根路径下的资源

    类名.class.getResourceAsStream("/db.properties");

    1.1加载资源文件

    1.2实例化属性文件

    1.3加载流对象

    2、获取同包下的资源
     类名.class.getResourceAsStream("db.properties");

    2.1加载资源文件

    2.2实例化属性文件

    2.3加载流对象

    /

    3、获取WEB-INF下的资源

    ServletContext context = this.getServletContext();
     InputStream in = context.getResourceAsStream("/WEB-INF/db.properties");

    配置 @WebServlet(name="demo",urlPatterns="/demo")

    相当于

     

    二、dom4j+xpath解析xml文件

    xpath等同数据库的select语句

    document.selectNodes(xpath);//查一组
     document.selectSingleNode(xpath);//查单个

    实现步骤:
    1、加载资源文件|
    2、解析得到所有的student标签
    3、通历得到每一个student标签xml内容
    4、判断该student的sid
    5、解析该student标签下面的子标签name
    6、打印sid以及name值

     

    1、利用dom4j进行解析

    2、dom4j+xpath解析

    总结:xml比较重要

    展开全文
  • JSP增删改查参考代码

    2011-09-22 20:14:51
    JSP增删改查参考代码 ............................
  • jsp增删改查源代码

    2016-08-31 20:56:17
    jsp简单的web增删改查代码,附上mysql数据库
  • JSP增删改查操作 1.删 <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import=...

    JSP增删改查操作

    1.删

    <%@ page import="java.sql.Connection" %>
    <%@ page import="java.sql.DriverManager" %>
    <%@ page import="java.sql.Statement" %>
    <%@ page import="com.media.bean.UserBean" %><%--
      Created by IntelliJ IDEA.
      User: 了
      Date: 2020/10/2
      Time: 10:32
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
      <jsp:useBean id="user" class="com.media.bean.UserBean"></jsp:useBean>
      <jsp:setProperty name="user" property="*"></jsp:setProperty>
      <%
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://localhost:3306/book";
        Connection connection = DriverManager.getConnection(url,"root","root");
        Statement stmt = connection.createStatement();
        //String sql ="insert into user(username,password,gender,age) values ('"+user.getUsername()+"','"+user.getPassword()"',1,22)";
        //String sql ="update user set status = 3 where id = 10";
        String sql = "delete from user where id = 10";
        int count = stmt.executeUpdate(sql);
        if (count>0){
          out.print("删除成功");
        }else{
          out.print("删除失败");
        }
      %>
      ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201002115928200.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1dXSFpaU1Q=,size_16,color_FFFFFF,t_70#pic_center)
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201002120036331.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1dXSFpaU1Q=,size_16,color_FFFFFF,t_70#pic_center)
    2.查
    
    ```java
    <%@ page import="java.sql.Connection" %>
    <%@ page import="java.sql.DriverManager" %>
    <%@ page import="java.sql.Statement" %>
    <%@ page import="java.sql.ResultSet" %><%--
      Created by IntelliJ IDEA.
      User: 了
      Date: 2020/10/7
      Time: 8:02
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%
    //加载数据库驱动
        Class.forName("com.mysql.jdbc.Driver");
        //建立数据库连接
        String url = "jdbc:mysql://localhost:3306/book";
        Connection connection = DriverManager.getConnection(url,"root","root");
        //使用Statement对象执行sql语句
        Statement stmt = connection.createStatement();
        String sql = "select * from user";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
        }
    %>
    </body>
    </html>
    
    

    在这里插入图片描述

    展开全文
  • 通用分页+jsp增删改查

    2020-05-30 20:56:28
    jsp增删改查分页 数据库:MSQL①准备工作1.1打开navicat进行数据库连接1.2进行数据库连接1.3新建一个数据库1.4然后将表格导入1.5在数据库新建查询②打开eclipse开始建项目2.1项目创建2.2先导入需要用到的jar包2.3...
  • jsp增删改查分页 数据库:MSQL①准备工作1.1打开navicat进行数据库连接1.2进行数据库连接1.3新建一个数据库1.4然后将表格导入1.5在数据库新建查询②打开eclipse开始建项目2.1项目创建2.2先导入需要用到的jar包2.3...
  • jfinal jsp增删改查

    2014-02-25 16:23:14
    jfinal 对mysql的增删改查,对入门者还是有帮助的。。。
  • Servlet+jdbc+jsp增删改查

    千次阅读 2018-01-13 16:05:26
    Servlet+jdbc+jsp增删改查目录Servlet 1. 新建一个工程名为servletStudy 2. tomcat xml配置 3. servlet类 User类 Function类 //插入 //查找 //删除 //修改 WEB-INF下xml Jsp文件 user_list.jsp ...
  • JSP增删改查案例

    2014-04-12 14:45:50
    这是一个简单的java增删改查简单数据表操作 适合初学者 希望能帮到大家
  • JSP增删改查数据库

    2012-05-16 12:54:08
    能很好的实现JSP页面对数据库的增删改查
  • 一个mvc框架的增删改查小项目,方便大家学习,用esclipse 和 sql server 2008 R2开发的,包含
  • hibernate+struts实现jsp增删改查分页! hibernate实现对数据库的操作,struts实现对表单的操作! 实现了分页功能! (注:本本项目中文会出现乱码,我还没有解决!附有数据库文件!)
  • JSP增删改查 案例一个,绝对真实
  • jsp增删改查 源码加操作视频

    热门讨论 2009-12-08 10:19:48
    jsp做的增删改查 有源码 也有视频
  • 没有写注释,也没有整理,适合练手,适合初学者,实现了登录,登出,记录来访数,数据库增删改查等功能
  • jsp增删改查项目

    2012-09-11 20:45:01
    适合新手,包看懂,看不懂找我

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,820
精华内容 1,528
热门标签
关键字:

jsp增删改查