Java XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java XML Database

Java XML Database

shape Description

A database is an accumulation of data that is sorted out so it can without much of a stretch be gotten to, oversaw, and updated. In one view, databases can be ordered by of substance: bibliographic, full-content, numeric, and pictures. XML database is an information steadiness programming framework utilized for putting away the immense measure of data in XML organize. It gives a protected place to store XML records. The client can inquiry put away information by utilizing XQuery, send out and serialize into the desired arrangement. XML databases are typically connected with document-oriented databases.

Types Of XML Databases

shape Description

The following are the databases in XML.

XML-enabled database

The XML-empowered database is just the extension obliged the change of XML record. This is the social database, where data is secured in tables involving sections and columns. The tables contain the arrangement of records, which along these lines involve fields.

Native XML Database

Local XML database relies on upon the holder rather than table design. It can store the sweeping measure of XML record and data. Neighborhood XML database is addressed by the XPath-expressions. Neighborhood XML database has the preferred standpoint over the XML-enabled database. It is significantly gifted to question, store the XML report than the XML-empowered database. The accompanying is an illustration. [xml] <?xml version="1.0"?> <contact-info> <contact1> <name>Satyn</name> <company>Microsoft</company> <phone>(040) 987-76543</phone> </contact1> <contact2> <name>Albert</name> <company>Microsoft</company> <phone>(040) 987-76543</phone> </contact2> </contact-info>[/xml] In the above code, a table of contacts is made that holds the records of contacts (contact1 and contact2), which thus comprises of three elements - company, phone and name.

Servlet Database Access

shape Description

The term JDBC stands for Java Database Connectivity and is a standard Java API for database and used for connecting the wide range database and the Java programming language. In servlet connecting to the database is an important task why because while dealing with big projects more databases will be used. All the databases will support, but the class should be mentioned in the code and username and password of the database are needed. The following task can be done using the JDBC library class.

shape Conceptual figure

The image below explains the connection.

shape Example

Following is an example to connect to the database. Example Servlet JDBC Insertion on Statement Interface: index.html [html] <html> <HEAD><TITLE>A Servlet</TITLE></HEAD> <body> <form action="./welcome" method="get"> <table border="1" bgcolor="ffff"> <tr> <td>ID</td> <td>firstname</td> <td>lastname</td> <td>Start_date(YYYYMMDD)</td> <td>End_date(YYYYMMDD)</td> <td>Salary</td> <td>city</td> </tr> <tr> <td><input type="text" name="Id"/></td> <td><input type="text" name="firstName"/></td> <td><input type="text" name="lastName"/></td> <td><input type="text" name="Start_date"/></td> <td><input type="text" name="end_date"/></td> <td><input type="text" name="salary"/></td> <td><input type="text" name="city"/></td> </tr> </table> <input type="submit" value="submit"/> </form> </body> </html>[/html] web.xml [xml] <web-app> <servlet> <servlet-name>ServletInsertion</servlet-name> <servlet-class>ServletInsertion</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletInsertion</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app> [/xml] ServletJdbcInsert.java [java] import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletInsertion extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection con = null; String id = request.getParameter("Id"); int Id = Integer.parseInt(id); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String start_date = request.getParameter("Start_date"); String end_date = request.getParameter("end_date"); String salary = request.getParameter("salary"); String city = request.getParameter("city"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system"); Statement statement = con.createStatement(); int result = statement.executeUpdate("insert into employee values('"+Id+"',"+"'"+firstName+"',"+"'"+lastName+"',to_date('"+start_date+"','yyyymmdd'),to_date('"+end_date+"','yyyymmdd'),'"+salary+"','"+city+"')"); out.println(result +" row sccessfully inserted your data"); out.flush(); out.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } [/java] Servlet Database Access - In doGet(), the parameters are added to the URL and sent alongside the header data. Sets the substance sort of the reaction being sent to the customer, if the reaction has not been submitted yet. The given substance sort may incorporate a character encoding particular. request.getParameter() method in the servlet class, to recover the info values from HTML page. The Class.forName("") loads the class in the event that it not effectively stacked. The JVM monitors every one of the classes that have been beforehand stacked. This technique utilizes the classloader of the class that conjures it. Once the JDBC driver class is stacked, you are prepared to associate with a SQL Server by utilizing the DriverManager.getConnection(connection_url) technique. The JDBC driver implements java.sql.Driver. The Class#forName() loads them by name which cause them to register themselves with DriverManager#registerDriver() inside a static initializer. the DriverManager#getConnection() in turn tests for every registered driver if Driver#acceptsURL() returns true for the given URL and then calls Driver#connect() on the driver which will then return the concrete Connection implementation. Output Program should be compiled from HTML page, where enter the values and click submit. When click on submit rows will be affected.

Summary

shape Key Points

  • Encoding is the way toward changing over unicode characters into their proportional paired representation.
  • The information put away in the database can be queried utilizing XQuery.