<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.sql.*"%>
<%
	Connection conn = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try
	{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		conn = DriverManager.getConnection("jdbc:odbc:Northwind");
		
		String employeeID = request.getParameter("eid");
		String firstName = request.getParameter("FirstName");
		String lastName = request.getParameter("LastName");
		String title = request.getParameter("Title");
		String extension = request.getParameter("Extension");
	
		if (request.getParameter("submitted") != null)
		{	
			String sqlUpdate = "UPDATE Employees SET FirstName=?, ";
			sqlUpdate += "LastName=?, ";
			sqlUpdate += "Title=?, ";
			sqlUpdate += "Extension=? ";
			sqlUpdate += " WHERE EmployeeID = ?";
			stmt = conn.prepareStatement(sqlUpdate);
			
			stmt.setString(1, firstName);
			stmt.setString(2, lastName);
			stmt.setString(3, title);
			stmt.setString(4, extension);
			stmt.setString(5, employeeID);

			stmt.executeUpdate();
		}
	
		String sqlSel = "SELECT FirstName, LastName, Title, Extension FROM Employees WHERE EmployeeID = ?";
		stmt = conn.prepareStatement(sqlSel);
		stmt.setString(1, employeeID);
		rs = stmt.executeQuery();
		rs.next();
%>
<html>
<head>
<title>Edit Employee</title>
</head>

<body>
<%
	if (request.getParameter("submitted") != null)
	{ 
		out.write("<b>Changes Made</b>");
	}
%>
<form method="post" action="EditEmployee.jsp?eid=<%=employeeID%>">
<input type="hidden" name="submitted" value="true"/>
<table>
<tr>
	<td>First Name:</td>
	<td><input type="text" name="FirstName" value="<%=rs.getString("FirstName")%>"/></td>
</tr>
<tr>
	<td>Last Name:</td>
	<td><input type="text" name="LastName" value="<%=rs.getString("LastName")%>"/></td>
</tr>
<tr>
	<td>Title:</td>
	<td><input type="text" name="Title" value="<%=rs.getString("Title")%>"/></td>
</tr>
<tr>
	<td>Extension:</td>
	<td><input type="text" name="Extension" value="<%=rs.getString("Extension")%>"/></td>
</tr>
<tr>
	<td colspan="2" align="right"><input type="submit" value="Submit Changes"/></td>
</tr>
</table>
</form>
<a href="EmployeeAdmin.jsp">Employee List</a>
</body>
</html>
<%
	}
	catch (Exception e)
	{
		out.write("Failed: <br/> " + e.toString());
	}
	finally 
	{
		if (rs != null) rs.close();
		if (stmt != null) stmt.close();
		if (conn != null) conn.close();
	}
%>