数据库技术A实验四--使用网页添加一条记录

说明:新建数据库技术A实验四+动态网页项目,新增insertDept.jsp,deptView.jsp和insertDept.java.并修改相关代码片段的参数至符合自己项目结构的要求。

insertDept.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增-单位表</title>
</head>
<body>
	<form action="/数据库技术A实验四+/insertDept.do" method="post" class="form-horizontal">
			编号:
			<input id="deptID" class="form-control" placeholder="Text input" name="deptID" type="text" />
			名称:
			<input id="majorName" name="deptName" type="text"/>
			<input id="btnInsert" class="btn btn-danger" type="submit" value="插入" />
		 </form>
</body>
</html>

deptView.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>study2单位表jsp</title>
</head>
<body>
SQLserver2019 study2 单位表:
<table border = "1">
<tr>
<td>单位编号</td>
<td>单位名称</td>
<td>操作</td>
</tr>
<%
String url = "jdbc:sqlserver://localhost:10086;DatabaseName=study2";
Connection con = null;
Statement stmt=null;
String query="SELECT * FROM 单位表$";
try{
	//加载jdbc驱动程序
	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	//建立连接
	con=DriverManager.getConnection(url,"sa1","202005295");
	//创建Statement语句
	stmt=con.createStatement();
	//执行SQL语句
	ResultSet rs=stmt.executeQuery(query);
	while (rs.next()){
		//通过循环语句检索结果
		String deptId = rs.getString(1);
		String deptName = rs.getString(2);
		out.println("<tr><td>"+deptId+"</td><td>"+deptName+"</td><td><a href='"+"http://localhost:8080/%E6%95%B0%E6%8D%AE%E5%BA%93%E6%8A%80%E6%9C%AFA%E5%AE%9E%E9%AA%8C%E5%9B%9B+/deleteDept.do?deptID="+ deptId+"'>删除</a></td></tr>");
	}
}catch (ClassNotFoundException e){
	out.print("类没有找到异常:");
	out.println(e.getMessage());
}catch (SQLException e){
	out.println("SQL异常:"+e.getMessage());
}finally{
	if (stmt != null){
		try{
			stmt.close();
		}catch (SQLException e){
		}
		stmt=null;
	}
	if (con != null){
		try{
			con.close();//关闭连接
		}catch (SQLException e){	
		}
		con = null;
	}
}
%>
</table>
</body>
</html>

insertDept.java:

package Servlet1;
import java.sql.*;
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;

/**
 * Servlet implementation class insertDept
 */
@WebServlet("/insertDept.do")
public class insertDept extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertDept() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		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);
		request.setCharacterEncoding("UTF-8");
		String driveName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		try {
			String deptID = request.getParameter("deptID");
			String deptName = request.getParameter("deptName");
			Class.forName(driveName);
			String url = "jdbc:sqlserver://localhost:10086;DatabaseName=study2";;
			Connection con = DriverManager.getConnection(url, "sa1","202005295");
			Statement statement = con.createStatement();
			String sql ="insert into 单位表$(单位编号,单位名称) values ('" + deptID + "','" + deptName + "')";
			Boolean flag = statement.execute(sql);
//			执行完成之后返回的网页地址(servlet或jsp网页)
			//网址内中文乱码解决:服务器默认接受方式字符编码与UTF-8编码格式互相转换
		    String UTF_8text1 = "数据库技术A实验四+";
	   //   String UTF_8text2 = "中文路径2";
	   //   String UTF_8text3 = "中文路径3";
		    response.sendRedirect("/"+new String(UTF_8text1.getBytes("utf-8"),"iso-8859-1")+"/Jsp/deptView.jsp");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

信管201张辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值