• 一、从网页给数据库添加数据(添加,修改,删除)


    package holle;
    
    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.dbutils.QueryRunner;
    
    import utils.C3P0Utils;
    
    public class Demo_03 extends HttpServlet{
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		//处理编码格式
    		//使用网页给数据库添加数据
    		//数据库链接
    		QueryRunner qu = new QueryRunner(C3P0Utils.getDataSource());
    		//预编译方法
    		String sql = "insert into product(PNAME,PRICE) VALUES(?,?)";
    		//index的值!通过网页的name和password值进入数据库
    		String name = req.getParameter("name");
    		String password = req.getParameter("password");
    		//异常最大化
    		try {
    			//把name和password放进o对象内,
    			Object[] o = {name,password};
    			//把sql添加数据,在把o网页上输出的内容给数据库(qu)
    			qu.update(sql,o);
    			System.out.println("添加成功");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		//跳转链接
    		req.getRequestDispatcher("index.jsp").forward(req, resp);
    	}
    }
    

      

    package holle3;
    
    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.dbutils.QueryRunner;
    
    import utils.C3P0Utils;
    
    public class Demo_01 extends HttpServlet{
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		// 通过网页修改一条数据
    		QueryRunner qu = new QueryRunner(C3P0Utils.getDataSource());
    		//要修改的数据格式
    		String sql = "update product set  PNAME=?,PRICE=? WHERE PID=?";
    		//jsp的属性值
    		String name = req.getParameter("name");
    		String password = req.getParameter("password");
    		String pid = req.getParameter("pid");
    
    		//异常的最大化
    		try {
    			//把属性值放进对象中
    			Object [] o = {name,password,pid};
    			//网页输入的内容放入数据库
    			qu.update(sql,o);
    			System.out.println("修改成功");
    		} catch (SQLException e) {
    			// TODO Auto-generated catch 
    			e.printStackTrace();
    		}
    		//跳转的链接
    		req.getRequestDispatcher("update.jsp").forward(req, resp);
    	}
    }
    

      

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    /*从网页中删除一条数据*/
    QueryRunner qu = new QueryRunner(C3P0Utils.getDataSource());
    String sql = "delete from product where pid=?";
    String pid = req.getParameter("pid");

    try {
    Object o[] = {pid};
    qu.update(sql,o);
    System.out.println("删除成功");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    req.getRequestDispatcher("delete.jsp").forward(req, resp);
    }

      

  • 相关阅读:
    Oracle 分析函数(Analytic Functions) 说明
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    Build Your Own Oracle RAC 10g Release 2 Cluster on Linux and FireWire
    ORACLE SEQUENCE 介绍
    RAC Ocfs2文件系统常见问题解决方法
    linux 下修改日期和时间
    寒假刷题之7——波纹
    iOS 游戏 Oh my fish! 切割痕迹实现
    ACM常识
    寒假刷题之6——迷宫
  • 原文地址:https://www.cnblogs.com/wsx123/p/13750426.html
Copyright © 2020-2023  润新知