我在oracle数据库有一个表,有两个字段,id和xml_c 分别为varchar2和xmltype类型,请问怎么编写程序应用jdbc从数据库中通过id查询到xml_c并且把它输出到指定的文件内,比如:c:/xml_out.xml
我的源程序如下:
public void myclass
{
try
{
// register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
);
// EDIT IF NECESSARY
// create a Connection object, and connect to the database
// as store using the Oracle JDBC Thin driver
myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"scott",
"tiger"
);
// disable auto-commit mode
myConnection.setAutoCommit(false);
// create a Statement object
myStatement = myConnection.createStatement();
File file=new File("C:/xml_out.xml");
FileOutputStream fout=new FileOutputStream(file);
String mysql="select xml_c from myxml where id=?";
OraclePreparedStatement opst=(OraclePreparedStatement) myConnection.prepareStatement(mysql);
opst.setString(1,"20070622001");
OracleResultSet ors=(OracleResultSet)opst.executeQuery();
if(ors.next())
{
// get the XMLType
XMLType poxml = XMLType.createXML(ors.getOPAQUE(1));
// get the XMLDocument
Document podoc = (Document)poxml.getDOM();
// 这里有问题
((XmlDocument)podoc).write(fout);
//?????????????????????????????
}
ors.close();
System.out.println("xml文件已经读出到指定文件内!");
}
catch(Exception e)
{
System.out.println("error :"+e.toString() );
e.printStackTrace();
}
请高手修改好此程序,能够正确输出好吗?或者有更好的方法也行
我的源程序如下:
public void myclass
{
try
{
// register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
);
// EDIT IF NECESSARY
// create a Connection object, and connect to the database
// as store using the Oracle JDBC Thin driver
myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"scott",
"tiger"
);
// disable auto-commit mode
myConnection.setAutoCommit(false);
// create a Statement object
myStatement = myConnection.createStatement();
File file=new File("C:/xml_out.xml");
FileOutputStream fout=new FileOutputStream(file);
String mysql="select xml_c from myxml where id=?";
OraclePreparedStatement opst=(OraclePreparedStatement) myConnection.prepareStatement(mysql);
opst.setString(1,"20070622001");
OracleResultSet ors=(OracleResultSet)opst.executeQuery();
if(ors.next())
{
// get the XMLType
XMLType poxml = XMLType.createXML(ors.getOPAQUE(1));
// get the XMLDocument
Document podoc = (Document)poxml.getDOM();
// 这里有问题
((XmlDocument)podoc).write(fout);
//?????????????????????????????
}
ors.close();
System.out.println("xml文件已经读出到指定文件内!");
}
catch(Exception e)
{
System.out.println("error :"+e.toString() );
e.printStackTrace();
}
请高手修改好此程序,能够正确输出好吗?或者有更好的方法也行
解决方案 »
- udp socket问题
- 关于数据库数据导出csv文件,用制表符分割出现问题。请大家帮忙看看
- 基本数据类型和引用类型的在内存中的分配
- 同事离职,祝他一路顺风!
- 想知道如何和银行系统对接实现网上支付吗
- 我刚接触JAVA两个月,现在开始想做个小项目,请高手推荐
- 实现frame的水平分割与重直分割。。。看了给点评价。。。。。。。
- 求助:我想买一本有关JAVA标准类库的书,大家知道买哪本比较好?
- 感谢sharptop版主提供的webchart,给分了!请版主进入!
- 今天第一次用Jbuilder4,给我的感觉是太烂了,光标都无法正确定位,这是什么原因?希望高手赐教!
- 紧急求助,打印机纸张方向问题
- 请问如何获取输入的数字
Document document = DocumentHelper.createDocument();
Element root = document.addElement("dataset");Element element = root.addElement("row");
element.addElement("xml_c").setText(ors.getOPAQUE(1));
element.addElement("tempaccount").setText(account.getTempAccount()); // lets write to a file
OutputFormat format = new OutputFormat();
format.setEncoding("gb2312");
XMLWriter writer = new XMLWriter(new FileWriter(fileName), format);
writer.write(document);
writer.close();
第一步:先把数据查出来,放在一个List。
第二步:利用XML的API进行操作,把数据写进去,很简单。
写的时候
FileOutputStream fileoutputstream = new FileOutputStream(文件路径);
byte bytes[] = strout.getBytes();
fileoutputstream.write(bytes);
最好先把事务去掉,调试好了在加上.
at getxml.main(getxml.java:59)
哪位高手再指点一下!
{
try
{
// register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
);
// EDIT IF NECESSARY
// create a Connection object, and connect to the database
// as store using the Oracle JDBC Thin driver
myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"scott",
"tiger"
);
// disable auto-commit mode
myConnection.setAutoCommit(false);
// create a Statement object
myStatement = myConnection.createStatement();
File file=new File("C:/xml_out.xml");
FileOutputStream fout=new FileOutputStream(file);
String mysql="select xml_c from myxml where id=?";
OraclePreparedStatement opst=(OraclePreparedStatement) myConnection.prepareStatement(mysql);
opst.setString(1,"20070622001");
OracleResultSet ors=(OracleResultSet)opst.executeQuery();
if(ors.next())
{
// get the XMLType
XMLType poxml = XMLType.createXML(ors.getOPAQUE(1));
// get the XMLDocument
Document podoc = (Document)poxml.getDOM();
((XmlDocument)podoc).write(fout);
}
ors.close();
System.out.println("xml文件已经读出到指定文件内!");
}
catch(Exception e)
{
System.out.println("error :"+e.toString() );
e.printStackTrace();
}