标签:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>地区列表</title>
</head>
<body>
<!-- 无序排序,版面设计 -->
<form method="post" action="InsertMember" >
<input name="parentid" type="hidden" value="0" />
<ul>
<li>请输入地区:<input name="name" type="text" width=30 /></li>
<br>
<li>请输入邮编:<input name="postcode" type="text" width=30 /></li>
<li><input type="submit" value="提交" /> <input type="reset" value="取消" /> </li>
</ul>
</form>
</body>
</html>
package com.hanqi; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class InsertMember */ @WebServlet("/InsertMember") public class InsertMember extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public InsertMember() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String postcode = request.getParameter("postcode"); //接收副ID String parentid = request.getParameter("parentid"); if(name != null && name.trim().length() > 0) { //get方式时需要的转码 post方式不需要 // name = new String(name.getBytes("ISO-8859-1"),"UTF-8"); if(parentid != null && parentid.trim().length() > 0) { //调用数据访问层 //构建实体类 实例化 Members m = new Members(); m.setName(name); m.setParentid(Integer.parseInt(parentid)); m.setPostCode(postcode); MembersDAL md = new MembersDAL(); try { if(md.insert(m) >0) { response.sendRedirect("index.jsp"); } else { response.getWriter().append("保存数据失败"); } } catch (Exception e) { response.getWriter().append("发生错误" + e.getMessage()); e.printStackTrace(); } } else { response.getWriter().append("parentid不能为空"); } } else { response.getWriter().append("请正确填写地区名称"); } response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi.dao; import java.sql.*; public class DBHelper { //获取connection对象 static 静态方法 不需要调用他的实例化 public static Connection getConnection() throws Exception //工具类中异常不要用try catch { //加载数据库驱动,并注册到驱动管理器 Class.forName("oracle.jdbc.driver.OracleDriver"); //数据库连接字符串 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //获取connection对象 (个人理解为登陆数据库 并将数据关联) Connection conn = DriverManager.getConnection(url,"MemberDB","123654789"); //url,名称,密码 return conn; } }
package com.hanqi.dao; public class Members { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } private int parentid; public int getParentid() { return parentid; } public void setParentid(int parentid) { this.parentid = parentid; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } private String postCode; public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } }
package com.hanqi.dao; import java.sql.*; import java.util.*; public class MembersDAL { //增 //创建一个实体类 m 并将它放置进去 public int insert(Members m) throws Exception { int rtn = 0; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "insert into MEMBERS (id, parentid, name,PostCode) values (SQ_MEMBERDB_ID.nextval,?,?,?) "; pst = conn.prepareStatement(sql); pst.setInt(1, m.getParentid()); pst.setString(2, m.getName()); pst.setString(3, m.getPostCode()); //将执行结果赋给返回值 rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception e) { conn.close(); } } } return rtn; } }
标签:
原文地址:http://www.cnblogs.com/name-hanlin/p/5058308.html