Mirroring of JavaBeans and relational databases

Source: Internet
Author: User
Tags odbc stmt table definition

Summary

JDK1.1 includes application interfaces (APIs) for new database access (JDBC) and Components (JavaBeans). Together, these two APIs can be used to develop common database code. By using a unique class to access any JDBC database (each application encapsulated in a different component has its specific encoding), users do not have to modify the database code because of the small changes in the database structure.

A relational database basically consists of a series of interconnected tables in which data related to the application system are stored in each table. For example, in an address book database, you might have tables about people, addresses, phone numbers, and so on. In a database, each such entity is stored as a series of strings, integers, and other raw data types. database, the table definition will describe how each entity-related information is stored in a table's fields. For example, you can have two fields in a table named "People" that indicate that the stored string is "last name" and "name." Each table should have one or several field values as identifiers to ensure uniqueness of each record. These identities or "keys" can be used to connect information that exists in different tables. For example, you can specify a unique person number key value for each person in the people table, and use the same key value in the corresponding field in the Address table. This allows you to associate each person with his or her address by matching the value of the "Person number" field in the two tables.

The relational database system appeared in the 70 's, and today it is still the main way to store huge amounts of data. As a result, Java software tools need to have the ability to handle relational databases.

For a relational database to be exploited by a Java application, you first need to solve two problems. First: Need some basic middleware to establish the connection with the database, send SQL query to the database and so on; second: manipulating the database processing results is as easy as manipulating any kind of Java information-as an object. The previous issue has been solved by Sun and several database manufacturers, and the latter one has to be explored further.

In the work of defining a large number of APIs for a common program development business, Sun has maintained a partnership with many software companies. In Jdk1.1apis, the JDBC API was first established. Moreover, it has been used in a number of applications. Some of these applications are 100% pure Java and some are a mix of Java and other programs, such as connecting with existing ODBC data sources (see Figure 1). JavaSoft has placed an introduction of an existing JDBC driver on its web site (http://splash.javasoft.com/jdbc/jdbc.drivers.html).

Figure 11 A typical JDBC or JDBC/ODBC configuration

Note: This diagram has been simplified. Additional components are included (such as the ODBD driver)

Obviously, the pros and cons of these applications depend on your environment and settings, and I'm not prepared to discuss each of their situations. In the following sections, we assume that you already have some kind of java development environment in your machine, and you have installed and tested a JDBC driver correctly, or used some kind of JDBC driver and Sun's Jdbc/odbc bridge.

Jdbcapi

Jdbcapi appears as a separate Java package (or class library, or java.sql), including a series of classes. These classes provide middleware for handling a relational database. Essentially, they make it possible for you to associate a database and issue a query to it. You can process these query results, retrieve the meta-information of your database (meta-information), and handle any anomalies that may occur here.

Let's take a look at a simple JDBC example and see how the query will be simplified after applying the JAVAJDBC. Table 1 is an extremely simple database. The encoding in Listing 1 is the simplest Java statement needed to make a SQL query to a relational database.

staff # name last name
43674 sandor spruit
903 doe
65435 donald duck

String ur1="jdbc:odbc:sample";
String query="SELECT * FROM PERSON";
boolean more;
try
{
Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
Connection con = DriverManager.getConnection(ur1,"sandor","guest");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
While (more = rs,next())
{
int number = rs.getInt("PERSON#");
String firstName = rs.getString("FIRST_NAME");
String lastName = rs.getString("LAST_NAME");
System.out.printIn(number + " " + firstName + " " + lastName);
}
rs.close();
stmt.close();
con.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}

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.