新闻(project)-- 修改(update)

首先我们来回顾一些小知识

一.页面跳转

1.在html当中,实现页面跳转有两种方式

1)用a标签来实现页面跳转

 out.print("<a href='login.jsp'>点我返回登录界面</a>);//不要忘了 out.print是可以输出html语句的

2)用JS语句来实现页面跳转 

Location.href='xxxx';//JS中的跳转

out.print("<script>location.href'home.jsp'</script>);

2.在Java当中,实现页面跳转也有两种方式 

1)  转发  

request.getRequestDispatcher("home.jsp").forward(request, response);
注意:home.jsp是需要跳转的页面

2)重定向 

 response.sendRedirect("home.jsp");
注意:home.jsp是需要跳转的页面

二.如果乱码了怎么办 

request.setCharacterEncoding("utf-8");

三. 传值与接收值

 request.getParameter

四. 强转获取到ID

Integer.valueOf 


 五.Because增删改查的代码相类似,So我今天以修改为例

首先从主页面获取id跳转到修改页面:

<a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a> 

update_sub(将网页界面文本框的值传给doupdate_sub)

<body>
	<div id="header">
		<div id="welcome">欢迎使用新闻管理系统!</div>
		<div id="nav">
			<div id="logo">
				<img src="../images/logo.jpg" alt="新闻中国" />
			</div>
			<div id="a_b01">
				<img src="../images/a_b01.gif" alt="" />
			</div>
		</div>
	</div>
	<div id="admin_bar">
		<div id="status">
			管理员: 登录 &#160;&#160;&#160;&#160;<a href="#">login out</a>
		</div>
		<div id="channel"></div>
	</div>
	<div id="main">
		<div id="opt_list">
			<ul>
				<li><a href="newspages/add_new.jsp">添加新闻</a></li>
				<li><a href="WEB-INF/admin.jsp">编辑新闻</a></li>
				<li><a href="#">查找新闻</a></li>
				<li><a href="newspages/add_sub.jsp">添加主题</a></li>
				<li><a href="#">编辑主题</a></li>
			</ul>
		</div>
		<div id="opt_area">
			<h1 id="opt_type">修改主题:</h1>
			<form action="doupda_sub.jsp" method="post">
				主题:<select name="tid">
				<%
			  	Class.forName("oracle.jdbc.driver.OracleDriver");
			  	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
			  	Connection con = DriverManager.getConnection(url, "scott", "tiger");
			  	PreparedStatement ps=con.prepareStatement("select* from subject");
			  	ResultSet rs=ps.executeQuery();
			  	while(rs.next()){
			  		
				%>
					<option value="<%=rs.getInt(1) %>"><%=rs.getString(2) %></option>
				<%} %>
				<option>主题</option>
				</select>
				<p>
					<label> 改为: </label> 
					<input name="tname" type="text" class="opt_input" />
				</p>
				<input name="action" type="hidden" value="addnews"> <input
					type="submit" value="提交" class="opt_sub" /> <input type="reset"
					value="重置" class="opt_sub" />
			</form>
		</div>
	</div>
</body>

 doupdate_sub(接收update_sub中的值)

<%
    //获取主题的编号
	int tid=Integer.valueOf(request.getParameter("tid"));
    //获取主题名
	String tname=request.getParameter("tname");
    //注册驱动类
	Class.forName("oracle.jdbc.driver.OracleDriver");
     //连接oracle数据库
  	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  	Connection con = DriverManager.getConnection(url, "scott", "tiger");
         //创建个对象来执行sql语句
  	PreparedStatement ps=con.prepareStatement("update subject set tname=? where tid="+tid);
  	ps.setString(1, tname);
  	int i=ps.executeUpdate();
    //处理结果集
  	if(i>0){
  		out.print("<script>alert('成功成功!!');location.href='update_sub.jsp'</script>");
  	}else{
  		out.print("<script>alert('修改失败!!');location.href='updasub.jsp'</script>");
  	}
%>


一开始的时候,总觉得来日方长,什么都有机会,殊不知人生是减法,见一面少一面,来日不方长。大家加油

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

隔竹观尘世

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

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

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

打赏作者

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

抵扣说明:

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

余额充值