
Hi Guys, I want to retrieve the xml document stored in the database on to a jsp page using jdbc/odbc connnection.Please help me in implementing this feature.
Thank You Madhu

import java.io.*;
import java.sql.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class JavaXmlDataBase{
public static void main(String args[]) throws IOException {
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
Document doc = docBuilder.newDocument();
new XmlServlet().createXmlTree(doc);
System.out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}
}
public void createXmlTree(Document doc) throws Exception {
//This method creates an element node
Element root = doc.createElement("Company");
//adding a node after the last child node of the specified node.
doc.appendChild(root);
Element child = doc.createElement("Location");
root.appendChild(child);
Element child1 = doc.createElement("Companyname");
child.appendChild(child1);
Text text = doc.createTextNode("Roseindia.Net");
child1.appendChild(text);
Comment comment = doc.createComment("Employee in roseindia");
child.appendChild(comment);
Connection con = null;
int count = 0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:college");
try{
String sql = "SELECT * FROM employee_details";
PreparedStatement prest = con.prepareStatement(sql);
ResultSet rs = prest.executeQuery();
Element element=null;
Text text1=null;
while (rs.next()){
String name = rs.getString(2) + " " + rs.getString(3);
element = doc.createElement("Employee");
child.appendChild(element);
text1 = doc.createTextNode(name);
element.appendChild(text1);
count++;
}
prest.close();
con.close();
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.