Use JDBC to extract data into XML

xiaoxiao2021-03-06  76

What is JDBC? Page 1 (7 pages)

If you have read "Inserting the data in XML in XML into the database" tutorial, you can jump to the SELECT statement. )

Not long ago, in order to interact with the database, developers must use the specific API of that database. This means that creating a database-based application is very difficult (if possible).

JDBC is similar to ODBC (or "Open Database Connection"): It provides a standard API intermediate layer to access the database. As you can see on the left, you can use standard JDBC commands, the JDBC driver converts them into database itself API.

A particular database has never mentioned in this tutorial because it is largely uncertain. All commands are standard JDBC commands, which are converted by the driver to any of the database you selected. Due to this API independence, you can easily use another database without having to change anything other than the driver name and possible connecting URLs in your application.

For information on downloading a JDBC driver suitable for your database, see Resources. More than 150 JDBC drivers can be used, almost override each database.

There is no database of JDBC drivers that can be available.

JBDC-ODBC Bridge Page 2 (7 pages)

As long as you have an ODBC driver, the database does not have to have a specific JDBC driver; you can use the JDBC-ODBC bridge to replace. The application calls the bridge, the bridge converts commands into ODBC, and then the ODBC driver converts them to the database itself API. The JDBC-ODBC bridge is not recommended to access the database, which involves both performance and configuration. - The command must pass two APIs, and must be installed and configured on each client - but if there is no pure JDBC driver It can also be tested and developed with it. If you choose to use the bridge, create a "System Data Source Name (DSN)" on a Windows system by selecting Start> Settings> Control Panel> ODBC Data Sources. Please write down the DSN name because you will use it when you create a connection.

Setting up Databases and Drivers Page 3 (a total of 7 pages) First, make sure you install and run any of the databases you selected, and the driver is available. The JDBC driver can be downloaded from http://industry.java.sun.com/products/jdbc/drivers. Once the database is created, create the necessary tables. This tutorial uses only one table products. Its structure is as shown on the left. Use the steps suitable for your database to create a table. Note: In normal case, you should represent this data at least in two related tables; however, for simplicity, this example only uses only one table.

Accessing Database Page 4 (7 pages) Using Java Language with Database Interaction Usually consists of following steps:

Load the database driver. This can be a JDBC driver or a JDBC-ODBC bridge. Create a Connection of the database. Create a Statement object. This object actually executes SQL or stored procedures. Create a ResultSet, then populate the result of the execution query (if the target is retrieved or directly updated). Retrieve or update data from ResultSet. As part of the "Javatm 2 SDK Standard Edition", the Java.sql package contains "JDBC 2.0 core API" for accessing the database. As part of the "Java 2 SDK Enterprise Edition", the Javax.SQL package contains "JDBC 2.0 optional package API". This tutorial uses only classes in "JDBC 2.0 Core API". Instantiation Drivers Page 5 (A total of 7) To access the database, first load the JDBC driver. Some different drivers can be used at any given time; which one is determined by trying to create a connection to each known driver. The application will use the first successful driver. DriverManager has two ways to know if the driver exists. The first method is to load it directly into it using class.Forname (), as shown in this example. When loading the driver class, it is registered with DriverManager as follows: Public class bricing extends object {

Public static void main (string args []) {

// for the jdbc-odbc bridge, use

// drivername = "sun.jdbc.odbc.jdbcodbcdriver"

String drivername = "jdata2_0.sql. $ Driver";

Try {

Class.Forname (drivername);

} catch (classnotfoundexception e) {

System.out.println ("Error Creating Class:" E.GetMessage ());

}

}

}

After successfully load the driver, the application can connect to the database. DriverManager can find the second method of the driver is to looping any drivers found in the SQL.DRIVERS system feature table. The SQL.DRIVERS feature table is a list of potential drivers separated by a colon. You always check the list before dynamic loading class, so if you want to use a specific driver, make sure that the SQL.DRIVERS feature table or empty or start with the driver you expect.

Creating Connections Page 6 (a total of 7) Successfully loaded the driver, the application can connect to the database. DriverManager is connected by a static getConnection () method, which uses the database's URL as an argument. URL is usually referenced as: JDBC: : DatabaseName

However, it is also possible to write a reference URL in any format that is understood by the active driver. For the format of the URL, please refer to your JDBC driver documentation. One of the cases involving a sub-protocol is connected via an ODBC. If you pass the ODBC to access the DSN's sample database, the URL may be: ODBC: Pricing

This means that the URL may be: JDBC: ODBC: Pricing

The actual connection is created below: Import java.sql.connection;

Import java.sql.driverManager;

Import java.sql.sqlexception; public class profing extends object {

Public static void main (string args []) {

// for the jdbc-odbc bridge, use

// drivername = "sun.jdbc.odbc.jdbcodbcdriver"

// and

// connectURL = "JDBC: ODBC: Pricing"

String drivername = "jdata2_0.sql. $ Driver";

String connectURL = "JDBC: JDataConnect: //127.0.0.1/pricles";

Connection DB = NULL;

Try {

Class.Forname (drivername);

DB = DriverManager.getConnection (connectURL);

} catch (classnotfoundexception e) {

System.out.println ("Error Creating Class:" E.GetMessage ());

} catch (sqlexception e) {

System.out.println ("Error Creating Connection: E.getMessage ());

}

}

}

Once the connection is successfully created, any required database operation (eg, insert or update data) can be performed.

Close Connection Page 7 (A total of 7) Because Statement and Connection are objects, Java will use garbage collection processing to release the database resources they occupied. This may make you mistakenly think that there is no need to close these objects, but it is not true. The Java application itself has a large amount of available resources, which means rarely garbage collection. There is also a possibility, although the Java application has a lot of resources, but the available database resources are limited. Many database resources may be occupied by Java objects, and the application is that this object can be easily turned off. Whether there is an error, make sure that the object is turned off, so you have to add a Finally block to the existing TRY-CATCH block: ...

Connection DB = NULL;

Try {

Class.Forname (drivername);

DB = DriverManager.getConnection (connectURL);

} catch (classnotfoundexception e) {

System.out.println ("Error Creating Class:" E.GetMessage ());

} catch (sqlexception e) {

System.out.println ("Error Creating Connection: E.getMessage ());

} finally {

System.out.Println ("Closing Connections ...");

Try {

db.close ();

} catch (sqlexception e) {

System.out.Println ("Can't Close Connection.");

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-109147.html

New Post(0)