从数据库读出来的数据如何保存到本地? 我想把 findAll()方法查出来的数据保存到本地e盘。public class DB { private static String URL_STR = "jdbc:oracle:thin:@10.1.2.32:1521:ld";
private static String DIV_STR = "oracle.jdbc.driver.OracleDriver";
private static String USER_STR = "shop88";
private static String PASS_STR = "shop88mj"; Connection conn;
Statement stm;
ResultSet rs;
public  Connection getconn(){
try {
Class.forName(DIV_STR).newInstance();
try {
conn=DriverManager.getConnection(URL_STR,USER_STR, PASS_STR);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}


//查找

public List<CustBean> findAll(){

List<CustBean> list = new ArrayList<CustBean>();
try {
stm = getconn().createStatement();
rs = stm.executeQuery("select CUST_ID,CUST_NM,CITI_NO,ADDR_1 from tb_ac001 where rownum<4");
CustBean cust = new CustBean();
while(rs.next()){
cust.setCUST_ID(rs.getString("CUST_ID"));
cust.setCUST_NM(rs.getString("CUST_NM"));
cust.setCITI_NO(rs.getString("CITI_NO"));
cust.setADDR_1(rs.getString("ADDR_1"));
list.add(cust);
// System.out.print(cust.getCUST_ID());
// System.out.print(cust.getCUST_NM());
// System.out.print(cust.getADDR_1());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

解决方案 »

  1.   

    import   java.sql.*;                                       /*   JDBC   Classes   */   
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
      //   import   COM.cloudscape.core.*;                 /*   Cloudscape   JDBC   classes   */   
      import   org.w3c.dom.*;                                 /*   W3C   Interfaces   */   import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
    import com.sun.org.apache.xml.internal.serialize.OutputFormat;
    import com.sun.org.apache.xml.internal.serialize.XMLSerializer;import java.io.*;                                         /*   Java   io   classes   for   file   access   */   
        
    /*
     * 从数据库读出数据保存到c盘
     */
      public   class   DBc_test   {   
      

    private static String URL_STR = "jdbc:oracle:thin:@10.1.2.32:1521:ld";
    private static String DIV_STR = "oracle.jdbc.driver.OracleDriver";
    private static String USER_STR = "shop88";
    private static String PASS_STR = "shop88mj";
         public   static   final   String   OUTPUTFILE ="c:\\customer.xml";   
         Document xmlDoc;
    static Connection conn;
    static Statement stm;
    static ResultSet rs;
    public  Connection getconn(){
    try {
    Class.forName(DIV_STR).newInstance();
    try {
    conn=DriverManager.getConnection(URL_STR,USER_STR, PASS_STR);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return conn;
    }    
    public List<CustBean> findAll(){

    List<CustBean> list = new ArrayList<CustBean>();
    try {
    stm = getconn().createStatement();
    rs = stm.executeQuery("select CUST_ID,CUST_NM,CITI_NO,ADDR_1 from tb_ac001 where rownum<4");
    CustBean cust = new CustBean();
    while(rs.next()){
    cust.setCUST_ID(rs.getString("CUST_ID"));
    cust.setCUST_NM(rs.getString("CUST_NM"));
    cust.setCITI_NO(rs.getString("CITI_NO"));
    cust.setADDR_1(rs.getString("ADDR_1"));



    try {
    xmlDoc = buildCustomerXML(rs);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      File outputFile=new File(OUTPUTFILE);  
    try {
    printDOM(xmlDoc,outputFile);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();


    list.add(cust);
    // System.out.print(cust.getCUST_ID());
    // System.out.print(cust.getCUST_NM());
    // System.out.print(cust.getADDR_1());
    }


     
                 
                  
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return list;
    }
        
          public   static   void   main(String   args[])   {   
        
          }         
     private   static   Document   buildCustomerXML(ResultSet   _customerRS)     
          throws   Exception   {   
    Document   xmlDoc   =   new   DocumentImpl();   

    /*   Creating   the   root   element   */   
    Element   rootElement   =   xmlDoc.createElement("CUSTOMERS");   
    xmlDoc.appendChild(rootElement);   

    while   (_customerRS.next())   {   
    Element   customer   =   xmlDoc.createElement("CUSTOMER");   

    /*   Building   the   id   attribute   for   the   DOM   */   
    customer.setAttribute("customerid",   _customerRS.getString("CUST_ID"));   

    /*   Creating   the   elements   within   my   customer   DOM   */   

    Element   CUST_NM   =   xmlDoc.createElement("CUST_NM");   
    Element   CITI_NO   =   xmlDoc.createElement("CITI_NO");  
    Element   ADDR_1   =   xmlDoc.createElement("ADDR_1");  

    /*   Populating   my   customer   DOM   with   data   */   


    CUST_NM.appendChild(xmlDoc.createTextNode(   
            _customerRS.getString("CUST_NM")));   
    CITI_NO.appendChild(xmlDoc.createTextNode(_customerRS.getString("CITI_NO")));
    ADDR_1.appendChild(xmlDoc.createTextNode(_customerRS.getString("ADDR_1")));

    /*   
    *   Appending   the   customer   elements   to   the   customer   element   declared   at   
    *   the   beginning   of   the   while   loop.   
    */   

    customer.appendChild(CUST_NM);   
    customer.appendChild(CITI_NO); 
    customer.appendChild(ADDR_1); 

    /*   Appending   the   customer   to   the   root   class   */   
    rootElement.appendChild(customer);   
    }     

    return   xmlDoc;   
    }     
    private   static   void   printDOM(Document   _xmlDoc,     
                    File   _outputFile)   throws   Exception   {   
    OutputFormat   outputFormat   =   new   OutputFormat("XML",   "UTF-8",   true);   
    FileWriter   fileWriter   =   new   FileWriter(_outputFile);   

    XMLSerializer   xmlSerializer   =   new   XMLSerializer(fileWriter,     
                                                              outputFormat);   
    xmlSerializer.asDOMSerializer();   

    xmlSerializer.serialize(_xmlDoc.getDocumentElement());   
    }     
           
      }