Add an enum class to the JDK1.5 and map the database table

Source: Internet
Author: User
Tags tostring

An enum class has been added to the JDK1.5 that can be used to map to database tables. And it's very convenient to implement.

Here's an example:

Development environment:

JDK 1.6 U2

Eclipse 3.3

There is a table in the database: Useruser

It has two fields: 1, id;2, username,

We map out an enum:

public enum UserUser {
username,
id;
}

Now we're going to use it to write the SQL statement:

public class UserTest {
public static void main(String [] args)
{
//构建SQL语句
 String sql="select * from "+UserUser.class.getSimpleName() +
" where "+UserUser.username.toString()+"=aksdkk";
System.out.println(sql);
}
}

is not very convenient, and if the database table structure has been changed, in these places in JDK 1.5 to implement the real object-oriented form of SQL statements

Some improvements have been made here:

1. Realize idbtable, let system know it is database table mapping class;

2. Implement the ToString () method so that the field changes in the datasheet;

3. If the table has a primary key, you can return the primary key field Getidcol ();

4. If you have a single primary key (not a composite primary key) and are not automatically growing, you can get the method of ID generation by Idgenerator ().

5. Because the INSERT statement of the table is the same throughout the system, I have added a getinsertsql () function in the mapping class to get the Add data function to improve development efficiency and reduce maintenance effort.

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.