热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

菜鸟学步之石器时代jspMVC数据操作

大纲:一、通用数据类二、增加数据三、查询数据通用数据类packagecom.sss.util;importjava.sql.*;publicclassDB{publ

大纲:

一、通用数据类

二、增加数据

三、查询数据

 

======通用数据类===========================

 

package com.sss.util;
import java.sql.*;public class DB {public static Connection getConn() {Connection conn = null;try {Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/smud","root","root");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return conn;}public static PreparedStatement prepare(Connection conn, String sql) {PreparedStatement pstmt = null; try {if(conn != null) {pstmt = conn.prepareStatement(sql);}} catch (SQLException e) {e.printStackTrace();}return pstmt;}public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) {PreparedStatement pstmt = null; try {if(conn != null) {pstmt = conn.prepareStatement(sql, autoGenereatedKeys);}} catch (SQLException e) {e.printStackTrace();}return pstmt;}public static Statement getStatement(Connection conn) {Statement stmt = null; try {if(conn != null) {stmt = conn.createStatement();}} catch (SQLException e) {e.printStackTrace();}return stmt;}/*public static ResultSet getResultSet(Connection conn, String sql) {Statement stmt = getStatement(conn);ResultSet rs = getResultSet(stmt, sql);close(stmt);return rs;}*/public static ResultSet getResultSet(Statement stmt, String sql) {ResultSet rs = null;try {if(stmt != null) {rs = stmt.executeQuery(sql);}} catch (SQLException e) {e.printStackTrace();}return rs;}public static void executeUpdate(Statement stmt, String sql) {try {if(stmt != null) {stmt.executeUpdate(sql);}} catch (SQLException e) {e.printStackTrace();}}public static void close(Connection conn) {try {if(conn != null) {conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}}public static void close(Statement stmt) {try {if(stmt != null) {stmt.close();stmt = null;}} catch (SQLException e) {e.printStackTrace();}}public static void close(ResultSet rs) {try {if(rs != null) {rs.close();rs = null;}} catch (SQLException e) {e.printStackTrace();}}public static void close(ResultSet rs,Statement stmt,Connection conn) {try {if(rs != null) {rs.close();rs = null;}if(stmt != null) {stmt.close();stmt = null;}if(conn != null) {conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}}}

 

====增加数据===========================================

jsp页面

添加地图

 

地图名称:

地图标签:


地图描述:

是否根地图

 


 

 



 

form提交给servlet类

 

package com.smud.servlet;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.smud.dao.impl.MapDAOImpl;
import com.smud.model.Map;public class MapAddServlet extends HttpServlet{/*** */private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stub
doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");//获取前台页面数据String mapName =request.getParameter("mapName");//地图名称String depict =request.getParameter("depict");//地图描述String isRootMap =request.getParameter("isRootMap");//是否根地图 1---是 0---否String lable =request.getParameter("lable");//地图标签String parentMapID =request.getParameter("parentMapID");//父地图id
System.out.print(mapName);System.out.print(isRootMap);System.out.print(parentMapID);//建立地图对象Map map = new Map();map.setMapName(mapName);map.setDepict(depict);if(isRootMap=="是根地图"){map.setIsRootMap(1);}else{map.setIsRootMap(0);}map.setLable(lable);map.setParentMapID(Integer.parseInt(parentMapID));MapDAOImpl mapDAOImpl = new MapDAOImpl();int count = mapDAOImpl.insert(map);if(count>0){response.sendRedirect("貌似提交成功");}else {response.getWriter().write("提交失败");}}}

 

model层

package com.smud.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import com.smud.dao.MapDAO;
import com.smud.model.Map;
import com.smud.util.DB;public class MapDAOImpl implements MapDAO {/*** 插入地图信息* * @param id* @return*/public int insert(Map map) {int count = 0;Connection conn = null;PreparedStatement pst = null;try {conn = DB.getConn();pst = conn.prepareStatement("insert into map(mapName,depict,isRootMap,lable,parentMapID) values(?,?,?,?,?)");pst.setString(1, map.getMapName());pst.setString(2, map.getDepict());pst.setInt(3, map.getIsRootMap());pst.setString(4, map.getLable());pst.setInt(5, map.getParentMapID());count = pst.executeUpdate();}catch(Exception e){e.printStackTrace();}finally{DB.close(pst);DB.close(conn);}return count;}/*** 根据地图编号查询单个地图信息* * @param id* @return*/public Map select(int id) {Map map = null;Connection conn = null;PreparedStatement pst = null;ResultSet rs = null;try {conn = DB.getConn();pst = conn.prepareStatement("select * from map where mapId=?");pst.setInt(1, id);rs = pst.executeQuery();if (rs.next()) {map = new Map();map.setMapId(rs.getInt("mapId"));map.setMapName(rs.getString("mapName"));map.setDepict(rs.getString("depict"));map.setIsRootMap(rs.getInt("isRootMap"));map.setLable(rs.getString("lable"));map.setParentMapID(rs.getInt("parentMapID"));}} catch (Exception e) {e.printStackTrace();} finally {DB.close(rs, pst, conn);}return map;}/*** 查询所有地图信息的方法* @return*/public List select() {List list = new ArrayList();Connection conn = null;PreparedStatement pst = null;ResultSet rs = null;try {conn = DB.getConn();pst = conn.prepareStatement("select * from map");rs = pst.executeQuery();while (rs.next()) {Map map = new Map();map.setMapId(rs.getInt("mapId"));map.setMapName(rs.getString("mapName"));map.setDepict(rs.getString("depict"));map.setIsRootMap(rs.getInt("isRootMap"));map.setLable(rs.getString("lable"));map.setParentMapID(rs.getInt("parentMapID"));list.add(map);}} catch (Exception e) {e.printStackTrace();} finally {DB.close(rs, pst, conn);}return list;}/*** 根据用户ID删除地图信息* * @param id* @return*/public int delete(int id) {int count = 0;Connection conn = null;PreparedStatement pst = null;try {conn = DB.getConn();pst = conn.prepareStatement("delete map where mapId=?");pst.setInt(1, id);count = pst.executeUpdate();} catch (Exception e) {} finally {DB.close(null, pst, conn);}return count;}/** 修改地图信息* */public int update(Map map){int count = 0;Connection conn = null;PreparedStatement pst = null;try {conn = DB.getConn();pst = conn.prepareStatement("update map set mapName=?,depict=?,isRootMap=?,lable=?,parentMapID=?,notes1=?,notes2=?,notes3=? where mapId=?");pst.setString(1, map.getMapName());pst.setString(2, map.getDepict());pst.setInt(3, map.getIsRootMap());pst.setString(4, map.getLable());pst.setInt(5, map.getParentMapID());pst.setString(6, map.getNotes1());pst.setString(7, map.getNotes2());pst.setString(8, map.getNotes3());pst.setInt(9,map.getMapId());count = pst.executeUpdate();} catch (Exception e) {e.printStackTrace();} finally {DB.close(null, pst, conn);}return count;}}

 model层

 

package com.smud.model;public class Map {private int mapId;private String mapName;//地图名称private String depict;//地图描述private int isRootMap;//是否根地图 1---是 0---否private String lable;//地图标签private int parentMapID;//父地图idprivate String notes1;private String notes2;private String notes3;public String getNotes1() {return notes1;}public void setNotes1(String notes1) {this.notes1 = notes1;}public String getNotes2() {return notes2;}public void setNotes2(String notes2) {this.notes2 = notes2;}public String getNotes3() {return notes3;}public void setNotes3(String notes3) {this.notes3 = notes3;}public int getMapId() {return mapId;}public void setMapId(int mapId) {this.mapId = mapId;}public String getMapName() {return mapName;}public void setMapName(String mapName) {this.mapName = mapName;}public String getDepict() {return depict;}public void setDepict(String depict) {this.depict = depict;}public int getIsRootMap() {return isRootMap;}public void setIsRootMap(int isRootMap) {this.isRootMap = isRootMap;}public String getLable() {return lable;}public void setLable(String lable) {this.lable = lable;}public int getParentMapID() {return parentMapID;}public void setParentMapID(int parentMapID) {this.parentMapID = parentMapID;}}

 

 

 

转:https://www.cnblogs.com/eddiego/archive/2012/08/12/2635580.html



推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 多维数组的使用
    本文介绍了多维数组的概念和使用方法,以及二维数组的特点和操作方式。同时还介绍了如何获取数组的长度。 ... [详细]
author-avatar
手机用户2502905615
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有