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());
}
}
}