Java database programming, Java and XML parsing technology

Source: Internet
Author: User
Tags xpath

Java Database programming:

1. JDBC Overview : JDBC (database connection) is a set of database programming APIs available in Java that defines a standard Java class library (located in the java.sql and javax.sql packages ) to access the database . The database application developed with JDBC can be cross-operating system platform and cross-database system platform.

Among the basic operations of JDBC, the most commonly used classes and interfaces include DriverManager, Connection, Statement, PreparedStatement, CallableStatement, and ResultSet.

(1) The DriverManager class is used to manage the JDBC-driven service class, which is used primarily to obtain connection objects in the program.

(2) The connection interface represents a database connection, and each connection object represents a physical connection session to the database. To access the database, you must first obtain a database connection.

(3) Statement Interface SQL statements, statement objects are used to execute SQL statements, which can be used to execute DML, DDL, and DCL statements. When the SQL query is executed, the query is returned to the result set.

(4) The PreparedStatement interface is a statement sub-interface that represents a precompiled SQL statement. Using the PreparedStatement object allows you to perform precompiled SQL statements with better performance.

(5) The CallableStatement interface is also a sub-interface of the statement, which represents the stored procedure in the database. Use the CallableStatement object to execute stored procedures in the database.

(6) The ResultSet interface represents the returned result set. ResultSet contains methods for accessing the results of a query, which can obtain column data by column index or column name. Use the ResultSet object to manipulate the result set returned by the query.

2.JDBC programming Six steps:

(1) Select the JDBC driver type according to the database used by the application;

(2) Connect to the database, get connection object;

(3) Creating statement objects through connection;

(4) Using the statement object to submit SQL statements;

(5) Operation result set;

(6) Recycling database resources.

3. Connect to the database: There are two ways to set up a connection to a database, namely:

(1) Use the static method of the Java.sql.DriverManager class getconnection (), which takes a URL to represent the name of the data source.

(2) Find the name of the data source using Jndi (Java naming and Directory Interface, Java Naming Service). Jndi returns the Getconnection () method of a Javax.sql.DataSource class.

4. Create a statement object from connection:

There are three types of statement objects in JDBC:

(1) Java.sql.Statement: Represents a simple SQL statement with no parameters;

(2) Java.sql.PreparedStatement:PreparedStatement is a statement sub-interface that represents a precompiled SQL statement. The statement with parameter parameters needs to be set before the SQL statement executes.

(3) Java.sql.CallableStatement:CallableStatement is a sub-interface of statement, CallableStatement objects are used to invoke stored data procedures in the database.

Connection has three methods for creating these three types of statement, respectively:

(1) Statement create Statement (): Creates a basic Statement object.

(2) PreparedStatement preparedstatement (String sql): Creates a PreparedStatement object based on the incoming SQL statement.

(3) CallableStatement preparecall (String sql): Creates a CallableStatement object based on the incoming SQL statement.

Statement is often used to execute an SQL statement that is definitely occurring only once and has no parameters.

5. Execute the SQL statement using JDBC:

Ⅰ executes SQL statements using statement: statement represents any SQL statement, while the statement interface provides some methods for defining and executing SQL statements, including:

(1) ResultSet executeQuery (String sql): Executes an SQL statement that returns a single result set. Only the method can execute a SELECT statement.

(2) int executeupdate (String sql): Use this method to execute DDL and DML (Update, Insert, Delete) statements that do not return a result set. If you are performing DML, the return value is the number of rows affected by the execution of the SQL statement, and if you are executing DDL, the return value is 0.

(3) Boolean execute (String SQL): The method can execute any SQL statement.

Ⅱ executes SQL statements using PreparedStatement: The PreparedStatement interface is used to represent a precompiled statement. The connection interface contains the following six methods for creating a PreparedStatement object:

(1) PreparedStatement (String sql): Creates a default PreparedStatement object using the given SQL statement;

