javaWeb(绑定&删除&查看&修改)

一.绑定

<%
				//数据库交互oracleDriver
				//加载驱动
				Class.forName("oracle.jdbc.driver.OracleDriver");
				//建立连接
				Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","123");
				//sql
				String sql="select *from tb_t281_news";
				PreparedStatement ps=conn.prepareStatement(sql);
				//返回结果集对象
				ResultSet rs=ps.executeQuery();
				//遍历
				while(rs.next()){
					out.print("
  • "+rs.getString(3)+" 作者:"+rs.getString(4)+"     修改      删除
  • "); } %>

    二.删除

    String sql="DELETE TB_XW WHERE NID=?";
    PreparedStatement ps=con.prepareStatement(sql);
    
    String id=request.getParameter("id");
    	ps.setInt(1, Integer.parseInt(id));
    	 int n=ps.executeUpdate();
    	if(con!=null&&!con.isClosed()){
    		con.close();
    	}
    	if(ps!=null){
    		ps.close();
    	}
    	if(n>0){
    		request.getRequestDispatcher("xwfind.jsp").forward(request, response);
    	}else{
    		out.print("");
    	} 
    

    三.查看

    <%
    String URL="jdbc:oracle:thin:@localhost:1521:orcl";
    String CNAME="oracle.jdbc.driver.OracleDriver";
    Class.forName(CNAME);
    Connection con=DriverManager.getConnection(URL, "scott", "tiger");
    String sql="select * from tb_xw where nid=?";
    PreparedStatement ps=con.prepareStatement(sql);
    ps.setInt(1, Integer.parseInt(request.getParameter("id")));
    ResultSet rs=ps.executeQuery();
    String nname="";
    String addtime="";
    String nzz="";
    String nnr="";
    while(rs.next()){
    	nname=rs.getString(2);
    	addtime=rs.getString(7);
    	nzz=rs.getString(3);
    	nnr=rs.getString(5);
    }
    给文本框赋值使用<%=nnr %>
    %>
    

    四.修改

    String sql="UPDATE TB_XW SET NNAME=?,NZZ=?,NZY=?,NNR=?,TNAME=? WHERE NID=?";
    
    	PreparedStatement ps=con.prepareStatement(sql);
    	ps.setString(1,request.getParameter("nname"));
    	ps.setString(2,request.getParameter("nzz"));
    	ps.setString(3,request.getParameter("nzy"));
    	ps.setString(4,request.getParameter("nnr"));
    	ps.setString(5,request.getParameter("tname"));
    	ps.setInt(6, Integer.parseInt(request.getParameter("id")));
    	int n=ps.executeUpdate();
    	
    	if(con!=null&&!con.isClosed()){
    		con.close();
    	}
    	if(ps!=null){
    		ps.close();
    	}
    	
    	if(n>0){
    		out.print("");
    	}else{
    		out.print("");
    	}
    

    你可能感兴趣的:(java)