请问如何将下面这段程序修改成可供调用的类呀,本人比较菜,谢谢!
请问如何将下面这段程序修改成可供调用的类呀,本人比较菜,谢谢!
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.jdom.Document;
import org.jdom.adapters.XercesDOMAdapter;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.Element;
import org.jdom.JDOMException;
import java.util.List;
import java.util.Iterator;
import java.sql.*;
import com.microsoft.jdbc.sqlserver.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class fly1 {
class ConnSql {
public ConnSql()
{
}
}
private static DOMBuilder builder=null;
public static void main(String args[])throws IOException,FileNotFoundException{
XercesDOMAdapter xercAdapter = new XercesDOMAdapter();
org.w3c.dom.Document w3Dom = xercAdapter.getDocument(new FileInputStream(
"2.xml"), false);
builder=new DOMBuilder("org.jdom.adapters.XercesDOMAdapter");
Document doc=builder.build(w3Dom);
List childs7=doc.getRootElement().getChild("SQL").getChildren("sqlsent");
Iterator itr7=childs7.iterator();
Element child7=null;
while (itr7.hasNext()){
child7=(Element)itr7.next();
System.out.println(child7);
System.out.println(child7.getText());
System.out.println(child7.getAttributes());
}
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url11="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user = "sa";
String password = "sa";
// Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver//jiafenghua:1433;User=jiafenghua;Password=197822");
Connection conn = DriverManager.getConnection(url11,user,password);
Statement ss=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// List childs6=doc.getRootElement().getChild("printText").getChildren("textMessage");
// Iterator itr6=childs6.iterator();
// Element child6=(Element)itr6.next();
String sql=child7.getText();//child6.getText();
//}catch(Exception e){}
// Statement ss= conn.prepareStatement(sql);
ResultSet result = ss.executeQuery(sql);
while(result.next())
{ String s= result.getString("LastName");
//System.out.println(result.getString("LastName"));
// System.out.println(result.getString("FirstName"));
// doc.getRootElement().getChild("Table")
// .addContent(new Element("RowData"))
// .addContent(new Element("FieldValue").setText(s));
Element ele = new Element("RowData");
ele.addChild( new Element("FieldValue").setText(s) );
doc.getRootElement().getChild("Table").addContent( ele );
}
result.close();
ss.close();
conn.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
// doc.getRootElement().getChild("Table")
// .addContent(new Element("RowData"))
// .addContent(new Element("FieldValue").setText(s));
// .setText(s);
String indent = " "; // 缩进符号
boolean newLines = true; // 是否产生新行
XMLOutputter outp = new XMLOutputter (indent,newLines,"gb2312"); // 构造新的输出流
outp.output(doc, new FileOutputStream("7.xml")); // 输出到2.XML文件中
}
}