大纲:
一、通用数据类
二、增加数据
三、查询数据
======通用数据类===========================
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
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; } }