(2) PreparedStatement (String sql, int resultsettype, int concurrency, int holdability): Use a given SQL statement, result set type, concurrency type, and type of persistence to create a Build a PreparedStatement object.

(3) PreparedStatement (String sql, int resultsettype, int concurrency): Creates a preparedstatement with the given SQL statement, result set type, concurrency type Like.

(4) PreparedStatement (String sql, int pk): Creates a default PreparedStatement object that can get the auto-generated keys. The parameter PK indicates whether the auto-generated key flag should be returned, which is one of Statementreturn_generated_keys or Statementno_generated_keys .

(5) PreparedStatement (String SQL, string[] keys): Creates a default PreparedStatement object that can get the auto-generated keys. The keys string array represents the name of the column that makes up the primary key.

(6) PreparedStatement (String SQL, int[] keys): The last method is similar except that the column array is described by using the column index instead of the column name.

       Note: The latter three methods only apply to the Insert method.

Java and XML parsing techniques:

1.XML Overview:

XML refers to Extensible Markup Language (extensible Markup Language), which is a markup language that is much like HTML. XML is designed to transmit data, focus on the content of the data, and HTML is designed to display the data, focusing on the appearance of the data. XML doesn't do anything, it's just designed to structure, store, and transfer information.

1.XML Syntax Rules:

1. It defines the version of the XML (1.0) and the encoding (UTF-8) character set used.

2. All XML elements are paired and there is a start tag that must have a close tag.

3. The XML tag is case sensitive, and label <Letter> is different from tag <letter>.

4. The XML document must have a root element, and if the tag needs to be nested, it must be nested correctly, and the label opened inside the inner layer must be closed in the inner layer.

5. XML tags if you need to add an attribute, the attribute value must be quoted.

6. The syntax for writing annotations in XML is very similar to the syntax of HTML, both <!–this is a comment–>.

7. In XML, spaces are retained, unlike HTML, where more spaces are eventually merged into one.

XML Naming conventions

XML elements must follow the following naming conventions:

    • Names can contain letters, numbers, and other characters
    • Names cannot start with numbers or punctuation marks
    • The name cannot begin with the letter XML (or XML, XML, and so on)
    • Name cannot contain spaces
    • You can use any name without a reserved word.

XML declaration: 1.XML document declaration begins with "<?xml" and ends with "?>";

The 2.XML declaration has three attributes (version), stand-alone document declaration (standalone), and encoding Declaration (encoding);

3. The version declaration document takes the XML version number, must be ranked first;

Dtd:

The purpose of the DTD is to define the structure of the XML document. It uses a series of legitimate elements to define the document structure:

In a DTD, an XML element is declared through an element declaration. The element declaration uses the following syntax:

1 <! Element name Category >

Or

1 <! Element elements name (element content) >

Empty elements: empty elements are declared by the category keyword empty:

1 <! Element name Empty>

Declares an element that appears only once:

     1 <! element name (child element name) >

Declare elements that appear at least once:

1 <! element name (child element name +) >

Declares an element that appears 0 or more times:

1 <! element name (child element name *) >

The declaration appears 0 or one time in the element:

     1 <!ELEMENT 元素名称 (子元素名称?)> 

Declaring attributes:

1 <! Attlist element Name Property Name property type default Value >

Xpath:

An XPath path expression

XPath uses path expressions to pick nodes or set of nodes in an XML document that are very similar to the expressions we see in a regular computer file system.

Xpth node:

In XPath, there are seven types of nodes: elements, attributes, text, namespaces, processing directives, annotations, and document (root) nodes. The XML document is treated as a node tree, and the root of the tree is called the document node or root node.

XPath wildcard characters can be used to select unknown XML elements:

Wildcard characters Describe
* Match any element node
@* Match any attribute node
Node () Match any type of node

Sax provides an event-driven model for parsing XML documents that the SAX parser uses callback mechanisms to notify client applications when different XML syntax structures are identified

Java database programming, Java and XML parsing technology

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.