读书人

关于servlet的链表操作现在只剩下一

发布时间: 2012-01-08 22:48:50 作者: rapoo

关于servlet的链表操作,现在只剩下一个将链表存入mysql的函数(saveNode())有毛病了,实在想不出来,大家帮看看,先谢谢了!

Java code
import java.util.Scanner;import java.io.*; import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;class Node {    public int BookStore;                 //图书库存量    public String BookNo;                //图书编号    public String BookName;          //图书名称    public int BookLend;                    //图书上架数量    public String BookPublishing;//图书出版社    public Node Next;                       //指向下一个Node对象的对象引用      public Node(){//只有一个节点      BookNo="000000";      BookName="书名未初始化";      BookLend=0;      BookStore=0;      BookPublishing="出版社未初始化";      Next = null;  }  public Node(Node eNext,Node t){//不止一个节点      BookNo=t.BookNo;      BookName=t.BookName;      BookLend=t.BookLend;      BookStore=t.BookStore;      BookPublishing=t.BookPublishing;      Next = eNext;   }  public void setData(int eBookStore,int eBookLend,String eBookNo,String eBookName,String eBookPublishing){      //设置当前结点的值      BookNo = eBookNo;      BookName = eBookName;      BookPublishing = eBookPublishing;      BookLend = eBookLend;      BookStore = eBookStore;     }  public Node getData() {//获取当前节点的值      Node rNode =new Node();      rNode.BookNo=BookNo;      rNode.BookName=BookName;      rNode.BookPublishing=BookPublishing;      rNode.BookLend=BookLend;      rNode.BookStore=BookStore;      rNode.Next=Next;      return rNode;  }  public void setNext(Node dNext){ //设置当前节点的指针      Next = dNext;   } //修改指针  public Node getNext() {  //获取当前节点的指针      return Next;   } //获得指向的对象引用}class LinkList {private Node firstNode;private Node headNode=firstNode;String htm="";public LinkList()    {        firstNode = null;}//建立空链表    public String visitAllNode(){//遍历表中每个结点,并串成一个字符串Node nextNode = firstNode; String s = "<table><tr><td>书号</td><td>书名</td><td>库存量</td><td>上架数量</td><td>出版社</td></tr>";  while (nextNode != null)    {       Node number = nextNode.getData();      s = s +"<tr>";      s = s +"<td>"+number.BookNo + "</td>";      s = s +"<td>"+number.BookName + "</td>";      s = s +"<td>"+number.BookStore + "</td>";      s = s +"<td>"+number.BookLend + "</td>";      s = s +"<td>"+number.BookPublishing + "</td></tr>";                nextNode = nextNode.getNext();  }s+="</table>";return s;   }public String vN(){//遍历表中每个结点,并串成一个字符串Node nextNode = firstNode; String s = "<table><tr><td>书号</td><td>书名</td><td>库存量</td><td>上架数量</td><td>出版社</td></tr>";if(nextNode==null){    s="没有可供删除的结点!";}else{while (nextNode != null)  {     Node number = nextNode.getData();    s = s +"<tr>";    s = s +"<td>"+number.BookNo + "</td>";    s = s +"<td>"+number.BookName + "</td>";    s = s +"<td>"+number.BookStore + "</td>";    s = s +"<td>"+number.BookLend + "</td>";    s = s +"<td>"+number.BookPublishing + "</td>";    s = s +"<td><a href=\"DemoLink?menu=1&no="+number.BookNo+"\" target=\"_self\">删 除</a></td></tr>";              nextNode = nextNode.getNext();}s+="</table>";}return s;   }public void insertAtEnd(Node tt) //在链表后插入结点{    firstNode = new Node(firstNode,tt);}public void deleteAtID(String data)//删除指定的节点{    int flag=0;    Node tempNode = firstNode;    Node headNode = tempNode;    if(firstNode == null)        htm="这是一个空链表,没有可供删除的结点!";    else     {        if(firstNode.BookNo.equals(data))        {            firstNode=firstNode.getNext();        }        else        {        while(firstNode!=null)        {            if(firstNode.BookNo.equals(data))                {                        tempNode.setNext(firstNode.getNext());                        firstNode=headNode;                    flag=1;                    htm="<script language=\"javascript\"> self.location.href=\"DemoLink?menu=4\"</script>";                    break;                }                else                { tempNode=firstNode;                    firstNode=firstNode.getNext();                    }            }                if(flag ==0 )                {                    htm="链表中没有你要删除的节点";                }        }    }}public void saveNode(){ String driver = "com.mysql.jdbc.Driver";                        // 驱动程序名 String url = "jdbc:mysql://localhost/book";            // URL指向要访问的数据库名scutcs String user = "root";                                                            // MySQL配置时的用户名     String password = "6868932";                                                // MySQL配置时的密码 String sql = ""; try {   Class.forName(driver);                                                        // 加载驱动程序  Connection conn = DriverManager.getConnection(url, user, password);// 连续数据库      Statement statement = conn.createStatement();            // statement用来执行SQL语句     if(firstNode == null)        htm="这是一个空链表,没有可供存储的结点!";    else     {        while(firstNode!=null)        {                    sql="insert into book(BookNo,BookName,BookLend,BookStore,BookPublishing) values(firstNode.BookNo,firstNode.BookName,first.BookLend,first.BookStore,first.BookPublishing)";        statement.executeUpdate(sql);            firstNode=firstNode.getNext();        }  } }    catch(ClassNotFoundException e) {  htm= e.toString();    }catch(SQLException e) {        htm= e.toString();       }catch(Exception e) {         htm= e.toString();     }}      public LinkList readNode(){Node h=new Node();    LinkList listNull = new LinkList();    h=firstNode; String driver = "com.mysql.jdbc.Driver";                        // 驱动程序名 String url = "jdbc:mysql://localhost/book";            // URL指向要访问的数据库名scutcs String user = "root";                                                            // MySQL配置时的用户名     String password = "6868932";                                                // MySQL配置时的密码 Node t1=new Node(); t1.Next=null; try {   Class.forName(driver);                                                        // 加载驱动程序  Connection conn = DriverManager.getConnection(url, user, password);// 连续数据库  //if(!conn.isClosed())   //        return htm="Succeeded connecting to the Database!";  Statement statement = conn.createStatement();            // statement用来执行SQL语句  String sql = "select * from book";                                // 要执行的SQL语句  ResultSet rs = statement.executeQuery(sql);                // 结果集  String BookNo = null;  String BookName=null;  String BookPublishing=null;  while(rs.next()) {   t1.BookNo = rs.getString("BookNo");     // 数据库编码为utf-8时的输出方式   // 数据库未使用utf-8时的输出方式:首先使用ISO-8859-1字符集将选定字段解码为字节序列并将结果存储新的字节数组中。   // 然后使用utf-8字符集解码指定的字节数组   //BookNo = new String(rs.getString("BookNo").getBytes("ISO-8859-1"),"utf-8");     t1.BookName = rs.getString("BookName");     //BookName = new String(rs.getString("BookName").getBytes("ISO-8859-1"),"utf-8");     t1.BookPublishing = rs.getString("BookPublishing");     //BookPublishing = new String(rs.getString("BookPublishing").getBytes("ISO-8859-1"),"utf-8");     t1.BookLend = rs.getInt("BookLend");     t1.BookStore= rs.getInt("BookStore");   listNull.insertAtEnd(t1);  }    //htm=listNull.showNode();  rs.close();  conn.close();    return listNull; } catch(ClassNotFoundException e) {  htm= e.toString();    }catch(SQLException e) {        htm= e.toString();       }catch(Exception e) {         htm= e.toString();     }     return listNull;}public String showNode(){    if(firstNode == null)        return htm="这是一个空链表!";            else      return htm=visitAllNode();        }}public class DemoLink extends HttpServlet{    String htm="";    LinkList listNull = new LinkList();    LinkList list = new LinkList();    public static void main(String args[]){            }    public void doPost(HttpServletRequest req,HttpServletResponse resp)    throws ServletException,IOException    {        resp.setContentType("text/html;charset=utf-8");        Node tt=new Node();        String     BN=new String(req.getParameter("BookNo").getBytes("ISO-8859-1"),"utf-8");        String     B =new String(req.getParameter("BookName").getBytes("ISO-8859-1"),"utf-8");        String     BP=new String(req.getParameter("BookPublishing").getBytes("ISO-8859-1"),"utf-8");        int            L    =Integer.valueOf(req.getParameter("BookLend")).intValue();        int            S    =Integer.valueOf(req.getParameter("BookStore")).intValue();        tt.BookNo=BN;        tt.BookName=B;        tt.BookPublishing=BP;        tt.BookLend=L;        tt.BookStore=S;        listNull.insertAtEnd(tt);        resp.sendRedirect("DemoLink?menu=3");    }    public void doGet(HttpServletRequest req,HttpServletResponse resp)        throws ServletException,IOException    {        String menu=req.getParameter("menu");        resp.setContentType("text/html;charset=utf-8");        int xz=Integer.valueOf(menu).intValue();        switch(xz){            case 1:                String delno=req.getParameter("no");                listNull.deleteAtID(delno);                htm=listNull.showNode();                break;            case 2:                htm=listNull.showNode();                break;            case 3:                htm="<table><form method=\"post\" action=\"DemoLink\">"+                            "<tr><td>图书编号:</td><td><input name=\"BookNo\" type=\"text\" /></td></tr>"+                            "<tr><td>图书名称:</td><td><input name=\"BookName\" type=\"text\" /></td></tr>"+                            "<tr><td>上架数量:</td><td><input name=\"BookLend\" type=\"text\" /></td></tr>"+                            "<tr><td>库 存 量:</td><td><input name=\"BookStore\" type=\"text\" /></td></tr>"+                            "<tr><td>出 版 社:</td><td><input name=\"BookPublishing\" type=\"text\" /></td></tr>"+                            "<tr><td><input type=\"submit\" value=\"确 定\" />  <input value=\"重 置\" type=\"reset\" />"+                        "</form></table>";                break;            case 4:                htm=listNull.vN();                break;            case 5:                listNull.saveNode();                //listNull=listNull.readNode();                htm=listNull.showNode();                break;            case 6:                listNull=listNull.readNode();                //list=listNull;                htm=listNull.showNode();                break;            default:                htm="参数调用不合法!";                break;    }        PrintWriter out=resp.getWriter();        out.println("<html>");        out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");          out.println("<head><title></title>");        out.println("<SCRIPT   LANGUAGE=\"JavaScript\"  type=\"text/javascript\" src=\"node.js\"></SCRIPT>");        out.println("</head><body>");        out.println(htm);        out.println("</body></html>");        out.close();    }} 



[解决办法]
那是 因为 你根本 没调用 那个方法呀 0.0
case 5:
listNull.saveNode();
除了 这里有 , 但是 listNull 是 LinkList listNull = new LinkList(); 一个集合而已
所以就想问下 这里的 listNull.saveNode();
是否是 public void saveNode() , 是否确定?

读书人网 >Java Web开发

热点推荐