H ello, i have an xml code, i need to save it into mysql 5.5 server database using java. i also want to be able to retrieve it as well. please note that i want it saved as xml and retrieve it the same way i saved it. SOMEONE PLEASE HELP.... this is my xml code
<Details> <title>Name</title> <addr>Address</addr> <age>Age</age> </Details> <Details> <title>Name</title> <addr>Address</addr> <age>Age</age> </Details>
import java.io.*; import java.sql.*; import org.w3c.dom.*; import javax.xml.parsers.*; public class InsertXMLData public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse (new File("C:/roseindia.xml")); doc.getDocumentElement ().normalize (); System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName()); NodeList listOfPersons = doc.getElementsByTagName("employee"); for(int s=0; s<listOfPersons.getLength() ; s++){ Node firstPersonNode = listOfPersons.item(s); if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){ Element firstPersonElement = (Element)firstPersonNode; NodeList firstNameList = firstPersonElement.getElementsByTagName("name"); Element firstNameElement = (Element)firstNameList.item(0); NodeList textFNList = firstNameElement.getChildNodes(); String name= ((Node)textFNList.item(0)).getNodeValue().trim(); NodeList lastNameList = firstPersonElement.getElementsByTagName("address"); Element lastNameElement = (Element)lastNameList.item(0); NodeList textLNList = lastNameElement.getChildNodes(); String address= ((Node)textLNList.item(0)).getNodeValue().trim(); int i=st.executeUpdate("insert into data(name,address) values('"+name+"','"+address+"')"); } } out.println("Data is successfully inserted!"); }catch (Exception err) { System.out.println(" " + err.getMessage ()); } }
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("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_register","root","root";); 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(); } } }
thank you so much for your response, unfortunately it still didnt work. what do you think is the problem?