Apply Java technology to implement database application system

zhaozj2021-02-16  66

[Content] This article details the JDBC API and Java.sql package programming for Java database. A database application management system case for Java technology design, development, practice is template, combined with Java2 platform procedures to explain the development ideas of Java-oriented language, language characteristics, security mechanisms, and case specific implementation steps, methods, and processes, etc. . I believe that through this paper, the majority of developers will appreciate the style of object-oriented language, deep understanding of Java's development ideas. Thus additional efficient, forward-looking management information systems and Live Intranet solutions are available for our users.

[Key words] OOP NC JDBC Applet Application Mis Live Intranet

The Java language is an object-oriented programming language-OP (Abbr.Object Oriented Programming) that Sun Microsystems officially named and launched in 1995. Sun said: Java programming language is designed to be BY Program for Programmers, which is widely popular with the development of the Internet. Use the system "once developed, run" characteristics and how the object-oriented ideas reflected in the program development design are deeply touched. Its language is simple, set a variety of programming languages, and object-oriented, portable, distributed, security, high performance. Java is penetrating to Internet / Intranet, and even the field of the computer world, slowly change people's thinking. Today, Today, the heat wave is overworm, from the external world to the enterprise to form intranet, which promotes the use of Java technology. IBM, ORACAL, Apple, Sun, Netscape, in the computer industry, also jointly launched the "Network Computer (NC-1) specification. Throwing "Wintel" bloated architecture, uses the user's price, easy to use, and become simple computer-NC (Network Computer) that can connect to the network, that is, "thin customer" machine, NC support Java virtual machine (JVM), which can run Java development applications (Application) and applets (Applet) and support multimedia applications. The combination of NC and Java has born new era: network computer era. Even Sun's family's head Microsoft's Bill. Gates had to recognize: "Java is the most outstanding programming language for a long time."

First, Java Database Basic JDBC API

Java languages ​​In database applications, especially in terms of the Irritable complex configuration of online database applications based on Web-based B / S structures, they do not make user and program development both sides are very satisfactory. Although SunSoft provides Java and database interface specification JDBC (Java Database Connectivity, Java Database Connectivity), and JavaSoft says that JDBC does not mean, so that Java programs can communicate with different databases through the JDBC API of the Unified Standard Specification. Ensure the "100% pure Java" solution. JDBC API defines classes and interfaces in Java, indicating database connections, SQL instructions, result collection, and more. It allows Java programmers to send SQL instructions and process results. The JDBC API provides two main interfaces: one is a Java.sql package for developers, so that the Java programmer can perform database connections, perform SQL queries, and get results collection. Java2's Java.sql package provides 6 classes and 18 interfaces, which will be introduced; another JDBC Drivers facing the underlying database manufacturer, so far, Java2 JDBC Drivers only provides the following four types of database drivers, And each of them:

(1) JDBC-ODBC Bridge Plus ODBC DRIVER Mode: JDBC-ODBC Bridge Method With Microsoft's Open Database Interconnection Exit (ODBC API) with database server, client computer first should install and configure ODBC Driver and JDBC-ODBC Bridge two Driver. This is the most likely solution that Applets accesses your database, but this is a very annoying and trouble solution for Internet and Intranet users. (2) Native-API Partly Java Driver Mode: This drive mode converts the special protocol of the database vendor into Java code and binary category, making the Java database client to communicate with the database server. For example: Oracle uses the SQLNET protocol, DB2 uses IBM's database protocol. The special agreement of database vendors should also be installed on the client. This is also an annoying and trouble solution.

(3) JDBC-NET PURE JAVA DRIVER mode: This method is pure Java Driver. Database customers communicate with database clients (such as http, shttp), database access server then translate the standard network protocol to become a dedicated special database access protocol (or ODBC DRIVER) to communicate with the database. This is an ideal solution for Internet and intranet users. Java Driver is automatically downloaded and installed on the user's computer with Applets from the web server with Applets.

(4) Native-Protocol Pure Java Driver mode: This method is also pure Java Driver. Database vendors provide a special JDBC protocol to make Java database customers communicate with database servers. However, the agent protocol will communicate with the database server to the special JDBC Driver for database vendors. This is efficient to intranet applications, but the database manufacturer's protocol may not be supported by firewalls, lack of firewall support, there is potential security hazards in Internet applications.

In summary, only the third, four ways drive support the zero installation of the applet. Because JDBC Drivers are completely written in Java, download from Applet from the web server. To support zero installation, the driver should be placed on the web and with the applet in the same directory. And the fourth type of safety hazard, the third product is not much, today's more mature IDS JDBC Driver belongs to this (http://www.idssoftware.com), but also uses ODBC Driver assist.

Even so, use Java technology to develop single-machine environment applications, LAN scope, or intranet environment, dynamic web applications, etc., Java languages ​​are efficient, safe and stable. The Java language has won support for many manufacturers, based on its Java API-JDBC, has also developed rapidly. Sun promises that any Java Applet or Java application can be combined with the database and will still have a spare no effort to support the development of future Java technology. The cross-platform characteristics of the Java language make it an ideal option for developing database application systems in the Internet and intranet environment.

