博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
菜鸟学步之石器时代 jsp MVC 数据操作
阅读量:6089 次
发布时间:2019-06-20

本文共 9765 字,大约阅读时间需要 32 分钟。

大纲:

一、通用数据类

二、增加数据

三、查询数据

 

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

 

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;    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(request, response);    }    @Override    protected 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;//父地图id	private 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

你可能感兴趣的文章
03-3 BGP专有命令--联盟
查看>>
ExtJS4.2学习(二)Ext统一组件模型
查看>>
Linux系统管理命令--lsof
查看>>
iis下 ActiveSync插件无法访问(上)
查看>>
Puppet学习笔记之常用资源类型
查看>>
Android第三十五期 - 记住登录实现和Fragment的页面
查看>>
Configuring Basic EIGRP
查看>>
java枚举类型学习
查看>>
shell实现秒级crontab计划任务
查看>>
Excel2010重复打印标题行
查看>>
Internet Server Application Programming Interface
查看>>
DHCP原理解析及其在cisco上的配置
查看>>
H3C路由器上配置远程端口镜像(3种配置方式之1)
查看>>
分布式存储
查看>>
repadmin查看域控之间的复制状态
查看>>
自定义ORM系列(三)工具雏形及基本用法
查看>>
配置RIP、下一跳、静态、单臂示例
查看>>
DELL 2950配置Raid操作
查看>>
windows7系统缺失误删default web site该怎么解决
查看>>
Linux 监控工具之Cacti使用详解(二)
查看>>