-- 作者:jackeyzeng
-- 发布时间:8/2/2004 8:29:00 PM
-- help ! it is about JDOM
我在编译这个文件时候出错,请教高手指点。 C:\dev\b2b2c>javac CartBean.java CartBean.java:234: cannot find symbol symbol : method setTrimText(boolean) location: class org.jdom.output.XMLOutputter outputter.setTrimText(true); ^ Note: CartBean.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: CartBean.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error 源程序: package b2b2c; import java.util.Hashtable; import java.util.Vector; import java.util.Enumeration; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import java.io.*; import java.net.*; import java.sql.*; import java.lang.System; import javax.servlet.*; import javax.servlet.http.*; public class CartBean { Hashtable params=new Hashtable(); Vector items = new Vector(); private static final String DEFAULT_SAX_DRIVER_CLASS ="org.apache.xerces.parsers.SAXParser"; private String saxDriverClass; private SAXBuilder builder; private Document doc; private Element root; // 輸出訂單到這個文件 private String xmldoc = "/xml/order.xml"; private String orderNo; //... JDBC .... private Connection con; private PreparedStatement pstmt_update_stock_qty; private PreparedStatement pstmt_select_stock_qty; private PreparedStatement pstmt_insert_order; private PreparedStatement pstmt_insert_orderitem; //----------------------------------- public CartBean() { System.out.println("start CartBean."); } /* // for debug public static void main(String args[]) { CatalogBean catalog = new CatalogBean(); CartBean cart = new CartBean(); catalog.startParse(); cart.addItem( catalog.getItem(1) ); cart.addItem( catalog.getItem(2) ); cart.toXML(); } */ // 處理網頁傳遞的參數, 存到Hashtable中 public void processRequest(HttpServletRequest req) { Enumeration en = req.getParameterNames(); while (en.hasMoreElements()) { String varName = (String)en.nextElement(); String[] varValues = req.getParameterValues(varName); if (varValues.length > 1) { params.put(varName, varValues); } else { params.put(varName, varValues[0]); System.out.println("req:"+varName+"="+varValues[0]); } } } public String getParam(String key) { return (String)params.get(key); } public void setParam(String key, String value) { if (params.containsKey(key)) params.put(key, value); else System.out.println("no such key:"+key+" in param Hashtable"); } public String[] getParamArr(String key) { return (String[])params.get(key); } //............................................... //.... 起始CartBean的準備工作 public void init() { // 清除Cart資訊 items.clear(); } // 加入有輸入購買數量的項目到購物車 public void addItems(Vector items) { // items為商品目錄的所有項目 String qtyParamValue; //int qty; Item item; for (int i=0; i<items.size(); i++) { qtyParamValue = getParam("qty_"+ new Integer(i).toString() ); if (qtyParamValue!=null && !qtyParamValue.equals("")) { item= (Item)items.get(i); item.setQty(qtyParamValue); // String addItem(item); } } } // 加入項目到購物車Vector public void addItem(Item item) { items.add(item); // modify warehouse database stock table if (con == null) { createConnection(); } modifyStock(item.getId(), Integer.parseInt(item.getQty())); System.out.println("item:"+item.getItemname()); } // 取得購物車內的所有項目 public Vector getItems() { return items; } // 取得購物車內的單一項目 public Item getItem(int index) { return (Item)items.get(index); } // 取得購物車內的項目數 public int getCartSize() { return items.size(); } // 取得訂單編號 public String getOrderno() { return orderNo; } //.... JDBC ...... // 修改庫存資料庫 public void modifyStock(String id, int qty) { try { int oldQty = 0; pstmt_select_stock_qty.setString(1, id); ResultSet rs = pstmt_select_stock_qty.executeQuery(); if (rs.next()) { oldQty = rs.getInt(1); System.out.println("old QTY="+oldQty); } int newQty = oldQty - qty; pstmt_update_stock_qty.setInt(1, newQty); pstmt_update_stock_qty.setString(2, id); pstmt_update_stock_qty.executeUpdate(); rs.close(); } catch (SQLException ex) { System.out.println(ex.toString()); } catch (Exception ex) { System.out.println(ex.toString()); } } // 修改訂單資料庫 public void modifyOrder(String name, String address) { try { pstmt_insert_order.setString(1, name); pstmt_insert_order.setString(2, address); pstmt_insert_order.setString(3, orderNo); pstmt_insert_order.executeUpdate(); } catch (SQLException ex) { System.out.println(ex.toString()); } catch (Exception ex) { System.out.println(ex.toString()); } } // 修改訂單項目資料庫 public void modifyOrderItem(Item item) { try { pstmt_insert_orderitem.setString(1, item.getId()); pstmt_insert_orderitem.setInt(2, Integer.parseInt(item.getQty())); pstmt_insert_orderitem.setString(3, orderNo); pstmt_insert_orderitem.executeUpdate(); } catch (SQLException ex) { System.out.println(ex.toString()); } catch (Exception ex) { System.out.println(ex.toString()); } } // 建立資料庫連結及prepared statement public void createConnection() { try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (Exception E) { System.err.println("Unable to load driver."); E.printStackTrace(); } try { con = DriverManager.getConnection("jdbc:mysql://localhost/warehouse?user=growbal&password=javaxml"); pstmt_update_stock_qty = con.prepareStatement("update stock set qty=? where id=?"); pstmt_select_stock_qty = con.prepareStatement("select qty from stock where id=?"); pstmt_insert_order = con.prepareStatement("insert into customorder (name, address, orderno) values (?,?,?)"); pstmt_insert_orderitem = con.prepareStatement("insert into customorderitem (id, qty, orderno) values (?,?,?)"); } catch (SQLException ex) { System.out.println(ex.toString()); } catch (Exception ex) { System.out.println(ex.toString()); } } // 輸出訂單XML到檔案/xml/order.xml, 並關閉資料庫 public void close() throws IOException, JDOMException { doc.setRootElement(root); startOutputXML(new FileOutputStream(xmldoc)); //.... close database ..... try { pstmt_update_stock_qty.close(); pstmt_select_stock_qty.close(); con.close(); } catch (SQLException ex) { System.out.println(ex.toString()); } catch (Exception ex) { System.out.println(ex.toString()); } } private void startOutputXML(OutputStream out) throws IOException, JDOMException { // Create an outputter with default formatting XMLOutputter outputter = getXMLOutputter(); outputter.output(doc, out); } private XMLOutputter getXMLOutputter() throws IOException, JDOMException { // Create an outputter with certain encoding XMLOutputter outputter = new XMLOutputter(" ", true, "Big5"); outputter.setTrimText(true); outputter.setExpandEmptyElements(true); return outputter; } // 增加項目到XML文件 private void addToXML(Item item) { // add one element to XML file Element curItem = new Element("item"); Element curItemname = new Element("itemname"); curItemname.addContent(item.getItemname()); Element curId = new Element("id"); curId.addContent(item.getId()); Element curQty = new Element("qty"); curQty.addContent(item.getQty()); curItem.addContent(curItemname); curItem.addContent(curId); curItem.addContent(curQty); root.addContent(curItem); } // 輸出訂單到XML文件, 並修改資料庫 public void toXML(String name, String address) { this.saxDriverClass = DEFAULT_SAX_DRIVER_CLASS; builder = new SAXBuilder(saxDriverClass); Item item; // build new XML doc root = new Element("order"); doc = new Document(root); // add timestamp as order's serial number Element elemOrderNo = new Element("orderno"); orderNo = String.valueOf( java.lang.System.currentTimeMillis() ); elemOrderNo.addContent( orderNo ); root.addContent(elemOrderNo); //System.out.println("add elements to root"); for (int i=0; i<getCartSize(); i++) { item = (Item)getItem(i); System.out.println("add one item: "+item.getItemname()); // save to XML file addToXML(item); // modify customorderitem table modifyOrderItem(item); } //..... modify customorder table modifyOrder(name, address); //System.out.println("add ok!"); try { close(); } catch (JDOMException e) { System.out.println(e.toString()); } catch (IOException e) { System.out.println(e.toString()); } } } classpath设置如下: .;c:\jdk1.5.0\lib;c:\jdk1.5.0\lib\tools.jar;c:\jdk1.5.0\lib\dt.jar; c:\jdk1.5.0\jre\lib\dt.jar;C:\dev\sax2\sax2.jar; C:\jdom-b9\build\jdom.jar; C:\dev\soap-2_3_1\lib\soap.jar;C:\dev\xerces-1_2_0\xerces.jar; C:\dev\xerces-1_2_0\xercesSamples.jar;C:\Tomcat 5.0\common\lib\servlet-api.jar; C:\Tomcat 5.0\bin;C:\dev\xt\sax.jar;C:\dev\xt\xt.jar; C:\dev\jaxp parser\Jaxp1.0.1\parser.jar
|