谁有java web中对数据库的增删改查的完整例子吗,

 我来答
育知同创教育
2016-10-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
文件太大 上传不了。。。。我给你看下部分代码public class CommentServlet extends HttpServlet {private static final long serialVersionUID = -2540110714710583202L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获取从放入request中的method的值request.setCharacterEncoding("UTF-8");String method = request.getParameter("method");if (method == null) {method = "";}//判断method的值调用不同方法if (method.equals("add")) {add(request, response);} else if (method.equals("admin")) {admin(request, response);} else if (method.equals("delete")) {delete(request, response);} else {admin(request, response);}}public void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String name = request.getParameter("name");String content = request.getParameter("content");String blog_id = request.getParameter("blog_id");if (name == null | name.equals("")) {name = "匿名";}String sql = "insert into comment (username,content,blog_id,createdtime) values(?,?,?,now())";String params[] = { name, content, blog_id };QueryRunner qr = DbHelper.getQueryRunner();try {qr.update(sql, params);} catch (SQLException e) {e.printStackTrace();}response.sendRedirect("/blog/servlet/HomeServlet?method=display&id="+ blog_id);}public void admin(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession session = request.getSession();User user = (User) session.getAttribute("user");if (user == null) {response.sendRedirect("/blog");} else {String sql = "select c.id,username,c.content,c.createdtime,title from comment c,blog b where blog_id=b.id order by c.id desc";QueryRunner qr = DbHelper.getQueryRunner();List list = null;try {list = (List) qr.query(sql, new BeanListHandler(Comment.class));} catch (SQLException e) {e.printStackTrace();}request.setAttribute("list", list);request.getRequestDispatcher("/admin/adminCommentList.jsp").forward(request, response);}}public void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {HttpSession session = request.getSession();User user = (User) session.getAttribute("user");if (user == null) {response.sendRedirect("/blog");} else {String id = request.getParameter("id");String ctid = request.getParameter("ctid");String id2 = (String) request.getParameter("id2");System.out.println(id2);if (ctid != null) {id = ctid;}String sql = "delete from comment where id=" + id;QueryRunner qr = DbHelper.getQueryRunner();try {qr.update(sql);} catch (SQLException e) {e.printStackTrace();}if (ctid == null) {response.sendRedirect("CommentServlet?method=admin");} else {request.setAttribute("id2", id2);request.getRequestDispatcher("HomeServlet?method=display").forward(request, response);}}}}下面这个类是连接数据库的 package cn.com.jobedu.blog; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.commons.dbutils.QueryRunner; public class DbHelper { public static QueryRunner getQueryRunner() { // 数据源对象可以理解为连接池的管理者,通过他可以获取数据库的链接 DataSource ds = null; Context context; try { // 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象 context = new InitialContext(); ds = (DataSource) context.lookup("java:comp/env/jdbc/mysqlds"); } catch (NamingException e) { System.out.println("获取数据源失败!"); } QueryRunner qr = new QueryRunner(ds); return qr; } }
追问
请问你会用json通过app获取java web的数据吗
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式