HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA

HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA

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>

View Answers

March 5, 2012 at 10:46 AM

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 ());
        }
    }

March 5, 2012 at 10:56 AM

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();
}
}
}

March 6, 2012 at 12:34 AM

thank you so much for your response, unfortunately it still didnt work. what do you think is the problem?









Related Tutorials/Questions & Answers:
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA
HOW TO SAVE XML INTO MYSQL AND RETRIEVE IT USING JAVA  H ello, i have an xml code, i need to save it into mysql 5.5 server database using java. i... as xml and retrieve it the same way i saved it. SOMEONE PLEASE HELP.... this is my
how to retrieve image from mysql using java - Java Beginners
how to retrieve image from mysql using java  hi i am using jsf and java IDE netbean,database mysql.how to retrieve image from mysql database. please help me   Hi friend, I am sending you a link. I hope
Advertisements
how to insert and retrieve an image from mysql using java - Java Beginners
how to insert and retrieve an image from mysql using java  how to insert and retrieve an image from mysql using java?  Hi friend, Code to insert image using java : import java.sql.*; import java.io.*; class
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve image from mysql database using java and show it in HTML img tag ?  how to retrieve image from mysql database using java and show it in HTML img tag
How To Retrieve Image From From MySQL Using Java
How To Retrieve Image From From MySQL Using Java In this section we will discuss about how to retrieve image from the MySQL using Java. This example explains you about all the steps that how to retrieve image from MySQL database
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java  How To Store... and then retrieve them in image gallery using java
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
How many ways we can retrieve the date in result set of mysql using php?
How many ways we can retrieve the date in result set of mysql using php?  How many ways we can retrieve the date in result set of mysql using php
how to retrieve text and images from mysql database and show on html page using jsp servlet
how to retrieve text and images from mysql database and show on html page using jsp servlet  <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Insert title here h3
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver
How to Retrieve Excel data into mysql using type 2 JDBC-ODBC driver  Can any please help it is very urgent. I tried to read excel sheet using ODBC.... so, can any one help me without creating DNS we should retrieve data from excel
How to retrieve image from mysql database in JSP?
How to retrieve image from mysql database in JSP?  Hi, I need JSP same codes for learning to get image which is stored in MySQL Database. How to retrieve image from mysql database in JSP? Thanks   Hi, You can write
retrieve related data from database using jsp and mysql
retrieve related data from database using jsp and mysql  Hi sir, please give some example of jsp code for retrieving mysql database values in multiple dropdown list. if we change a value in a dropdown its related value must
Retrieve The Data From MySql Datbase
Retrieve The Data From MySql Datbase   How to Retrieve The Data From MYSQL database TO Use Select the Emp_id Option.And Also Search Option
Retrieve image from mysql database through jsp
Retrieve image from mysql database through jsp... to retrieve image from mysql database through jsp code. First create a database.... Before running this java code you need mysql connector jar file
retrieve data from mysql database
retrieve data from mysql database  hi am not familiar in php.....even... selected value on combobox which is to be retrieve the relevant data from mysql database using php.... below my code is that.. <html> <head>
Data retrieve from mysql database
Data retrieve from mysql database  Hi sir, please give some example of jsp code for retrieving mysql database values in multiple dropdown list... text field using struts and hibernate. Regards Subrat   The given
HTML & MYSQL - retrieve record from table
HTML & MYSQL - retrieve record from table  Hi. I have a field...,trichy,kanchipuram for a single record. I have to retrieve these data from the field... it in html. how to do
MYSQL retrieve record from Data table
MYSQL retrieve record from Data table  Hi. I have a field in database...,kanchipuram for a single record. I have to retrieve these data from the field... it in html. how to do
retrieve data from mysql database and store it in a variable ?
retrieve data from mysql database and store it in a variable ?  sir... which is stored in mysql. so to apply some arithmetic operation on it we have to store that data in int variable. how to do this ?   Here is an example
Retrieve Image in JSF From MySQL Table Using NetBeans IDE
to retrieve image and data from MySQL table using NetBeans IDE... .style1 { margin-right: 0px; } Retrieve Image in JSF From MySQL Table Using NetBeans IDE     
Retrieve date from MYSQL database
Retrieve date from MYSQL database In this tutorial, you will learn how to retrieve date from database. Storing and Retrieving dates from the database is a common task. MYSQL provides different ways of inserting and fetching dates from
Mysql Date To Java Date
Mysql Date To Java Date       Mysql Date To Java Date retrieve the records from Mysql and display... an example from 'Mysql Date To Java Date'. To understand and grasp the example
Connecting to MySQL database and retrieving and displaying data in JSP page
; This tutorial shows you how to connect to MySQL database and retrieve the data... Connecting to MySQL database and retrieving and displaying data in JSP page... web application. Creating Table in the database.ADS_TO_REPLACE_1 Using a JDBC
GET DATE in JDBC
GET DATE in JDBC    This example shows how to retrieve date from existing table of MySql database. Date functions can...;String url = "jdbc:mysql://lacalhost:3306/"; 
Mysql Date Today
Mysql Date Today       Mysql Date Today show you Query to retrieve the current today date. ... the current today date in Mysql. Query for finding the current date:-ADS
Execute SQL Queries with Java Application
values and retrieve values from the table. Before running this java code you... Execute SQL Queries with Java Application       This is detailed java program to connect java
Mysql Date Parse
Mysql Date Parse       Mysql Date is used to parse the string date time in Date Format function and retrieve the date in specified format. Understand with ExampleADS_TO_REPLACE_1
Mysql Like Operator
Mysql Like Operator       Mysql Like is used to retrieve the records on the basis... you to understand an  example from 'Mysql Like Operator'.To grasp
Mysql Like Operator
Mysql Like Operator       Mysql Like is used to retrieve the records on the basis of pattern matching... an  example from 'Mysql Like Operator'.To grasp this exammple we create
MySQL Select Statement
MySQL Select Statement       In this lesson you will be learn how to use SELECT statement in MySQL and you can also learn how to use SELECT statement with WHERE
Mysql like syntax
Mysql like syntax       Mysql Like is used to retrieve the records on the basis... you to understand an  example from 'Mysql Like Operator'.To grasp
Mysql like syntax
Mysql like syntax       Mysql Like is used to retrieve the records on the basis of pattern matching... an  example from 'Mysql Like Operator'.To grasp this exammple we create
Mysql Date
Mysql Date       Mysql Date retrieve the date with the help of timestamp. Timestamp is used... The Tutorial explains you 'Mysql Date'. To understand this example, we use Date(Current
Create table and insert data by sql query
      This is detailed java program to connect java application and execute sql query like create table in mysql... this java code you need mysql-connector-java-3.1.6-bin.jar file in the jdk1.6.0_01
PDO Fetch Object
, accessing fields as object. By using this  method we get the result set...;; tryADS_TO_REPLACE_3 { $dbh=new PDO("mysql:host=$hostname;dbname... by using print_r or var_dump, as follows: Example:ADS_TO_REPLACE_9 <?php
PDO Fetch Execute
PDO Fetch Mode: We have studied before in the earlier tutorials that how to connect with a database and how to fetch data from the tables, in many times we... $dbh=new PDO("mysql:host=$hostname;dbname=db",$user,$password
PDO Fetch Num
("mysql:host=$hostname;dbname=ajax",$user,$password); $sql="
PDO Fetch Both
PDO("mysql:host=$hostname;dbname=ajax",$user,$password); //echo "
PDO Fetch Lazy
_TO_REPLACE_3 { $dbh=new PDO("mysql:host=$hostname;dbname=ajax",$user
PDO Fetch Class
PDO Fetch Class: Using PDO::FETCH_CLASS we can map the fields of a table as properties (fields/variables) of a class. With the help of this method we can...;'; try{ADS_TO_REPLACE_5 $dbh=new PDO("mysql:host=$host;dbname
PDO Fetch Into
;; $obj = new Test();ADS_TO_REPLACE_7 $db=new PDO("mysql:host=$host;dbname
PDO Prepared Statement
statements and how to use it using PDO. Sometimes it is more commodious..., using this we can reduce the execution time. The prepared statement can...;mysql:host=$host;dbname=$db",$user,$password); $dbh->setAttribute(PDO
PDO Error Handling
;; $db='DB'; try{ADS_TO_REPLACE_3 $dbh=new PDO("mysql:$host
Mysql Date Values
Mysql Date Values       Sometimes we want to retrieve the Date Values. In this Mysql query we provide you an example demonstration with complete code description that describe you
image retrieve - Java Beginners
Image Retrieve  How to retrieve the image from mysql field using java

Ads