• JDBC连接Oracle实现增、删、改操作



    最近在做一个练习项目“在线考试系统”,在将整体架构搭好然后将任务分配给组员以后,自己完成自己部分的功能,在自己所完成的功能当初,涉及了一个jsp页面写入数据,将数据提交保存至数据库,可对数据实现增、删、改、查等操作,将其中主要功能实现以作总结。


    jsp页面的form表单

    <form method="post" action="AddMent.action">
        <input name="deptid" type="text">
        <input type="text" name="deptid"  placeholder="请输入部门编号" />
        <input type="text" name="deptname"  placeholder="请输入部门名称" />
        <input type="text" name="deptnews"  placeholder="请输入部门信息" />
        <button class="button" type="submit"> 提交</button>  
    

    AddMent.action实现向数据新增数据操作

    @WebServlet(value="/System/AddMent.action")
    public class AddMentDao extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            doPost(req, resp);
            super.doGet(req, resp); 
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
    /*      String uname = req.getParameter("uname");
            String qx = req.getParameter("qx");*/
            Connection con=null;
            Statement st=null;
            try {
                //1.加载驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //2.建立数据库连接
                con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
                //3.获取执行sql语句的平台
                st= con.createStatement();
                String deptid = req.getParameter("deptid");
                String deptname = req.getParameter("deptname");
                String deptnew = req.getParameter("deptnew");
                //4.执行sql语句插入数据
                String sql = "INSERT INTO dept(deptid, deptname, deptnew) "  
                        + " VALUES(?, ?, ?)";  
                 PreparedStatement pstmt = con.prepareStatement(sql);  
                 pstmt.setString(1, req.getParameter("deptid"));  
                 pstmt.setString(2, req.getParameter("deptname"));  
                 pstmt.setString(3, req.getParameter("deptnew"));  
                 pstmt.execute();  
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{             
                //关闭st  
                if(st != null){  
                    try {  
                        st.close();  
                    } catch (SQLException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
                //关闭con  
                if(con != null){  
                    try {  
                        con.close();  
                    } catch (SQLException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }

    DeleteMent删除操作

    @WebServlet(value="/System/Delete.Action")
    public class DeleteMent extends HttpServlet{
            /**
             * 添加试题
             */
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException {
                // TODO Auto-generated method stub
                doPost(req, resp);
                super.doGet(req, resp);
            }
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException {
                // TODO Auto-generated method stub
                String deptname = req.getParameter("deptname");
                Connection con=null;
                Statement st=null;
                try {
                    //1.加载驱动
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    //2.建立数据库连接
                    con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
                    //3.获取执行sql语句的平台
                    st= con.createStatement();
                    //4.执行sql语句插入数据
                    String sql = "DELETE FROM dept WHERE deptname=?";
                    PreparedStatement pstmt = con.prepareStatement(sql);
                    pstmt.setString(1, req.getParameter("deptname"));  
                    pstmt.execute(); 
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{             
                    //关闭st  
                    if(st != null){  
                        try {  
                            st.close();  
                        } catch (SQLException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    }  
                    //关闭con  
                    if(con != null){  
                        try {  
                            con.close();  
                        } catch (SQLException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            }
    }
    

    UpdateMent修改操作

    @WebServlet(value="/System/Update.action")
    public class UpdateMent extends HttpServlet{
            /**
             * 添加试题
             */
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException {
                // TODO Auto-generated method stub
                doPost(req, resp);
                super.doGet(req, resp);
            }
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException {
                // TODO Auto-generated method stub
                Connection con=null;
                Statement st=null;
                try {
                    //1.加载驱动
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    //2.建立数据库连接
                    con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jredu","OnlineTest","Jredu12345");
                    //3.获取执行sql语句的平台
                    st= con.createStatement();
                    String deptid = req.getParameter("deptid");
                    String deptname = req.getParameter("deptname");
                    String deptnew = req.getParameter("deptnew");
                    String sql="update dept set deptid=?,deptname=?,deptnew=?"+  
                             "where deptid=?";  
                     PreparedStatement pstmt = con.prepareStatement(sql);  
                     pstmt.setString(1, req.getParameter("deptid"));  
                     pstmt.setString(2, req.getParameter("deptname"));  
                     pstmt.setString(3, req.getParameter("deptnew"));  
                     pstmt.setString(4, req.getParameter("deptid"));  
                     pstmt.execute();    
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{             
                    //关闭st  
                    if(st != null){  
                        try {  
                            st.close();  
                        } catch (SQLException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    }  
                    //关闭con  
                    if(con != null){  
                        try {  
                            con.close();  
                        } catch (SQLException e) {  
                            // TODO Auto-generated catch block  
                            e.printStackTrace();  
                        }  
                    }  
                }  
            }
    }
    

  • 相关阅读:
    【转】awk内置变量
    【转】awk数组操作
    【转】awk 数组用法【精华贴】
    【转】linux shell 逻辑运算符、逻辑表达式
    指挥作战
    人脸相关
    TD
    后台
    前台 html 空格
    linux
  • 原文地址:https://www.cnblogs.com/aixing/p/13327701.html
Copyright © 2020-2023  润新知