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?

Ads









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 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
Advertisements
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 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       In this section we will see how to retrieve image from mysql database through jsp code. First create a database
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... 'Mysql Date To Java Date'. To understand and grasp the example we create a table
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. ... date in Mysql. Query for finding the current date:- mysql>
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 Example The Tutorial
Mysql Like Operator
Mysql Like Operator       Mysql Like is used to retrieve the records on the basis... to understand an  example from 'Mysql Like Operator'.To grasp this exammple we create
Mysql Like Operator
Mysql Like Operator       Mysql Like is used to retrieve the records on the basis of pattern matching... from 'Mysql Like Operator'.To grasp this exammple we create a table'employee1
Mysql like syntax
Mysql like syntax       Mysql Like is used to retrieve the records on the basis... an  example from 'Mysql Like Operator'.To grasp this exammple we create
Mysql like syntax
Mysql like syntax       Mysql Like is used to retrieve the records on the basis of pattern matching... from 'Mysql Like Operator'.To grasp this exammple we create a table'employee1
Mysql Date
Mysql Date       Mysql Date retrieve the date with the help of timestamp. Timestamp is used... 'Mysql Date'. To understand this example, we use Date(Current_timestamp) query
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
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 in mysql
Image in mysql  Hi. How to insert and retrieve images in mysql db using JSP or JAVA Servlet? Thanks in advance
how to retrieve data from database
how to retrieve data from database  unable to retrieve data from database using mySQL by using jsp sessions and beans for editing
select query
select query  how to retrieve a single image from mysql table using $row[] value
how to store and retrieve images int and from the database using php
how to store and retrieve images int and from the database using php  please anyone can help me in storing the images and retrieve it back from the Mysql database using php
mysql jdbc connectivity
mysql jdbc connectivity  i want to connect retrieve data from mysql using jdbc
Getting the last inserted id using jsp
Getting the last inserted id using jsp  Pls how can i retrieve the last inserted id after submitting a form to mysql using jsp
Java
Java  How to retrieve image from MySQL database using java?   Java Swing retrieve image: import java.sql.*; import java.awt.*; import...: Servlet retrieve image Java retrieve image
need help....how to connect and disconnect multiple databases(databases created in mysql) using java and my sql
in mysql) using java and my sql  i am working on a project on deadlock in distributed transactions , and in that i am using my sql in java i need to know that how to query multiple databases(databases created in my mysql) so
question
question  i need a jsp and mysql program to retrieve data from employee table and attendance table using user name and password at a time
Image retrieve
to mysql database... Now I want to retrieve the data from directory using path that store in a data base... How is it possible... Please help me.. Please reply...Image retrieve  HI.. store image path/data Java Coding
How to provide navigation in quiz application using jsp?
How to provide navigation in quiz application using jsp?  hi everyone. i am using jsp with mysql connectivity. i have to retrieve questions from db one at a time. on clicking the next it should go the next question
mouse events
mouse events  When i click a button i want to retrieve image and audio from database and play it. how can i do dis. I m using Netbeans IDE and mysql for database. pls do hel[p me
Retrieve image from database using servlet and display in JSP
Retrieve image from database using servlet and display in JSP  Hi, I am total new to JSP although I am learning it for the last few days. Now I want to use MySQL Database from JSP page. How to retrieve image from database using
How to insert and retrieve Questions with special symbols and images into database in an ONLINE EXAMINATION project?
How to insert and retrieve Questions with special symbols and images... an online examination application. I am using JSP,MYSQL and ECLIPSE .The problem...?". and how can I retrieve them to randomize
Sitemap JSP Tutorial Section
mysql database through jsp | How To Page Refresh Using JavaScript ... To Lowercase Tags of JSTL | Display image on JSP page using XML | Make a Pdf... Windows Media Player | Connect JSP with mysql | Connect from database using
java
java  am new to java, I developed a small project using netbeans 6.8, mysql and tomcat, I successfully implement login, check, change, logout.jsp with successfully connected mysql database. Now I want a forgot password jsp which
Sitemap Java Servlet Tutorial Section
value Using Servlet | Hit Counter Servlet Java Servlet Tutorial Section... | How to add a column in a table | Delete a table in mysql | changing column name | insert into statement in sql using servlets | join tables mysql
how to retreive data dynamically from mysql to drop down list
how to retreive data dynamically from mysql to drop down list   sir, i created a table in mysql and i inserted some values into the table through fron end using jsp , after storing the data successfully .i want to retrieve
Retrieve image from database using Servlet
Retrieve image from database using Servlet       In this example we will show you how to develop a Servlet... site you can save the user's photo in database and then retrieve the photo
question
question  i need a jsp and mysql program to retrieve data from employee table and attendance table using user name and password at a time for a Sign in user
JPA 2.1 CRUD examples
for Create, Retrieve (Read), Update and Delete operations on an Entity. From Java...Learn how to create CRUD operation examples in JPA 2.1 In this section you will learn how to create example program that forms CRUD operations against

Ads