javaweb增删改查

大二下学期机考题目:基于javaweb对Address的增删改查



Address类

package entity;

public class Address{
	private int id;
	private String name;
	private String street;
	private String city;
	private String state;
	private String zip;

	public int getId(){
		return id;
	}
	public void setId(int id){
	    this.id = id;
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
	    this.name = name;
	}
	public String getStreet(){
		return street;
	}
	public void setStreet(String street){
	    this.street = street;
	}
	public String getCity(){
		return city;
	}
	public void setCity(String city){
	    this.city = city;
	}
	public String getState(){
		return state;
	}
	public void setState(String state){
	    this.state = state;
	}
	public String getZip(){
		return zip;
	}
	public void setZip(String zip){
	    this.zip = zip;
	}
}

工具类

package util;

import java.sql.*;

public class JdbcUtil{
	static{
	   try{
	       //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		   Class.forName("com.mysql.jdbc.Driver");
	   }catch(ClassNotFoundException e){
		   e.printStackTrace();
	   }
	}

	public static Connection getConnection() throws SQLException{
		//return DriverManager.getConnection("jdbc:odbc:address");
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/address?user=root&password=root&characterEncoding=utf-8");
	}
	
	public static void close(Statement stmt,Connection conn)throws SQLException{
	     stmt.close();
		 conn.close();
	}

	public static void close(ResultSet rs,Statement stmt,Connection conn)throws SQLException{
	     rs.close();
		 close(stmt,conn);
	}
    
}

dao包

//DAO:Data Access Object,Design Pattern设计模式
package dao;

import java.sql.*;
import java.util.*;

import entity.*;
import util.*;

public class AddressDao{
	public void add(Address address){
		try{
			Connection conn = JdbcUtil.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("insert into address (name,street,city,state,zip) values (?,?,?,?,?)");
			pstmt.setString(1,address.getName());
			pstmt.setString(2,address.getStreet());
			pstmt.setString(3,address.getCity());
			pstmt.setString(4,address.getState());
			pstmt.setString(5,address.getZip());
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt,conn);
		}catch(SQLException e){
		    e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public void delete(int id){
		try{
			Connection conn = JdbcUtil.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("delete from address where id =?");
			pstmt.setInt(1,id);
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt,conn);
		}catch(SQLException e){
		    e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public void update(Address address){
		try{
			Connection conn = JdbcUtil.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("update address set name=?,street=?,city=?,state=?,zip=? where id=?");
			pstmt.setString(1,address.getName());
			pstmt.setString(2,address.getStreet());
			pstmt.setString(3,address.getCity());
			pstmt.setString(4,address.getState());
			pstmt.setString(5,address.getZip());
			pstmt.setInt(6,address.getId());
			pstmt.executeUpdate();
			JdbcUtil.close(pstmt,conn);
		}catch(SQLException e){
		    e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
    public List
findAll(){ List
list = new ArrayList
(); try{ Connection conn = JdbcUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement("select * from address"); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ Address address = new Address(); address.setId(rs.getInt(1)); address.setName(rs.getString(2)); address.setStreet(rs.getString(3)); address.setCity(rs.getString(4)); address.setState(rs.getString(5)); address.setZip(rs.getString(6)); list.add(address); } JdbcUtil.close(pstmt,conn); }catch(SQLException e){ e.printStackTrace(); throw new RuntimeException(e); } return list; } public Address findById(int id){ Address address = new Address(); try{ Connection conn = JdbcUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement("select * from address where id=?"); pstmt.setInt(1,id); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ address.setId(rs.getInt(1)); address.setName(rs.getString(2)); address.setStreet(rs.getString(3)); address.setCity(rs.getString(4)); address.setState(rs.getString(5)); address.setZip(rs.getString(6)); } JdbcUtil.close(pstmt,conn); }catch(SQLException e){ e.printStackTrace(); throw new RuntimeException(e); } return address; } }


首先在首页index.jsp文件中定向到AddressServlet类中


  

    
    My JSP 'index.jsp' starting page
	
  
  
  
      
  

AddressServlet类负责处理底层数据

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AddressDao;
import entity.Address;

public class AddressServlet extends HttpServlet {
    private AddressDao addressDao = new AddressDao();   
    
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String method = request.getParameter("method");
	    String path="list.jsp"; 
		
	    if(method.equals("list")){
		    List
list = addressDao.findAll(); request.setAttribute("list", list); }else if(method.equals("add")){ String name = request.getParameter("name"); String street = request.getParameter("street"); String city = request.getParameter("city"); String state = request.getParameter("state"); String zip = request.getParameter("zip"); Address address = new Address(); address.setName(name); address.setStreet(street); address.setCity(city); address.setState(state); address.setZip(zip); addressDao.add(address); path="AddressServlet?method=list"; }else if(method.equals("delete")){ int id = Integer.parseInt(request.getParameter("id")); addressDao.delete(id); path="AddressServlet?method=list"; }else if(method.equals("toedit")){ int id = Integer.parseInt(request.getParameter("id")); Address address = addressDao.findById(id); request.setAttribute("address", address); path ="edit.jsp"; }else if(method.equals("edit")){ int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); String street = request.getParameter("street"); String city = request.getParameter("city"); String state = request.getParameter("state"); String zip = request.getParameter("zip"); Address address = new Address(); address.setId(id); address.setName(name); address.setStreet(street); address.setCity(city); address.setState(state); address.setZip(zip); addressDao.update(address); path="AddressServlet?method=list"; } request.getRequestDispatcher(path).forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

list文件显示所有信息

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="entity.Address;"%>



  
    
    
    list.jsp
	
  
  
  
               
                
               <%  
                   List
list =(List
)request.getAttribute("list"); for(Address addr : list){ %>
<% } %>
IdNameStreet CityStateZipOption
<%= addr.getId() %><%= addr.getName() %><%= addr.getStreet() %> <%= addr.getCity() %><%= addr.getState() %><%= addr.getZip() %> delete   edit
Add


add.jsp文件负责增加条目

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>




  

    
    My JSP 'index.jsp' starting page
	
  
  
  
      
Name:
Street:
City:
State:
Zip:



edit文件负责修改

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="entity.Address" %>



  

    
    edit.jsp
	
  
  
  
     <%  
       Address address =  (Address)request.getAttribute("address"); 
     %>
      
Name:
Street:
City:
State:
Zip:



你可能感兴趣的:(java,Web)