Jbuilder9+weblogic7 of the actual combat article (JDBC 1)

Source: Internet
Author: User
Tags connection pooling odbc sybase database zip
!--google_ad_client = "pub-2947489232296736";/* 728x15, created in 08-4-23msdn/Google_ad_slot = "362 4277373 "; Google_ad_width = 728; Google_ad_height = 15; -->
!--google_ad_client = "pub-2947489232296736";/* 160x600, created in 08-4-23msdn/Google_ad_slot = "4367022601" ; Google_ad_width = 160; Google_ad_height = 600; -->

jbuilder9+weblogic7 actual combat chapter "!--? Xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/-->

tool Piece (Configure JDBC 1)

Author: Huang Kai

E_Mail: hk_sz@163.com

Configuring JDBC 1 in Weblogic7

ABrief introduction of JDBC

Since the Java language was officially published in May 1995, Java has been popular all over the world, with a large number of programs written in the Java language, including database applications. Because there is no Java language API, programmers have to add a C-language ODBC function call to the Java program. This makes many of the best Java features can not be fully developed, such as platform independence, object-oriented characteristics. As more and more programmers become more and more interested in the Java language, more and more companies are investing more and more in Java program development, and the requirements of the Java language interface to access the database are becoming more and more intense. Also because ODBC does have its drawbacks, such as its not easy to use, no object-oriented features, and so on, Sun decided to develop a Java-language interface for database application development. In the jdk1.x version, JDBC is only an optional part, and by the time the JDK1.1 is published, the SQL class package (the JDBC API) becomes the standard part of the Java language.

JDBC (Java Database Connectivity Java DB connection) is the standard API for communication between the Java language and the database system. It consists of a set of classes and interfaces written in the Java language. JDBC provides database developers with a standard API that enables them to write database applications with a pure Java API. With JDBC, it's easy to send SQL statements to various relational databases. In other words, with the JDBC API, you don't have to write a program for accessing the Sybase database, write a program for accessing an Oracle database, and simply write a program with the JDBC API to send SQL statements to the appropriate database.

Simply put, JDBC can do three things:

★ Establish a connection with the database

★ send SQL statement

★ Processing Results

Use jdbc to introduce two packages that make up JDBC:

★ java.sql (Core API)

★ Javax.sql (Extended API)

For the latest information on JDBC, you can check out the JDBC Web site at:

jdbc/">http://java.sun.com/products/JDBC/

Ii. structure of JDBC

JDBC mainly includes the following classes: Connections (Connection), statements (Statement), and result sets (Resultset). The JDBC application works like this: first load the JDBC driver for the database (the JDBC driver has DriverManager class management), then establish the connection (Connection), and have a connection to establish the Statement object (the statement object has three kinds: Statement, PreparedStatement and Callabelstatement) and result set (Resultset) objects (the result set represents records fetched from the database), perform various database operations through the statement object and result set objects, and finally close the connection.

2.1 JDBC Driver

There are 4 types of JDBC drivers. The type of driver you choose depends primarily on the application scope of your program. The correct choice of the appropriate driver, so that it conforms to the design of the database program, is to improve the performance of the program must consider an aspect. To understand these drivers, you first need to understand the fundamentals of the database driver.

The database driver is used to troubleshoot application and database communication problems. Early database products that provide a network library (network libraries) that communicates with the database through a network. The network library consists of components running on the client component and the database server side. In Windows systems, network libraries are usually inthe form of DLLs (Dynamic-link library dynamic Link Libraries ). This form of database client program development has a problem, each database has its own set of APIs, so that the development of the program can not be independent of the database. To solve this problem, the database manufacturer decided to provide a unified high-level API built on top of the network library, and the developer called the high-level API, thus masking the network library closely related to the database product. Microsoft's launch of the ODBC (open databases connectivity Open database Interconnect )API is one such product.

JDBC is an ODBC-like database access interface for Java developers. Unlike ODBC, JDBC supports only Java applications. In a Java application that uses JDBC, only JDBC statements are invoked in the client program, and the actual database communication is done by the JDBC driver provided by the database vendor.

The JDBC driver is divided into the following 4 categories:

JDBC-ODBC Bridge plus ODBC driver

It is translating JDBC operations into the corresponding ODBC calls. It has the advantage of having access to all databases that ODBC can access, with the disadvantage of low execution efficiency.

In this way, ODBC binaries (in many cases, including database client code) must be loaded into each client computer that uses the driver.

Local API

It connects the application directly to the network library, so that the network library must be installed on the computer that uses the driver.

★ JDBC Network pure Java driver

Converts JDBC to a DBMS-independent network protocol, which is then converted by a server into a DBMS protocol. This database driver is installed on the middleware server, not on the client. A network server middleware, such as WebLogic Server, can connect its pure Java clients to a variety of different databases. The specific protocol used depends on the provider.

Local Protocol Pure Java driver

Convert the JDBC call directly to the network protocol used by the DBMS. This will allow direct invocation of the DBMS server from the client machine, which is a useful solution to Internet access. It is fully implemented in Java and requires no additional drivers and client network libraries. This type of driver is provided by the database manufacturer to provide optimal access to the company's database system.

Sun offers a JDBC driver directory on the Internet, which is:

Jdbc/drivers ">http://servlet.java.sun.com/products/JDBC/drivers

2.2 Load JDBC Driver

The first step in using JDBC is to install the driver. Most databases have JDBC drivers, and if there are no JDBC drivers, you can use the sun's Jdbc-odbc bridge driver.

The statement using the Jdbc-odbc Bridge driver is as follows:

Class.forName ("Sun.") JDBC.odbc.JDBCOdbcDriver ");

Statements using the Oracle JDBC driver are as follows:

Class.forName ("Oracle.") JDBC.driver.OracleDriver ");

2.3 Connections (Connection)

2.3.1 Direct connection with pool connection

A connection is a communication connection between the client side Java code and the database, established by the JDBC driver. It holds all context information that is invoked on the database server. In the JDBC API, the connection corresponds to the Java.sql.Connection interface. After you create a connection, you can create a statement (Statement) object, set a connection option, or manage a transaction (Transaction) by connecting.

There are two types of connections: direct connection and pool connection.

1. Direct connection

Direct connections are opened and maintained directly in the client Java code, corresponding to the 1, 2, 4 drivers. When using a direct connection, the connection must be closed after the operation of the database is complete. Otherwise, too many connections will cause system performance to fall even beyond the database server's connection limit so that other programs cannot establish a connection to the database server.

2. Pool Connection

The pool connection is opened and maintained by the Java EE server. The Java EE server starts up with a certain number of pool connections (the specific number is determined by the configuration parameters) and maintains no less than this number of pool connections. When a client needs to connect, the pool connector returns an unused pool connection and marks it as busy. If there are currently no idle connections, the pool driver creates a new number of connections, and the number of new connections is determined by the configuration parameters. When the call to use a pool connection completes, the pool driver marks the connection as idle so that other calls can use the connection.

2.3.2 How to establish a JDBC connection for Oracle

Oracle provides 2 types of JDBC drivers:

JDBC OCI driver

JDBC Thin driver

the statement to establish a connection using the JDBC OCI driver is as follows:

Connection con = drivermanager.getconnection ("JDBC:oracle:oci8: @kkdb", "Test", "test");

the statement to establish a connection using the JDBC thin driver is as follows:

Connection con = drivermanager.getconnection ("JDBC:oracle:thin: @localhost: 1521:kkdb", "Test", "test");

2.4 Statement (Statement)

Statements are used to send data manipulation commands to the database, through the statement object, you can complete the acquisition result set, the database records to add, delete, change, and other operations, statements are divided into the following three types:

The simple statement (Statement) corresponds to the Java.sql.Statement interface and is used to execute simple SQL commands;

The precompiled statement (PreparedStatement) corresponds to the Java.sql.PreparedStatement interface, which can use precompiled SQL statements to improve execution efficiency;

The callable statement (callablestatement) corresponds to the Java.sql.CallableStatement interface and is used primarily to invoke stored procedures defined in the database. A stored procedure is a piece of code that is used to perform repetitive, well-defined tasks that are compiled and stored in a database.

2.5 result Set (results)

The result set corresponds to the Java.sql.ResultSet interface. The statement object emits a query command, and a result Recordset object exists after all eligible records are removed. The result set contains all the rows that conform to the query criteria in the SQL statement, and it provides access to the data in those rows through a set of Get/set methods. The result set is similar to a table with the column headings and corresponding values returned by the query.

Iii.Configuring JDBC in Weblogic7

Weblogic server configuration for JDBC primarily refers to the configuration of connection pooling (Connection pool) and data source (DataSource).

As described earlier,there are two types of JDBC connections, one is a direct connection, the other is a pool connection. The connection pool (Connection pool) in Weblogic server is used to store pool connections. The data source (DataSource) is the intermediary of the client program and the connection pool, and to obtain the connection object in the connection pool, you must establish a data source corresponding to the connection pool, and then obtain the connection through that data source.

Benefits of using connection pooling:

☆ Save time to establish a database connection is very time-consuming. Through the connection pool, you can build a certain number of connections in the connection pool in advance, when new requests come in, you can get a good connection to use without creating a new connection.

☆ Encapsulating User Information use a connection pool to encapsulate the user information (account number and password) used to connect to the database system so that the client program does not have to consider security information when establishing a connection.

3.1 Connecting Oracle Databases

Start WebLogic Server after 3.1.1 configuration startwls.cmd

To use Oracle in WebLogic, first start files in WebLogic Server (my WebLogic installed on C disk) C:eaweblogic700server Add oracle9i to the Instartwls.cmd file (my Oracle uses the default installation in D disk) D:oracleora92jdbclibclasses12.zip, so add in the CLASSPATH statement:

Set Classpath=%java_home%lib Ools.jar;%wl_home%serverlibweblogic_sp.jar;%wl_home%serverlibweblogic.jar; D:oracleora92jdbclibclasses12.zip; %classpath%

Click start /program/bea Weblogic Platform 7.0/user projects/mydomain/start Server;

3.1.2 starts IE, enters http://localhost:7001/consolein the address bar, returns, appears WebLogic server login page, enters Username: and Password:Here are my training, please remember, because in the future will also use this username and password. Click Sign in to enter;

3.1.3 Click Connection Pools;

3.1.4 in the right form, click Configure a new JDBC Connection Pool to establish a connection pool, set in the General tab of the connection pool:

Name: oraclepool (Connection pool name [custom])

URL: JDBC:oracle:thin: @localhost: 1521:kkdb (database connection URL string, typical Oracle configuration: JDBC:oracle:thin: @host Name:port:SID, Where host name represents the hostname, port represents the port number used, and the SID is the global database name of the Oracle database

Driver Classname: Oracle. JDBC.driver.OracleDriver

Properties: user=test

Password=test

Dll=ocijdbc9

Protocol=thin

Click Create create;

In the Connections tab, make the following settings:

Initial Capacity: 5 (the initial number of connections in the connection pool, the default value is 1)

Maximum Capacity: (the maximum number of connections in the connection pool, the default value is 1)

Capacity Increment: 1 (determines the number of connections added per connection [when there are new connection requests and no idle connections in the connection pool])

Click Apply apply;

Set the Targets tab

Set Targets-server, that is, the target server for this connection pool is set. Select a server from the left Available list and choose it to the chosen list on the right. Click apply apply.

3.1.5 Configuration Data Source Testtxdatasource

Click the jdbc/tx Data Source node of the domain tree on the left side of the console, and then click Configure a new JDBC Tx Data source on the right ... ;

Configure the Configuration tab

Name: Testtxdatasource

JNDI Name: Jdbc/testtxdatasource

Pool Name: Oraclepool

Click Create to create

Set the Targets tab

Set Targets-server, that is, the target server for this connection pool is set. Select a server from the left Available list and choose it to the chosen list on the right. Click apply apply.

This completes the Oracle configuration of JDBC in WebLogic server. You can restart WebLogic Server, if not the error is OK.

Note: All the parameters in this paper are based on the "jbuilder9+weblogic7" tool (Oracle9i), the main parameters of the configuration, Sid is Kkdb, the user is test, the password is test. If you are not, please refer to the "Jbuilder9+weblogic7" tool (oracle9i) to build your own SIDs and users.

My Article Starter forum (Www.newer.com.cn/bbs) and Programmer's Forum (www.111cn.net), Welcome to reprint, however, please retain the author and the name of the revising person, thank you.

-->
-->

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.