接着上次的网页登陆之后,把跳转路径更改之后会显示数据的增删改查
此页面的数据来自Oracle数据库,自己建的一共三个,暂时用了两个,以下是数据库的添加数据
select * from subject;
select * from news;
--给新闻分类表添加7条数据
insert into subject values(1,'社会');
insert into subject values(2,'娱乐');
insert into subject values(3,'军事');
insert into subject values(4,'实事');
insert into subject values(5,'政治');
insert into subject values(6,'国内');
insert into subject values(7,'国际');
--给新闻表添加50条数据
declare
tid number:=1;
begin
for i in 1..50 loop
insert into news values(i,tid,'感人,真经'||i,'哇噻'||i,'这是一篇感人又震惊的新闻'||i,sysdate,0,'罗海妹又震惊了');
tid:=tid+1;
if tid>7 then
tid:=1;
end if;
end loop;
end;
commit;
点击左上角的添加新闻会到一个添加界面,让我们看看代码
添加数据
request.setCharacterEncoding("utf-8");
//接受添加数据
//新闻编号
int tid = Integer.valueOf(request.getParameter("ntid"));
//新闻标题
String ntitle = request.getParameter("ntitle");
//新闻作者
String nzz = request.getParameter("nauthor");
//新闻摘要
String nzy = request.getParameter("nsummary");
//新闻内容
String nnr = request.getParameter("ncontent");
//连接数据库
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "scott");
//获取下一个新闻的编号
int nextNid = 1;
PreparedStatement ps = con.prepareStatement("select max(nid) from news");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
nextNid = rs.getInt(1) + 1;
}
ps = con.prepareStatement("insert into news values(?,?,?,?,?,sysdate,0,?)");
ps.setInt(1, nextNid);
ps.setInt(2, tid);
ps.setString(3, ntitle);
ps.setString(4, nzz);
ps.setString(5, nnr);
ps.setString(6, nzy);
//执行
int i = ps.executeUpdate();
if (i > 0) {
out.print("<script>alert('添加成功');location.href='admin.jsp'</script>");
} else {
out.print("<script>alert('添加失败');location.href='add_news.jsp'</script>");
}
绑定数据
<%
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "scott");
PreparedStatement ps = con.prepareStatement("select * from subject");
ResultSet rs = ps.executeQuery();
while(rs.next()){
%>
<option value='<%=rs.getInt(1) %>'> <%=rs.getString(2) %> </option>
<%} %>
删除数据
//删除的新闻编号
String id = request.getParameter("nid");
int nid = Integer.valueOf(id);
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "scott");
PreparedStatement ps = con.prepareStatement("delete news where nid="+nid);
int i = ps.executeUpdate();
if(i>0){
out.print("<script>alert('删除成功');location.href='admin.jsp'</script>");
}else{
out.print("<script>alert('删除失败');location.href='admin.jsp'</script>");
}
修改数据
request.setCharacterEncoding("utf-8");
int nid=Integer.valueOf(request.getParameter("nid"));
int tid=Integer.valueOf(request.getParameter("ntid"));
String ntitle=request.getParameter("ntitle");
String nzz=request.getParameter("nauthor");
String nzy=request.getParameter("nsummary");
String nnr=request.getParameter("ncontent");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "scott");
int nextNid=1;
PreparedStatement ps=con.prepareStatement("select max(nid) from news");
ResultSet rs=ps.executeQuery();
while(rs.next()){
nextNid=rs.getInt(1)+1;
}
ps=con.prepareStatement("update news set ntitle=?,nrr=?,nzy=? where nid="+nid);
ps.setString(1, ntitle);
ps.setString(2, nnr);
ps.setString(3, nzy);
int i=ps.executeUpdate();
if(i>0){
out.print("<script>alert(修改成功');location.href='admin.jsp'</script>");
}else{
out.print("<script>alert('修改失败');location.href='update_news.jsp'</script>");
}