By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,265 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,265 IT Pros & Developers. It's quick & easy.

how to store XML Document in MS SQL using JAVA

P: n/a
hi all
i am trying to store the XML Document object into MS SQL2005
i want to store this as whole not different tables;
plz help me regarding this i am trying form last two weeks i asked many
people but no use.
below is my program In that i want to store Document document object
into database which as var image/blob/clob plz help me


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import java.util.*;
import java.io.*;
import java.sql.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.Character;
/*class input implements Serializable
{
byte[] toByteArray()
{
ByteArrayOutputStream ob=new ByteArrayOutputStream(100);
try
{
ObjectOutputStream oob=new ObjectOutputStream(ob);
oob.writeObject(this);
}catch(Exception e)
{System.out.println("HeartBeat to Byte Conversion Error");}

if(len<0)
{
byte b[];
b=ob.toByteArray();
len=b.length;
return b;
}
else return ob.toByteArray();
}
}*/
class TestXml implements Serializable
{
public static String getFileData(byte [] data) {
System.out.println("\nIN GET FILE DATA");
String tmpString=null;
for (int i = 0; i < data.length; i++)
{
char c = Character.toChars(data[i])[0];
tmpString += c;
System.out.print(c);
}
return tmpString;
}
public static void main(String[] args) throws SQLException,IOException

{
// input in=new input();
org.w3c.dom.Document document;
Document doc;
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument(); // Create from whole cloth
doc = builder.newDocument();
Element root =
(Element) document.createElement("rootElement");
document.appendChild(root);
Element Child1 =
(Element) document.createElement("Child1");
Element Child2 =
(Element) document.createElement("Child2");
root.appendChild( document.createTextNode("Some") );
root.appendChild( document.createTextNode(" ") );
root.appendChild( document.createTextNode("text") );

Child1.appendChild( document.createTextNode("Some") );
Child1.appendChild( document.createTextNode(" ") );
Child1.appendChild( document.createTextNode("text") );
Child2.appendChild( document.createTextNode("Some") );
Child2.appendChild( document.createTextNode(" ") );
Child2.appendChild( document.createTextNode("text") );
root.appendChild(Child1);
root.appendChild(Child2);
// System.out.println(JDBCUtil.serialize(document));
Foo f=new Foo();
ConnectionObject con=new ConnectionObject();
Connection co=con.GetConnection();
Statement st = co.createStatement();
st.executeUpdate("Insert into XML1
values('"+document.getDocumentElement()+"');");
String str="Nataraju";
System.out.println(str.getBytes());
Statement st1 = co.createStatement();
ResultSet rs=st1.executeQuery("select * from XML1;");
Element root1;
while(rs.next())
{
// BufferedReader br=new BufferedReader(rs.getAsciiStream(1));
/* byte b1[]=(rs.getAsciiStream(1)).getBytes();
ByteArrayInputStream b=new ByteArrayInputStream(b1);
int c;
while((c=b.read())!=-1)
{
System.out.print((char)c);
b.reset();
}*/
byte [] data = rs.getBytes(1);
String Str=getFileData(data);
System.out.println(Str);

}

co.close();

}
catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();

}
}
}

May 3 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.