首页 > 数据库技术 > 详细

一、从网页给数据库添加数据

时间:2020-09-29 17:45:38      阅读:52      评论:0      收藏:0      [点我收藏+]
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);
	}
}

  

一、从网页给数据库添加数据

原文:https://www.cnblogs.com/wsx123/p/13750426.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!