Java Dbutils cannot assign a value to a partial field when querying a database

Source: Internet
Author: User

1,javabean as follows:

public class User {

/**
* User Unique identification (ID)
*/
Private String uid;

/**
* Username (user's mobile phone number)
*/
Private String phone;

/**
* Get user Unique identification (ID)
*
* @return User Unique identification (ID)
*/
Public String Getuid () {
return UID;
}

/**
* Get username (user's mobile phone number)
*
* @return Username (user's mobile phone number)
*/
Public String Getphone () {
return phone;
}

/**
* Set username (user's mobile phone number)
*
* @param phone
* Username (user's mobile phone number)
*/
public void Setphone (String phone) {
This.phone = phone;
}
}

2, the query record is as follows:

String sql = "SELECT * from user where phone =?";
Queryrunner runner= New Queryrunner (Daoutils.getsource ());
@SuppressWarnings ("Rawtypes")
User query = (user) runner.query (SQL, New Beanhandler (User.class), phone);

You will find that the result of the query UID is null,phone value.

~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reason:

The UID field does not have a set method, so there is no value assigned. Thus the dbutils is assigned by the Set method reflection.

Workaround:

The set method of adding UID in JavaBean.

Java Dbutils cannot assign a value to a partial field when querying a database

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.