四、程序设计题(10分)
设计一个插入单位表的页面insert.jsp以及提交后的插入处理程序insert.do,并使用插入语句插入相关信息。
Insert.jsp页面仅写下form表单中的相关程序,程序不需要对表单中的信息作检查,insert.do 程序仅填写doPost中的相关程序。
单位表的名字为dept,它有两列,deptID,类型为char(2),deptName,类型为nvarchar(50) 数据库服务器地址为:exam,端口号为:8888。数据库名为dept,用户名为:sa,密码为:123,数据库驱动程序全称为:com.microsoft.sqlserver.jdbc.SQLServerDriver
insert.jsp页面代码
<body> //请填写form表单中的程序
<form action =”insert.do” method=”post” >
<label>单位编号</label> <input type="text" name=”deptId “> <br>
<label>单位名称</label> <input type="text" name=”deptName”> <br>
<button type="submit"> 插入</button>
</form>
</body>
insert.do页
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
try {
//请填写下方的程序段来完成插入的程序
String deptId = request.getParameter("deptId");
String deptName = request.getParameter("deptName");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url ="jdbc:sqlserver://exam:8888;DatabaseName=dept";
Connection conn = DriverManager.getConnection(url,"sa","123");
Statement stmt = conn.createStatement();
String sql= " insert into dept values('"+deptId+"', '"+deptName+"' ) ";
Boolean flag= stmt.execute(sql);
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}