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); } }