登录页面
登录页面代码(表单验证)
<form action="dologin.jsp" onsubmit="return login_yz()">
<div id="user">
用 户 <input type="text" id="uname" name="uname" />
</div>
<div id="password">
密 码 <input type="password" id="upwd" name="upwd" />
</div>
<div id="yzm">
验证码 <input id="uyzm" style="width: 50px;" type="text" name="textfield3" />
<span id="syzm" onclick="yz()"></span>
</div>
<div id="btn">
<input type="submit" value="登录">
<input type="reset" value="清空">
</div>
</form>
JavaScript代码
<script type="text/javascript">
var str = "qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKLZXCVBNM23456789";
function yz() {
//随机出4个0-字符串长度之间的数字 做为下标
yzmStr="";
for(var i=1;i<=4;i++){
yzmStr+=str.substr(Math.round(Math.random()*(str.length-1)),1);
}
//把验证码赋值到页面
document.getElementById("syzm").innerHTML=yzmStr;
}
function $(id) {
return document.getElementById(id);
}
function login_yz() {
//验证 用户名 和 密码不能为空
//获取用户名
var uname = $("uname").value;
if(uname.length==0){
alert("用户名不能为空");
return false;
}
//获取密码
var upwd = $("upwd").value;
if(upwd.length==0){
alert("密码不能为空");
return false;
}
//获取验证码:判断和电脑的验证码是否相等
//PS:自己实现-输入的验证码不区分大小写-大小写转换
var yzm = $("uyzm").value;
if(yzm!=yzmStr){
alert("验证码输入错误,请重新输入");
//清空输入的验证码
$("uyzm").value="";
//重新生成验证码
yz();
return false;
}else if(yzm.toUpperCase()!=yzmstr){
alert("验证码输入错误,请重新输入");
yz();
return false;
}
}
</script>
给本页面绑定数据库(一定要记得导入驱动包)
1.注册驱动类: Class.forName("驱动代码块")
2.连接数据库:Connection con=DriverManager.getConnection("连接代码","数据库用户名","密码/口令");
3.创建preparedstatement对象,用来执行sql语句
PreparedStatement ps=con.preparedStatement("sql语句")
4.给占位符赋值
ps.set数据类型
5.执行sql语句
ResultSet rs=ps.executeQuery();---查询结果处理
ResultSet rs=ps.executeUpdate();---增删改结果处理
6.处理结果
while(rs.next()){
}
7.关闭连接
con.close()
ps.close();
rs.close();
新闻信息页面
登录后,跳转到信息页面(页面信息同样需要绑定数据库),然后对数据库里面的数据进行操作【增加新闻,删除新闻,修改新闻,查询新闻】
代码
<script language="javascript">
function clickdel(){
return confirm("删除请点击确认");
}
</script>
<form action="admin.jsp" align="center">
<input type="text" name="str">
<input type="submit" value="查询">
</form>
<ul class="classlist">
<%
String str=request.getParameter("str");
if(str==null){
str="";
}
String sql="select * from news where ntitle like '%"+str+"%' order by nid desc";
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(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
<li><a href="read_news.jsp?nid=<%=rs.getInt(1)%>"><%=rs.getString("ntitle") %></a><span> 作者:
<%=rs.getString("nzz") %>
     <a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a>     
<a href='dodel.jsp?nid=<%=rs.getInt(1) %>' onclick='return clickdel()'>删除</a> </span> </li>
<li class='space'></li>
<%} %>
<p align="right"> 当前页数:[1/3] <a href="#">下一页</a> <a href="#">末页</a> </p>
</ul>
</div>
查询新闻页面(绑定数据库,采用模糊查询的方法)
添加新闻页面(绑定数据库,执行sql添加语句)
添加新闻页面代码
<div id="opt_area">
<h1 id="opt_type"> 添加新闻: </h1>
<form action="doadd_news.jsp" method="post">
<p>
<label> 主题 </label>
<select name="ntid">
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "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>
<%}%>
</select>
</p>
<p>
<label> 标题 </label>
<input name="ntitle" type="text" class="opt_input" />
</p>
<p>
<label> 作者 </label>
<input name="nauthor" type="text" class="opt_input" />
</p>
<p>
<label> 摘要 </label>
<textarea name="nsummary" cols="40" rows="3"></textarea>
</p>
<p>
<label> 内容 </label>
<textarea name="ncontent" cols="70" rows="10"></textarea>
</p>
<p>
<label> 上传图片 </label>
<input name="file" type="file" 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>
添加新闻的代码块
<%
//设置请求的编码
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 ncontent = request.getParameter("ncontent");
//连接数据库
//注册驱动类
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
//获取新闻的下一个编号
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, ncontent);
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>");
}
%>
删除新闻页面(绑定数据库,执行sql删除语句)
<在新闻信息页面拼一个新闻编号[?新闻编号=<%=%>]>
删除新闻的代码
<%
int nid=Integer.valueOf(request.getParameter("nid"));
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
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>");
}
%>
修改新闻页面(连接数据库,执行sql修改语句)
【和添加新闻页面有雷同,但代码不同】
修改新闻的代码
<在新闻信息页面拼一个新闻编号[?新闻编号=<%=%>]>
<%
//乱码
request.setCharacterEncoding("utf-8");
//接受添加页面传过来的值
//新闻主题编号
int tid = Integer.valueOf( request.getParameter("ntid"));
int nid = Integer.valueOf( request.getParameter("nid"));
//新闻标题
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");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
PreparedStatement ps = con.prepareStatement("update news set tid=?,ntitle=?,nzz=?,nzy=?,ncontent=? where nid="+nid);
//给占位符赋值
ps.setInt(1, tid);
ps.setString(2, ntitle);
ps.setString(3, nzz);
ps.setString(4, nzy);
ps.setString(5, nnr);
//ResultSet rs = ps.executeQuery();
//执行sql语句
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?nid="+nid+"'</script>");
}
%>
添加主题页面(绑定数据库,执行sql语句添加语句)
添加主题的代码(表单验证)
往数据库中的主题表添加主题
<%
request.setCharacterEncoding("utf-8");
String sname = request.getParameter("nsub");
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
int nextTid=1;
PreparedStatement ps=con.prepareStatement("select max(tid) from subject");
ResultSet rs=ps.executeQuery();
while(rs.next()){
nextTid=rs.getInt(1)+1;
}
ps=con.prepareStatement("insert into subject values(?,?)");
ps.setInt(1, nextTid);
ps.setString(2, sname);
int i=ps.executeUpdate();
if(i>0){
out.print("<script>alert('添加成功');location.href='../admin.jsp'</script>");
}else{
out.print("<script>alert('添加成功');location.href=''doadd_sub.jsp'</script>");
}
%>
修改主题页面(绑定数据库,执行sql修改语句)
修改数据库中的主题表信息
修改页面的代码
<form action="doup_sub.jsp">
<p>
<label> 主题 </label>
<select name="tid">
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
//创建那个对象,用来执行sql语句
//查询到新闻主题的 最大编号
PreparedStatement ps = con.prepareStatement("select * from subject");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
%>
<option value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option>
<%
}
%>
</select>
</p>
<label>改为:</label>
<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>
修改主题的代码
<%
//设置 请求的编码
request.setCharacterEncoding("utf-8");
int tid = Integer.valueOf(request.getParameter("tid"));
String tname= request.getParameter("tname");
//连接数据库 进行修改
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接数据库
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "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='update_sub.jsp'</script>");
}
%>