Model 1モードの学生情報の添削

23617 ワード

Student.java
package entity;

public class Student {
	private int stuid;
	private String stuname;
	private String gender;

	public int getStuid() {
		return stuid;
	}

	public void setStuid(int stuid) {
		this.stuid = stuid;
	}

	public String getStuname() {
		return stuname;
	}

	public void setStuname(String stuname) {
		this.stuname = stuname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Student()
	{
		
	}
	public Student(int stuid, String stuname, String gender) {
		super();
		stuid = this.stuid;
		stuname = this.stuname;
		gender = this.gender;
	}

}

Model.java
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.Student;

import util.DBUtil;

public class Model {
	private Statement sta;
	private ResultSet rs;
	PreparedStatement ps;
	DBUtil u=new DBUtil();
	
	
	public  int Insert(int stuid,String stuname,String gender) throws SQLException{
		Connection conn=u.getCon();
		String sql="insert student values(?,?,?)";
		ps=conn.prepareStatement(sql);
		ps.setInt(1,stuid);
		ps.setString(2,stuname);
		ps.setString(3,gender);
		int a=ps.executeUpdate();
		return a;
	}
	
	public  int delete(int stuid) throws SQLException{
		Connection conn=u.getCon();
		String sql="delete from student where stuid=?";
		ps=conn.prepareStatement(sql);
		ps.setInt(1,stuid);
		int a=ps.executeUpdate();
		return a;
	}
	
	public  int update(int stuid,String stuname,String gender) throws SQLException{
		Connection conn=u.getCon();
		String sql="update student set stuname=?,gender=? where stuid=?";
		ps=conn.prepareStatement(sql);
		ps.setInt(3,stuid);
		ps.setString(1,stuname);
		ps.setString(2,gender);
		int a=ps.executeUpdate();
		return a;
	}
	public List<Student> queryAll() throws SQLException{
		List<Student> students=new ArrayList<Student>();
		Connection conn=u.getCon();
		String  sql="select * from student";
		sta=conn.createStatement();
		rs=sta.executeQuery(sql);
		while(rs.next()){
			Student student=new Student();
			student.setStuid(rs.getInt("stuid"));
			student.setStuname(rs.getString("stuname"));
			student.setGender(rs.getString("gender"));
			students.add(student);
		}
		return students;
	}
	
	public Student queryById(int stuid) throws SQLException{
		Student student=new Student();
		Connection conn=u.getCon();
		String sql="select * from student where stuid=?";
		ps=conn.prepareStatement(sql);
		ps.setInt(1,stuid);
		rs=ps.executeQuery();
		if(rs.next()){
			student.setStuid(rs.getInt("stuid"));
			student.setStuname(rs.getString("stuname"));
			student.setGender(rs.getString("gender"));
		}
		return student;
		
	}
	
}
EncodingFilter.java
package servlet;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
	
	private String encoding = null;
	public void init(FilterConfig config) throws ServletException {
		this.encoding = config.getInitParameter("encoding");
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(this.encoding);
		response.setCharacterEncoding(encoding);
		chain.doFilter(request, response);
	}
	
	public void destroy() {
		this.encoding = null;
	}

}
DBUtil.java
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author sawyer 2014 1:20:16
 * 
 */

public class DBUtil {
	private Connection conn = null;
	private PreparedStatement stmt = null;
	private ResultSet rs = null;
	private static String driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/userdb";
	private String user = "root";
	private String password = "orcl";

	/**
	 * Get the driver
	 */
	static {

	}

