一:首先先讲一下会用到哪些基础且重要的知识点、以及一些容易被忽略的一些小问题:

1.简单描述一下jsp的数据绑定,连接数据库拿到值还是一样的七步走:

a.注册驱动类

Class.forName("oracle.jdbc.driver.OracleDriver");

注意:红色字体部分是OracleDriver导包出来的。

b.连接数据库:

String url = "jdbc:oracle:thin:@localhost:1521:库名";
Connection con = DriverManager.getConnection(url, "用户名或管理员名", "密码");

c.创建PreparedStatement对象
    PreparedStatement ps = con.prepareStatement("sql语句");
d. 给占位符赋值
    ps.setString(第几个占位符, 字段名);

e. 执行sql语句

     增删改:int i=ps.executeUpdate();
   查: ResultSet rs = ps.executeQuery();

注意:后面接的可能是取值如:

int tid=rs.getInt(数据库表内字段的位置);

String title=rs.getString(数据库表内字段的位置);

f.处理结果

注意:跳转的两种方法
 

   if(rs.next()){
        //转发
        request.getRequestDispatcher("页面一.jsp").forward(request, response);
    }else{
        //失败
        out.print("<script>alert('用户名或密码错误,请重新登录');location.href='页面二.jsp'</script>");
    }

g.关闭连接

if(con!=null){

   con.close();

 }

if(ps!=null){

   ps.close();

 }

if(rs!=null){

   rs.close();

 }

这些呢是最基本的代码,还会有一些形式上的转变,友友们要学会随机应变哈!

然后我们结合上节课的页面就可以,把数据库取到的值赋值给对应的文本框、文本域等。


2,然后就是对数据的增加删除

与前面其实差不多,不同的是他会有页面的传递数据

所以小重点来了:

a.<form>表单传值

<form action="dologin.jsp">
        用户名:<input type="text" name="uname"><br>
        密码:<input type="password" name="upwd"><br>
        <input type="submit" value="登录">     
    </form>

这里呢,它会通过submit 将数据提交到 dologin页面

然后在dologin页面用 request.getParameter();接收如:

//接收login页面传递的数据
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");

b.href 传值:

<script language="javascript">
	function clickdel(){
		return confirm("删除请点击确认");
	}
	
</script>
<a href='dodelnews.jsp?nid=<%=rs.getInt(1) %>' onclick='return clickdel()'>删除</a>

注意:这里呢确认修改才会跳转页面

假设我们连接到了数据库,就可以用此法便捷的将如: nid传到 dodelnews页面,

另一边也是用request.getParameter();接收。


最后看看部分功能完善的代码:

主页面拿到数据

<%
    		//连接数据库,查询新闻信息
    		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		Class.forName("oracle.jdbc.OracleDriver");
		Connection con=DriverManager.getConnection(url, "scott", "123");
		PreparedStatement ps=con.prepareStatement("select * from news");
		ResultSet rs=ps.executeQuery();
		while(rs.next()){
    	%>
    	
        <li>
        <a href='newspages/read_news.jsp?nid=<%=rs.getInt(1)%>'>
        <%=rs.getString("ntitle") %>
        </a>
         <span> 作者:
        <%=rs.getString("nzz") %>                                             
        &#160;&#160;&#160;&#160; <a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a>&#160;&#160;&#160;&#160; 
         <a href='dodelnews.jsp?nid=<%=rs.getInt(1) %>' onclick='return clickdel()'>删除</a> </span> </li>
      
      <%} %>

到删除页面:

<%
	//接收要删除的新闻编号
	String id=request.getParameter("nid");
	int nid=Integer.valueOf(id);
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	Class.forName("oracle.jdbc.OracleDriver");
	Connection con=DriverManager.getConnection(url, "scott", "123");
	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>");
	}
			
	
%>>

或者到添加页面:

 <h1 id="opt_type"> 添加新闻: </h1>
    <form action="doadd_news.jsp" method="post">
      <p>
        <label> 主题 </label>
        <select name="ntid">
          <option value="1">选择</option>
          <%
         String url = "jdbc:oracle:thin:@localhost:1521:orcl";
      	Class.forName("oracle.jdbc.OracleDriver");
      	Connection con=DriverManager.getConnection(url, "scott", "123");
      	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>

到添加页面的内部操作页面:

<% 
    request.setCharacterEncoding("utf-8");
	//接收添加页面的编号
	//新闻主题编号
	int tid=Integer.valueOf(request.getParameter("ntid"));
	//新闻标题
	String ntitle=request.getParameter("ntitle");
	//新闻作者
	String nzz=request.getParameter("nauthor");
	//新闻内容
	String nnr=request.getParameter("ncontent");
	//新闻摘要
	String nzy=request.getParameter("nsummary");
	
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  	Class.forName("oracle.jdbc.OracleDriver");
  	Connection con=DriverManager.getConnection(url, "scott", "123");
  	//获取新闻下一个编号
  	PreparedStatement ps=con.prepareStatement("select max(nid) from news");
  	int nextNid=1;
  	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);
	//执行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'</script>");
	}
	
%>

今天的小知识就到这里咯,当然代码有问题或对它有疑问的可以联系我

下一篇见哈,┏(^0^)┛拜拜!

更多推荐

Jsp数据绑定(oracle数据库)、以及数据的删除and增加