Java绿地(~ming~)

Java 草地

常用链接

统计

最新评论

JSP添加查询和删除模块

添加信息项目流程



1. 
javaBean:

a.Stu.java: public class Stu {private String name,sex,myclass; private int age;

public void setName(String nm){name=nm;} public String getName(){return name;  }

 

b.DBContext.java: import java.sql.*;import sun.jdbc.odbc.*; public class DBContext {

public static final DBContext getInstance=new DBContext ();   //静态可共享连接

    public Connection getConnection(){    try{   Class.forName("Driver");

    Connection conn=DriverManager.getConnection(url,user,password);

    System.out.println("连接成功!"); return conn; } //getConnection要返回个Conn

    catch(Exception e){e.printStackTrace();return null;}}} 

 

c.DBBean.java:   import java.sql.*;   public class DBBean {

    Connection conn=null;   ResultSet rs=null;

    public ResultSet executeQuery(String sql){    rs=null;

        try{  conn=DBContext.getInstance.getConnection();

              Statement stmt=conn.createStatement(); rs=stmt.executeQuery(sql); }

        catch(SQLException ex){ex.getMessage();} return rs; }//代替方法体里面return

   

public int executeUpdate(String sql){

        try{    conn=DBContext.getInstance.getConnection();

            Statement stmt=conn.createStatement();int i=stmt.executeUpdate(sql);

            if(i>0)     System.out.println("update success!:"+i);

            else    System.out.println("update error!");    return i;  }

         catch(Exception ex){ ex.printStackTrace(); return -1;  }   }

    public void closeConnection(){

        try{    if(!conn.isClosed()) conn.close();}//检索connection对象是否被关闭

        catch(Exception ex){    ex.printStackTrace();}  }

 

进行测试public static void main(String[] args){DBBean dbo=new DBBean();

             ResultSet rs=dbo.executeQuery("select *from mytalbe");

             try{    while(rs.next()){if(rs.getString("name")!=null){

                System.out.println(rs.getString("name").toString());}}  }

             catch(Exception ex){    ex.printStackTrace();}}}

 

2.  添加信息块:insertStu.jsp:<%@ include file="stuManager.jsp"%>

添加测试:<jsp:useBean id="stu" scope="page" class="Stu"/>

<jsp:setProperty name="stu" property="*"/>

<jsp:useBean id="DBbean" scope="page" class=" DBBean"/>

<%String name=stu.getName();String sex=stu.getSex();int  age=stu.getAge();

String myclass=stu.getMyclass();

String sql="insert into mytable(name,sex,age,myclass)

values("+"'"+name+"',"+"'"+sex+"',"+"'"+age+"',"+"'"+myclass+"'"+")";

int i=DBbean.executeUpdate(sql);    if(i>0){    out.println("insert success!"); }

else{       out.println("insert error!");   }%>

 

3.查询信息块(表头如图)<jsp:useBean id="DBbean" scope="page" class=" DBBean"/>

<%  ResultSet rs=DBbean.executeQuery("select * from mytable");   %>

<%  while(rs.next()){ %>  <tr>

    <td height="23"><div align="center"><%= rs.getString("name") %></div></td>

    <td><div align="center"><%= rs.getString("sex") %></div></td>

    <td><div align="center"><%= rs.getString("age") %></div></td>

    <td><div align="center"><%= rs.getString("myclass") %></div></td>

    <td><div align="center"><a href="delete.jsp?

id=<%= rs.getInt("id") %>">delete</a></div></td>

    <td><div align="center"><a href="update.jsp?

id=<%= rs.getInt("id") %>">update</a></div></td>

    </tr>  <% } %>

 

4.  删除信息块:<jsp:useBean id="DBbean" scope="page" class=" DBBean"/>

<% String n=request.getParameter("id"); //selectStuhref(id)

      String sql="delete from mytable where id='"+n+"'";

      int i=DBbean.executeUpdate(sql);

    if(i>0) { response.sendRedirect("selectStu.jsp");} else {out.print("失败!");} %>

 

5.更新信息块(表单如图):<jsp:useBean id="DBbean" scope="page" class=" DBBean"/>

<% String n=request.getParameter("id");//selectStuhref(id)

      ResultSet rs=DBbean.executeQuery("select *from mytable where id='"+n+"'");

      if(rs!=null) rs.next(); %>//返回到第一个记录之上,所以一定要取next的值

测试更新(提交id)<jsp:useBean id="DBbean" scope="page" class=" DBBean"/>

<%   String name=stu.getName();String sex=stu.getSex();  int  age=stu.getAge();

        String myid=request.getParameter("id");String myclass=stu.getMyclass();

        String sql="update mytable set name='"+name+"',sex='"+sex+"',

age='"+age+"',myclass='"+myclass+"' where id="+myid;

    int i=DBbean.executeUpdate(sql);

    if(i>0){    out.println("更新成功!");response.sendRedirect("selectStu.jsp");    }

    else {  out.print("error!");}   %>

posted on 2007-06-29 11:42 mrklmxy 阅读(1245) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: