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
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 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 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 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