	/**
	 * Connect the database
	 */
	public Connection getCon() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = (Connection) DriverManager
					.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * @param sql
	 * @param obj
	 *            Update
	 *//*
	public int update(String sql, Object... obj) {
		int count = 0;
		conn = getCon();
		try {
			stmt = conn.prepareStatement(sql);
			if (obj != null) {
				for (int i = 0; i < obj.length; i++) {
					stmt.setObject(i + 1, obj[i]);
				}
			}
			count = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return count;
	}

	*//**
	 * @param sql
	 * @param obj
	 *            Query
	 *//*
	public ResultSet Query(String sql, Object... obj) {
		conn = getCon();
		try {
			stmt = conn.prepareStatement(sql);
			while (obj != null) {
				for (int i = 0; i < obj.length; i++) {
					stmt.setObject(i + 1, obj[i]);
				}
			}
			rs = stmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return rs;
	}*/

	/**
	 * CLose the resource
	 */
	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <filter>
  <filter-name>EncodingFilter</filter-name>
  <filter-class>servlet.EncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>EncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<h1><a href="insert.jsp"> </a></h1>
	<hr>
	<h1><a href="delete.jsp"> </a></h1>
	<hr>
	<h1><a href="update.jsp"> </a></h1>
	<hr>
	<h1><a href="queryAll.jsp"> </a></h1>
	<hr>
	<h1><a href="queryById.jsp"> </a></h1>
</body>
</html>
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'deletes.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1> </h1>
	<form action="deleteShow.jsp" method="post">
		<table>
			<tr>
				<td> ID :</td>
				<td><input type="text" name="stuid" id="stuid">
				</td>
				<td><input type="submit" value="   " id="submit">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>

deleteShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'delete.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1> </h1>
    <%
    int stuid=Integer.parseInt(request.getParameter("stuid"));
    Model model=new Model();
    model.delete(stuid);
     %>
  </body>
</html>

insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'in.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <h1> </h1>
	<form action="insertShow.jsp" method="post">
		<table>
			<tr>
				<td> :</td>
				<td> :<input type="text" name="stuid" id="stuid">
				</td>
				<td> :<input type="text" name="stuname" id="stuname">
				</td>
				<td> :<input type="text" name="gender" id="gender">
				</td>
				<td><input type="submit" value=" " id="submit">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>

insertShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@page language="java" import="model.*"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">





<title>My JSP 'insert.jsp' starting page</title>





<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<span style="white-space:pre">	</span><link rel="stylesheet" type="text/css" href="styles.css">

<span style="white-space:pre">	</span>-->





</head>





<body>

<span style="white-space:pre">	</span><h1> </h1>

<span style="white-space:pre">	</span><%

<span style="white-space:pre">		</span>Model model = new Model();

<span style="white-space:pre">		</span>int stuid = Integer.parseInt(request.getParameter("stuid"));

<span style="white-space:pre">		</span>String stuname = request.getParameter("stuname");

<span style="white-space:pre">		</span>String gender = request.getParameter("gender");

<span style="white-space:pre">		</span>model.Insert(stuid, stuname, gender);

<span style="white-space:pre">	</span>%>

</body>

</html>
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'update.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<h1> </h1>
	<form action="updateShow.jsp" method="post">
		<table>
			<tr>
				<td> :</td>
				<td> :<input type="text" name="stuid" id="stuid">
				</td>
				<td> :<input type="text" name="stuname" id="stuname">
				</td>
				<td> :<input type="text" name="gender" id="gender">
				</td>
				<td><input type="submit" value=" " id="submit">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>

updateShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'update.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	<h1> </h1>
	<% 
    Model model=new Model();
    int stuid = Integer.parseInt(request.getParameter("stuid"));
	String stuname = request.getParameter("stuname");
	String gender = request.getParameter("gender");
    model.update(stuid, stuname, gender);
   %>
</body>
</html>
queryById.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'queryById.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1> </h1>
	<form action="queryByIdShow.jsp" method="post">
		<table>
			<tr>
				<td> ID :</td>
				<td><input type="text" name="stuid" id="stuid">
				</td>
				<td><input type="submit" value="   " id="submit">
				</td>
			</tr>
		</table>
	</form>
  </body>
</html>
queryByIdShow.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'queryById.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1> </h1>
    <table>
    <tr><td> </td><td> </td><td> </td></tr>
    <% 
    int stuid=Integer.parseInt(request.getParameter("stuid"));
    Model model=new Model();
    Student student=model.queryById(stuid);
     %>
      <tr>
    <td><%=student.getStuid()%></td>
    <td><%=student.getStuname()%></td>
   <td><%=student.getGender()%></td>
   </tr>
   </table>
  </body>
</html>
queryAll.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page language="java" import="model.*,entity.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'queryAll.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <table>
    <tr><td> </td><td> </td><td> </td></tr>
    <%
    Model model=new Model();
    List<Student> list=model.queryAll();
    for(Student stu:list)
    {%>
    <tr>
    <td><%=stu.getStuid()%></td>
    <td><%=stu.getStuname()%></td>
   <td><%=stu.getGender()%></td>
   </tr>
   <%} %>
   </table>
  </body>
</html>