java 从oracle取数据生成xml文件

import java.sql.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
class testdb1
{
    public static void main(String args[]) {
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (Exception e) {
    System.out.println("异常"+e.getMessage());
    System.exit(1);
    }

    if (args.length<7)
    {
        System.out.println("usage: java testdb1 [ip][sid][username][passwd][tablename][columname][djbh]");
        System.exit(1);
    }
    //定义数据库对象
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet rset = null;
    ResultSet rs = null;

        try {
            Scanner s=new Scanner(System.in);
          //  System.out.println("请输入用户名:");
            String username=args[2];
           // System.out.println("请输入密码:");
            String passwd=args[3];
           // System.out.println("请输入要生成的表名:");
            //String tablename=s.nextLine(); 
            String tablename=args[4];
            String columname=args[5];
            String file_name=args[6];
           // System.out.println("请输入字段名:");
          //  String columname=s.nextLine();
           // System.out.println("请输入你要生成的xml文件的单据编号:");
           // String file_name=s.nextLine();
            String xmlFile =file_name+".xml";
           
            //String sql=args[3];

      conn = DriverManager.getConnection("jdbc:oracle:thin:@"+args[0]+":1521:"+args[1], username,passwd);
      //get database metadata
      DatabaseMetaData dbmd = conn.getMetaData();
 

    try {
      /*          //文本创建
      if (!xmlFile.isFile())
      {
      xmlFile.createNewFile(); 
      }

     //以下生成的gb2312
      FileWriter fw = new FileWriter(xmlFile); 
      fw.write("<?xml version=\"1.0\" encoding=\"gb2312\"?>"+"\n"); 
      fw.write("<Document>"+"\n");
      fw.write("\t"+"<Events>"+"\n");
   fw.write("\t"+"\t"+"<Event Name=\"PurchaseWareHouseIn\" MainAction=\"WareHouseIn\">"+"\n");
      fw.write("\t"+"\t"+"<DataField>"+"\n");
      */
      //生成UTF-8文件
      // OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
      OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(xmlFile),"UTF-8");
      fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+"\n"); 
      fw.write("<Document>"+"\n");
      fw.write("\t"+"<Events>"+"\n");
   fw.write("\t"+"\t"+"<Event Name=\"PurchaseWareHouseIn\" MainAction=\"WareHouseIn\">"+"\n");
      fw.write("\t"+"\t"+"<DataField>"+"\n");
     
      stmt = conn.prepareStatement("select * from  "+tablename+" where "+columname+"=?") ;
      //corporderid
         //Execute the query to populate the ResultSet
      stmt.setString(1,file_name);
      rs = stmt.executeQuery();
         //stmt = conn.createStatement();
         // rs=stmt.executeQuery("select * from  "+tablename);
    //get resultset metadata     
        ResultSetMetaData rsData = rs.getMetaData(); 
        int columncount=rsData.getColumnCount();
    while (rs.next())
      {
      fw.write("\t"+"<data ");
   for (int i=1; i<columncount ;i++ )
    {
            String colName=rsData.getColumnName(i);
            String colVal=rs.getString(i);
            //System.out.println("<"+colName+">");
            fw.write(colName+"=\"");
            if (colVal != null && !colVal.equals("")) {
            fw.write(colVal);
             }
            else{            
            fw.write("");
            };
            fw.write("\" ");
      }  
       fw.write("></data>"+"\n");
      }
      fw.write("\n"+"\t"+"\t"+"</DataField>"+"\n");
      fw.write("\t"+"\t"+"</Event>"+"\n");  
      fw.write("\t"+"</Events>"+"\n");
      fw.write("</Document>"+"\n");
 
     fw.flush(); 
     fw.close();
     rs.close();
                  } catch (Exception e) { 
          e.printStackTrace(); 
       } 
    }
catch (SQLException e) {
      System.out.println("Darn! A SQL error: " + e.getMessage());
    }

    }
}

posted on 2013-09-24 22:43 snowhill 阅读(330) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