Add, delete, modify, and query concentration camps

Source: Internet
Author: User

Combined Query

Key points:

1. upload function parameters

2. Check whether the parameter is null. If it is not null, spell the SQL statement.

If (name! = NULL &&! "". Equals (name )){
Querysql. append ("and Em. cn like? ");
}

3. Set a parameter for the SQL statement, set an initial value for the location parameter, and then make a judgment. If it is not empty, set the parameter and add the location to 1.

Int Pos = 1;

If (name! = NULL &&! "". Equals (name )){
Pst. setstring (Pos ++, "%" + name + "% ");
}

Complete code:

Public static list finduserbygroup (string erptype, long orgid, string name, string number, string [] groupname, int begin, int end) throws systemexception {
List result = NULL;
Connection conn = NULL;
Preparedstatement PST = NULL;
Resultset rs = NULL;
Stringbuffer querysql = new stringbuffer ();

Querysql. append ("select *");
Querysql. append ("from (select rownum RN ,");
Querysql. append ("em. userid ,");
Querysql. append ("em. u_id screenname ,");
Querysql. append ("em. cn fullname ");
Querysql. append ("from ecp_user_mapping em ,");
Querysql. append ("users_orgs uo ");

If (groupname! = NULL & groupname. length> 0 ){
Querysql. append (", usergroup ug ,");
Querysql. append ("users_usergroups uug ");
}

Querysql. append ("where 1 = 1 ");
Querysql. append ("and Em. erp_type =? ");
Querysql. append ("and Em. userid = uo. userid ");
Querysql. append ("and exists (select * from Organization _ org where org. organizationid = uo. organizationid ");
Querysql. append ("Start with org. organizationid =? ");
Querysql. append ("connect by prior org. organizationid = org. parentorganizationid )");

If (name! = NULL &&! "". Equals (name )){
Querysql. append ("and Em. cn like? ");
}

If (number! = NULL &&! "". Equals (number )){
Querysql. append ("and Em. u_id like? ");
}

If (groupname! = NULL & groupname. length> 0 ){
Querysql. append ("and uug. userid (+) = em. userid ");
Querysql. append ("and UG. usergroupid (+) = uug. usergroupid ");
Querysql. append ("and (");
For (INT I = 0; I <groupname. length; I ++ ){
If (I = 0 ){
Querysql. append ("ug. Name =? ");
} Else {
Querysql. append ("or ug. Name =? ");
}
}
Querysql. append (")");
}
Querysql. append ("order by em. u_id, Em. cn ");
Querysql. append (") t ");
Querysql. append ("where T. Rn? And? ");
System. Out. println (querysql. tostring ());
Try {
Conn = getconnection ();
Pst = conn. preparestatement (querysql. tostring ());
Int Pos = 1;
Pst. setstring (Pos ++, erptype );
Pst. setlong (Pos ++, orgid );
If (name! = NULL &&! "". Equals (name )){
Pst. setstring (Pos ++, "%" + name + "% ");
}

If (number! = NULL &&! "". Equals (number )){
Pst. setstring (Pos ++, "%" + number + "% ");
}
If (groupname! = NULL & groupname. length> 0 ){
For (INT I = 0; I <groupname. length; I ++ ){
Pst. setstring (Pos ++, groupname [I]);
}
}

Pst. setint (Pos ++, begin );
Pst. setint (Pos ++, end );
Rs = pst.exe cutequery ();

Result = util. resultset2javabean (RS, userinfobean. Class );

} Catch (exception e ){
E. printstacktrace ();
Throw new systemexception (E );
} Finally {
Close (Conn, Pst, RS );
}
Return result;
}

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.