JBUILDER9 WebLogic7 actual articles
Tool articles (configure JDBC 1)
Author: Kay Huang
E_mail: hk_sz@163.com
Configuring JDBC 1 in WebLogic7
I. Introduction to JDBC
Since Java Language officially announced in May 1995, Java has a large number of programs written in Java language, including database applications. Since there is no Java language API, the programmer has to add an ODBC function call to the C language in the Java program. This makes many excellent characteristics of Java unable to play, such as platform independent, object-oriented characteristics, etc. As more and more programmers have increasingly affected Java language, more and more companies have increasing the effort in Java program, and the requirements for accessing the database of the Java language interface have become more and more strong. Since ODBC does have its shortcomings, such as it is not easy to use, no object-oriented features, etc., Sun decided to develop a database application development interface with Java languages as an interface. In the JDK1.x version, JDBC is just an optional component. When the JDK1.1 is published, the SQL class (that is, JDBC API) has become the standard part of the Java language.
JDBC (Java Database Connectivity Java Database) is a standard API for the Java language and database system communication. It consists of a class and interface written in Java language. JDBC provides a standard API for database developers, enabling them to write database applications with pure Java API. With JDBC, send SQL statements to various relational databases is a very easy thing. In other words, there is a JDBC API, you don't have to write a program for accessing the Sybase database. To access the Oracle database, you can write a program with JDBC API, which can send SQL statements to the corresponding 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 (extension API)
For the latest information about JDBC, you can consult the JDBC website, the URL is:
http://java.sun.com/products/jdbc/
Second, JDBC structure
JDBC mainly includes the following: Connection, statement, and result set. The JDBC application works like this: first load the JDBC driver of the database (the JDBC driver has DRIVERMANAGER type management), then establish a connection, there is a connection to establish a statement object (statement objects: Statement, preparedStatement and CallabelStatement ) And result sets (result set represents a record from the database), perform various database operations through statement objects and result set objects, and finally close the connection.
2.1 JDBC driver
The JDBC driver has four types. What type of driver for choosing is mainly dependent on the application range. Correctly select the appropriate driver to make it in line with the design of the database program, which is an aspect that improves program performance must be considered. To understand these drivers, we must first understand the basic principles of the database driver. The database driver is used to solve the application and database communication problem. Early database products provide a network library that communicates over the network and databases (NetWork Libraries). The network library consists of a client component and a database server component. In a Windows system, the network library usually exists in the form of a DLL (Dynamic-Link Library Dynamic Link Library). This form of database client program has a problem, and each database has its own set of API, so that the program cannot be independent of the database. In order to solve this problem, the database manufacturer decided to provide a unified high-rise API on the network library, and developers call high-level APIs to block network libraries closely related to database products. Microsoft's ODBC (Open Database Connectivity Open Database Interconnection) API is such a product.
JDBC is a database access interface similar to ODBC-oriented Java developers. Unlike ODBC, JDBC only supports Java applications. In the JDBC's Java application, only the JDBC statement is called only in the client program, and the database communication is completed by the JDBC driver provided by the database vendor.
The JDBC driver is divided into the following four types:
★ JDBC-ODBC bridge plus ODBC driver
It is translated JDBC operation into a corresponding ODBC call. Its advantage is that all databases that can access ODBC can access, and the disadvantage is that the execution efficiency is relatively low.
In this way, the ODBC binary code (in many cases, including database client code) to each client using the driver.
★ Local API
It connects the application directly to the network library so that the network library must be installed on a computer using this driver.
★ JDBC network pure Java driver
Convert JDBC to a network protocol that is not related to DBMS, then this protocol is converted to a server to a DBMS protocol. This database driver is installed on the middleware server instead of being on the client. Network Server Middleware (such as WebLogic Server) can connect its pure Java client on a variety of different databases. The specific agreement used depends on the provider.
★ Local protocol pure Java driver
Convert JDBC call directly to the network protocol used by DBMS. This will allow DBMS servers directly on the client machine, which is a very practical solution for Internet access. It is fully implemented using Java, no other drivers and client network libraries. Such drivers are provided by database vendors to provide access to the company's database system optimization.
Sun offers a JDBC driver directory on the Internet. The URL is:
http://servlet.java.sun.com/products/jdbc/drivers
2.2 Loading a JDBC driver
The first step using JDBC is to install the driver. Most databases have a JDBC driver. If there is no JDBC driver, you can use Sun's JDBC-ODBC bridge driver.
The statement using the JDBC-ODBC bridge driver is as follows:
Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");
The statement of the JDBC driver using Oracle is as follows:
Class.Forname ("Oracle.jdbc.driver.OracleDriver");
2.3 Connections 2.3.1 Direct connection to the pool connection
The connection is a communication connection between the client-side Java code and the database, which is established by the JDBC driver. It saves all context information that is called by the database server. In the JDBC API, the connection corresponds to the java.sql.connection interface. After creating a connection, you can create a statement object, set a connection option or management transaction.
The connection is divided into two types: direct connection and pool connection.
Direct connection
Direct connection is to open and maintain in the client Java code, corresponding to 1, 2, 4 driver. When you use a direct connection, you must turn the connection after the operation of the database is completed. Otherwise, too many connections will result in a decrease in system performance and even more than the connection restrictions of the database server, so that other programs cannot establish to the database server connection.
2. Pool connection
The pool connection is opened and maintained by the J2EE server. The J2EE server starts a certain number of pool connections (the specific number is determined by the configuration parameter) and maintains no less than this number of pool connections. When the client needs to connect, the pool connection program returns an unused pool connection and tagging it as a busy. If there is currently no idle connection, the pool driver has created a certain number of connections, and the number of new connections is determined by the configuration parameters. When the call to the pool connection is complete, the pool driver is marked as idle so that other calls can be used.
2.3.2 How to establish an Oracle JDBC connection
Oracle provides 2 JDBC drivers:
◆ JDBC OCI driver
◆ JDBC Thin driver
Establish a connection to establish a connection using the JDBC OCI driver:
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)
The statement is used to send the data manipulation of the database. Through the statement object, the acquisition result set can be completed, and the database record is increased, deleted, modified, and the statement is divided into the following three types:
● Simply statement corresponds to the java.sql.statement interface, used to perform simple SQL commands;
● PreparedStatement corresponds to the java.sql.preparedStatement interface, which can use the precompiled SQL statement to improve execution efficiency;
● The callastate will correspond to the Java.sql.callablestatement interface, mainly used to call the stored procedure defined in the database. The stored procedure is a code for performing duplicate, defining a clear task, which is stored in the database after compiling.
2.5 Results (result)
The result set corresponds to the java.sql.resultset interface. The statement object issues a query command, and all the qualified records are removed after the results record set objects. The result set contains all rows that meet the query conditions in the SQL statement, and it provides access to data in these rows through a GET / SET method. The result set is similar to a table, where there is a query returned column header and the corresponding value. Third, configure JDBC in WebLogic 7
WebLogic Server's configuration of JDBC is mainly referring to Connection Pool and DataSource.
The front has been introduced, there are two connections in JDBC, one is direct connection, the other is a pool connection. Connection pool in WebLogic Server is used to store pool connections. Dataource (DataSource) is equivalent to the intermediary of the client program and the connection pool, you want to get the connection object in the connection pool, you must establish a data source corresponding to the connection pool and then get a connection via the data source.
Use the advantage of the connection pool:
☆ Save time to establish a database connection is very time. By connecting the pool, you can build a certain number of connections in the connection pool in advance. There is a new request to get a good connection to use it, you don't have to create a new connection.
☆ Package User Information Use the connection pool to encapsulate user information (accounts and passwords) used to connect to the database system so that the client program does not need to consider security information when establishing a connection.
3.1 Connect Oracle Database
3.1.1 Start WebLogic Server after configuring StartWls.cmd
To use Oracle in WebLogic, first add Oracle9i in the startup file c: /bea/weblogic700/server/bin/startwls.cmd file in WebLogic Server (My WebLogic installation) (my Oracle uses the default installation) Driver D: /oracle/ora92/jdbc/lib/classes12.zip in D: /oracle/ora92.zip, so add:
Set classpath =% java_home% / lib / Tools.jar;% wl_home% / server / lib / weblogic_sp.jar;% wl_home% / server / lib / weblogic.jar; D: / Oracle / ORA92 / JDBC / lib / class12. ZIP;% classpath%
Click Start / Programs / Bea WebLogic Platform 7.0 / User Projects / Mydomain / Start Server;
3.1.2 Start IE, enter http: // localhost: 7001 / console, Enter, and Enter username: and password:, here I have training, please remember that you are training. This username and password will be used later. Click Sign IN to enter;
3.1.3 Click Connection Pools;
3.1.4 Click the Configure A New JDBC Connection Pool in the right form to set up a connection pool, set in the General tab of the connection pool:
Name: OraclePool (Connected Pool Name [Custom])
URL: JDBC: Oracle: Thin: @localhost: 1521: KKDB (Database Connection URL String, Oracle Typical Configuration: JDBC: Oracle: Thin: @Host Name: Port: SID, where Host Name Represents Host Name, port representative Port number, 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 to create;
Do the following settings in the Connections tab:
Initial Capacity: 5 (the number of initial connections in the pool, the default value is 1)
Maximum Capacity: 10 (the maximum number of connections in the pool, the default is 1)
Capacity Increment: 1 (Decided to increase the connection of each connection [When there is a new connection request, there is no idle connection in the pool])
Click Apply Application;
Set the Targets tab
Set Targets-Server, which is set to set the target server of this connection pool. Select a server from the Available list from the left side, select it to the chosen list on the right. Click Apply App.
3.1.5 Configuring Data Source TesttxDataSource
Click the JDBC / TX Data Source node of the left side of the console, then click Configure a New JDBC TX Data Source ...
Configure Configuration tab
Name: TesttxDataSource
JNDI NAME: JDBC / TESTTXDATASOSOURCE
Pool Name: OraclePool
Click Create to create
Set the Targets tab
Set Targets-Server, which is set to set the target server of this connection pool. Select a server from the Available list from the left side, select it to the chosen list on the right. Click Apply App.
At this point, complete the JDBC of Oracle in WebLogic Server. You can restart WebLogic Server, if you don't report an error, ok.
Note: All parameters of this article are mainly configured in the "Oracle9i" "article (Oracle9i)", SID is KKDB, login users TEST, password is TEST. If you are not, please refer to the "Oracle9i" "tool (Oracle9i)" to establish your own SID and users.
My article is the first Auro Forum (www.newer.com.cn/bbs) and programmers forum (www.9cbs.net), welcome to reprint, but please keep the author and the name of the revision, thank you.