relational database and object-oriented conflict

Source: Internet
Author: User
Tags stmt

Object-oriented is developed from the basic Principles of software engineering (such as coupling, aggregation and encapsulation), and the relational database is developed from the mathematical theory, the two sets of theories have distinct differences, and Java is a completely object-oriented language, and this conflict is becoming more and more obvious.

For the current information system of Java EE, the process of object-oriented analysis and object-oriented design is usually adopted. The system is based on the object-oriented approach from requirement design to system design. However, in the detailed design phase, because data persistence needs to be saved to the relational database, it is very frustrating to have to modify the design plan from the bottom up, and return to the old way of programming in the process.

As shown in code 9.2, the data is queried by the database, each field in the result set is read as a bar, and then assembled into an object, and code 9.3 shows the opposite, saving an object to the database. Each property of the object needs to be split, pieced together into a SQL string, and then committed to the database. The process of converting back and forth is cumbersome and extremely error-prone, for an object-oriented program such as nightmares.

Code 9.2 querying Database Assembly objects

List persons = new Arraylist ();
ResultSet rs = stmt.executequery ("SELECT * from person");
To process a result set
while (Rs.next ()) {
person person = new person
int id = rs.getint ("id");
String name = rs.getstring ("name");

Null:rs.getInt ("Age");
Person.setid (ID);
Person.setname (name);
Persion.setaget (age);
Persons.add (person);
}
Code 9.3 saving objects to data
String sql = "INSERT into T_person (ID, name, age) values

Statement stmt = Con.createstatement ();
Stmt.executeupdate (SQL)
Con.commit ()
Good object-oriented developers are tired of this repetitive labor and begin to adopt the usual "positive" lazy approach of creating tools that automate the entire process and help developers care only about objects without having to consider the structure of the database. For relational databases, the best outcome of this effort is the object/relational mapping (ORM) tool introduced in the next SectionTo.

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.