主界面上的模糊查询:以下就不全部上代码了,上的是关键点
<form action="admin.jsp">
<input type="text" name="str">
<input type="submit" value="查询" id="a1">
</form>
//表单提交的跳转界面仍然是 此 主界面
<ul class="classlist">
<%
//获得文本框的值
String str=request.getParameter("str");
//灵活使用sql语句 默认str为空,查询的是全部
String sql="select * from newsb";
//当文本框有值输入时,则更改sql语句
if(str!=null){
sql="select * from newsb where newstitle like '%"+str+"%'";
}
//和往前一样的一系列操作。
Class.forName("oracle.jdbc.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="add-look.jsp?nid=<%=rs.getInt(1)%>">
<%=rs.getString(3) %>
</a>
<span>作者:
<%=rs.getString(4) %>
    
<a href='update_news.jsp?newid=<%=rs.getInt(1) %>'>修改</a>     
<a href='dodel.jsp?newid=<%=rs.getInt(1) %>' onclick='return clickdel()'>删除</a> </span> </li>
<li class='space'></li>
<%} %>
修改界面的赋值:主要点:定义其字段,判断循环有值,则给其赋值 以下有不同组件赋值的方式
<h1 id="opt_type"> 修改新闻: </h1>
<%
String nid = request.getParameter("newid");
//把字符串转换为整数编号
int newid =Integer.valueOf(nid);
Class.forName("oracle.jdbc.OracleDriver");
//连接数据库
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
//创建对象
PreparedStatement ps = con.prepareStatement("select * from newsb where newsid="+newid);
ResultSet rs=ps.executeQuery();
//定义
int neid=0;
String ntitle="";
String nzz="";
String nzy="";
String nnr="";
if(rs.next()){
neid=rs.getInt("newsid");
nzz=rs.getString("newstitle");
nzy=rs.getString("newszy");
nnr=rs.getString("newscontent");
}
%>
<form action="doupdatejsp.jsp?newid=<%=newid %>" method="post">
<p>
<label> 主题 </label>
<select name="ntid">
<option value="<%=rs.getInt(1)%>"><%=rs.getString(3) %></option>
</select>
</p>
<p>
<label> 标题 </label>
<input name="ntitle" value="<%=ntitle %>" type="text" class="opt_input" />
//输入框value="<%=ntitle %>
</p>
<p>
<label> 作者 </label>
<input name="nauthor" value="<%=nzz %>" type="text" class="opt_input" />
//输入框value="<%=nzz %>"
</p>
<p>
<label> 摘要 </label>
<textarea name="nsummary" cols="40" rows="3">
//文本域
<%=nzy %>
</textarea>
</p>
<p>
<label> 内容 </label>
<textarea name="ncontent" cols="70" rows="10">
//文本域
<%=nnr %>
</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>
咱就举一个 主题修改的栗子吧,增删改 都类似就不来了。
<h1 id="opt_type"> 修改主题: </h1>
<form action="doupd_sub.jsp?" method="post">
主题: <select name="ntid">
<%
Class.forName("oracle.jdbc.OracleDriver");
//连接数据库
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
Connection con = DriverManager.getConnection(url, "scott", "tiger");
//创建对象
PreparedStatement ps = con.prepareStatement("select * from newsfl");
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
<option value="<%=rs.getInt(1)%>"><%=rs.getString(2) %></option>
<%} %>
</select>
<p>
改为:
<input name="nauthor" type="text" class="opt_sub">
</p>
<input name="action" type="hidden" value="addnews">
<input type="submit" value="提交" class="opt_sub" />
<input type="reset" value="重置" class="opt_sub" />
</form>
帮助修改主题:
<%
String nid = request.getParameter("ntid");
//把字符串转换为整数编号
int newsid =Integer.valueOf(nid);
String nname = request.getParameter("nauthor");
//连接数据库
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 newsfl set newstname=? where newstid="+newsid);
ps.setString(1, nname);
//执行sql语句
int i = ps.executeUpdate();
ResultSet rs= ps.executeQuery();
if(i>0){
out.print("<script>alert('修改成功');location.href='admin.jsp'</script>");
}else{
out.print("<script>alert('修改失败');location.href='upd_sub_subs.jsp'</script>");
}
%>
好啦,今天的内容就是以上部分,一起加油,一起进步!