Second, the implementation of the Java database application system

Based on this, the author uses Java 2 SDK, Standard Edition Version 1.2.1 is the development tool, with the Java database application system case that has been successfully developed as a template, combined with the Java2 program statement and java.sql package, introduce the Java database application system development Specific implementation steps, methods, etc.

1. Set the connection of the database

In order to take advantage of JDBC, you will need a database server and a database driver. Because most readers' computers have installed Windows95 / 98, I use Microsoft's Access 97 as my database server (specifically action.mdb), of course, you can also use other databases as a database server. Because of JDBC, the type of database server is not critical, JDBC provides a method of accessing a database of database servers. This is the main advantage of JDBC. You will also need a database driver to provide JDBC connections to your database. The author uses a more likely JDBC-ODBC Bridge mode. To set up the data source of 32-bit ODBC, select System DSN (Data can be accessed by multi-user access, different), add new data sources, and configure the path to the new data source is the location of the Action.mdb database server.

The installation is complete, the Java programmer can make the program stage. First, the programmer refers to the corresponding class and interface in the java.sql package to perform the management of the JDBC drive: load the JDBC drive with the forname () method in the class class, as an example of JDBC-ODBC Bridge, load JDBC driver execution The statement is class.forname ("Sun.jdbc.odbc.jdbCodbcDriver); returns all the JDBC drives installed in your computer in the DRIVERMANAGER class, and GetConnection () establishes the connection to the database. The getConnection () method provides the following three forms:

GetConnection (String URL)

GetConnection (String Url, String Userid, String Password)

GetConnection (String Url, Properties Arguments)

The string URL represents the special URL where the database server is located. The URL is a unified network resource that is used to driver a connection with a different database, which is as follows:

JDBC: Subprotocol: SUBNAME

All JDBC database protocols began in JDBC:. SubProtocol is used to distinguish the JDBC driver. For example: JDBC-ODBC Bridge With JDBC: ODBC: Subname This user protocol form, IDS JDBC Driver is in the form of JDBC: IDS: Subname. The subname identifies the database protocol and is related to the SubProtocol and JDBC drivers. For example, in a JDBC-ODBC Bridge mode, I use the following URL form to establish a connection to the data source TEND:

JDBC: ODBC: TEND

GetDrivers () and getConnection () methods are the most important ways in the DriverManager class, and some other methods are here.

2. Connect with the database

The GetConnection () method of the DriverManager class establishes with the database connection, getConnection () returns an object, this object is the Connection interface object in the java.sql package. This interface defines some methods, constants, etc. of the database interaction with it, these methods are used to manage the connection of the database, obtain the connection information, submit the database processing event and preprocessing the executable SQL statement. Below is some important methods of the Connection interface:

Close () - Close a database connection

GetMetadata () - Returns a DatabaseMetadata interface object that can be used to get detailed information about the database, including database structure and database content.

CreateStatement () - Create a SQL statement object.

PrepareStatement () - Create a SQL pre-processing statement object with the SQL string. The pre-processing statement is a precompiled SQL statement that can be efficiently performed.

Next, a program is provided to implement JDBC drivers and database connections for reference:

Import java.sql. *;

Import java.util. *;

Class connectApp {

Public static void main (string args []) {

Try {

// load the jdbc-odbc bridge driver mode

Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");

String Url = "JDBC: ODBC: TEND";

/ / Connect to the database

Connection Connection = DriverManager.getConnection (URL);

// Get information on the database

DatabaseMetadata meta = connection.getMetadata ();

3. Utilize the result set

After the database query is executed, the result of the query is returned as a two-dimensional (row, column) data table. The ResultSet interface is used to provide a data sheet for accessing the query results, and the results of the query are returned as the ResultSet object, and the ResultSet object provides "pointer", the pointer each access the database table. When the RESULTSET object returns from the query, the pointer initially points to the first line of the data table, the resultset's next () method is used to move the next line of the pointer to the data table, if the end of the table, the next () method returns a fake Boolean value -false, otherwise it is true. The ResultSet interface provides a large number of methods for obtaining data, which returns data in any location in the data table, whether the basic data type or data of the reference data type. GetMetadata () Method Returns the ResultSetMetadata interface object that contains information about the data table line. The ResultSetMetadata interface provides a variable and a method of obtaining information from the ResultSet object. The getColumnCount () method returns the number of columns of the data table, the getColumnName () method returns the column name in the extracted database, that is, the database field name, getColumnType () method Returns the SQL of the column Type, other methods in ResultMetadata are used with additional properties of the access column, such as the list of display widths, data formats, and read / write status. Note that the columns in the ResultSet are 1 rather than 0 instead of 0. Let's provide a program:

Static Void DisplayResults (ResultSet R) throws sqlexception {

// Get a collection of data results

ResultSetMetadata Rmeta = R.getMetadata ();

/ / Determine the number of columns of the data set, and the number of fields

Int numcolumns = rmeta.getColumncount ();

/ / Output Each data value

For (int i = 1; i <= numcolumn; i) {

IF (i

System.out.print (Rmeta.getColumnname (i) "|");

Else

System.out.println (Rmeta.getColumnName);

}

While (r.next ()) {

For (int i = 1; i <= numcolumn; i) {

IF (i

System.out.print (R.Getstring (i) "|"); ELSE

System.out.println (R.getstring (i) .trim ());

}

}

}

4. Execute SQL statements

SQL represents the structured query language, the abbreviation of Structure Query Language. SQL is a language used to interact with the database server. SQL was developed from IBM in a 70-280, and it was standardized after the 1980s. But the standard has been updated, and several versions have now occur. Other database manufacturers also increased some special functional statements. JDBC needs to support the JDBC-compatible SQL drive support to support the ANSI SQL72 standard. SQL has a lot of uses, when the SQL statement is used to create, design the database, it is a data definition language; when used to insert information, update, or delete existing information to the database, it is a data maintenance language; When you query information in the database, it is a data query language. For some syntax rules for the SQL statement, please refer to the book, this is omitted. The purpose of the Java language design java.sql package is to let you perform the Java's SQL statement. The Statement interface in java.sql defines some methods that use the execution of the SQL statement to interact with the database, also supports the processing of the query result collection through the ResultSet object and provides the control of the query processing mechanism. Execute (), ExecuteQuery () and executeUpdate () methods are the main ways of the Statement interface. The executeQuery () method performs a SQL statement (such as a SELECT statement, etc.) and queries a RetallSet object. The executeUpdate () method performs a SQL statement (such as INSERT, UPDATE, DELETE statement, etc.) and update the database, returns the number of rows (records) of the SQL query result collection, if the result set is a space number of 0.

The program fragment of the SQL statement in Java is as follows:

Import java.sql. *;

Import java.util. *;

Class resultapp {

Public static void main (string args []) {

Try {

// load the jdbc-odbc bridge driver mode

Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");

String Url = "JDBC: ODBC: TEND";

/ / Connect to the database

Connection Connection = DriverManager.getConnection (URL);

Statement Statement = connection.createstatement ();

String SQL = "SELECT * from Courses";

// Execute SQL statement to get the result collection

ResultSet Result = Statement.executeQuery (SQL);

DisplayResults (Result);

/ / Turn off the database connection

Connection.Close ();

} catch (exception ex) {

System.out.println (ex);

System.exit (0);

}

}

5. Database security

The security issue of the database is a question that many organizations are very concerned. Because the database may contain some relatively sensitive information needs to restrict access range; the integrity of data is also extremely important, unauthorized arbitrarily modifying database information may result in a huge loss of the company; the effective provision of data is also important, Internet / intranet Users need information at any time, which often requires 24 hours a day, 7 days a week. JDBC supports database security? The answer is not very satisfied. The current JDBC must provide security protection with the database server itself. Enterprise-wide database servers, such as Microsoft's SQL Server provides user IDs and passwords, however, even if password protection is not very secure, if the password is not encrypted between customers and servers, they can be easily intercepted. Java's security mechanism is very high, Java2 improves the sandbox, providing users and proceeds with flexible security management and configuration tools. The Applet program is more secure. It is recommended to use Java security mechanism and some of the tools to be developed to develop database applications.

The security tools provided by Java2 (JDK1.2) are as follows:

Pack your applets with JAR tools into Java Archive file (JAR);

Use Jarsigner to digitally sign the JAR file;

Configure the Java Running Environment for Trust Applets with PolicyTool. Java Runtime Environment (JRE) configuration;

Use KeyTool to perform a visa for digital certificates;

Third, Java's database application development prospects

At this point, you have known: To perform the design of Java database applications, you need to have multi-faceted skills, including understanding or skilled database drivers, SQL (structured query language), and class, interfaces contained in the Java.sql package. JavaSoft has noticed that Java's tricky and complex in database program design, and they are now developing new products Javablend as easy to use as Java, which will greatly simplify the process of building database application systems using JDBC. Javabled will automatically mirror Java objects into information stored in the database. All objects of objects will generate a database of the mirroring database, including automatic queries, updates, etc. stored in the database. Once Java objects are successfully established, JavaBend will be sent to the Java programmer for free, so that Java programmers will use the cumbersome SQL statement, the result collection solution transitions to the use of Javablend The method of Java object calls automatically converts to SQL statements and results collection. This will make JDBC and SQL knowledge do not have to consider too much when developing in database, but make programmers to provide solve the user business solution. Javablend is currently not included in JDK 1.2.

Java language and its technology are the mainstream of today's world programming language. Applying Java technology to implement the database application system is the development direction of the future. Java's database applications have been successfully applied to commercial, government and universities, and applications have just started. I believe that Java technology will be more mature in the near future. Java's database applications will also flow through.

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

New Post(0)