zoukankan      html  css  js  c++  java
  • 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();  
                        }  
                    }  
                }  
            }
    }
    

  • 相关阅读:
    android开发我的新浪微博客户端登录页面UI篇(4.1)
    android开发我的新浪微博客户端用户授权页面UI篇(3.1)
    js操作select相关方法(收集)
    C# 位操作
    用c#创建支持多语言的WinForm应用程序
    简单的Android对文件进行读写操作
    nginx 安装+反向代理设置
    安装APK文件到Android模拟器
    浅谈C# Socket编程及C#如何使用多线程
    iPhone 动画效果类型
  • 原文地址:https://www.cnblogs.com/aixing/p/13327701.html
Copyright © 2011-2022 走看看