热门标签 | 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



推荐阅读
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社区 版权所有