利用线性表进行UPDATE操作

来源:互联网 发布:mysql format函数 编辑:程序博客网 时间:2024/05/22 06:36
public void updateChannel(Channel channel) throws Exception {
        
//构造一线性表,然后将数据库字段的名称逐一加入该线性表
        List colums = new ArrayList() ;
        colums.add(
"channel_name") ;
        colums.add(
"channel_order") ;
        colums.add(
"channel_status") ;
        
//StringBuffer sqlstring = new StringBuffer() ;
        sql = "UPDATE news_channel SET " ;
        
for(int i=0; i<colums.size(); i++{
            
if(colums.size() -1 == i) {
                sql 
+= colums.get(i) + "=?" ;
            }
else {
                sql 
+= colums.get(i) + "=?, " ;
            }

        }

        sql 
+= " WHERE channel_id =?" ;
        pstmt 
= con.prepareStatement(sql) ;
        
for(int i=0; i<colums.size(); i++{
            String v 
= ((String)(colums.get(i))).replace("_""") ;
            
//利用BeanUtil包从对象中取出数据,赋值到SQL语句中
            
//static Object getValue(Object obj, String property) 
                                               
//  Get a single property of the bean. 
            pstmt.setObject(i+1, BeanUtils.getValue(channel,v)) ;
        }

        pstmt.setInt(colums.size()
+1, channel.getChannelid().intValue()) ;
        System.out.println(
"the SQL is:"+sql) ;
        pstmt.executeUpdate() ;
        pstmt.close() ;

    }

 附:com.opensymphony.util.BeanUtils API

Overview  Package   Class  Use  Tree  Deprecated  Index  Help   PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD


com.opensymphony.util
Class BeanUtils

java.lang.Object  extended by com.opensymphony.util.BeanUtils

public class BeanUtils
extends Object
 

Convenience methods for manipulating the getter/setter properties of JavaBeans (well any kind of Object).

These methods are provided purely for convenience to frequent operations. No exceptions are thrown from the methods.

 

 

Version:
$Revision: 47 $
Author:
Joe Walnes

Constructor Summary BeanUtils()
              Method Summary static String[] getPropertyNames(Object obj)
          Get list of property names of bean. static Object getValue(Object obj, String property)
          Get a single property of the bean. static Map getValues(Object obj, String[] allowedProperties)
          Get Map of property values from a bean. static boolean setValue(Object obj, String property, Object value)
          Set a single property of the bean. static void setValues(Object obj, Map valueMap, String[] allowedProperties)
          Set multiple properties of a bean at once using a Map. static void setValues(Object obj, Object src, String[] allowedProperties)
          Set multiple properties of a bean at once using properties of another bean. static void setValues(Object obj, ServletRequest request, String[] allowedProperties)
          Set multiple properties of a bean at once using the params passed across from the ServletRequest (useful for mapping HTML forms to beans).   Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait  

Constructor Detail

BeanUtils

public BeanUtils()
Method Detail

getPropertyNames

public static final String[] getPropertyNames(Object obj)
Get list of property names of bean.

 

Parameters:
obj - Object to query for property names.
Returns:
Array of property names, or null if an error occurred.

setValue

public static final boolean setValue(Object obj,                                     String property,                                     Object value)
Set a single property of the bean.

 

Parameters:
obj - The object to be manipulated.
property - Name of property to set.
value - Value to set property to.
Returns:
Boolean indicating success.

getValue

public static final Object getValue(Object obj,                                    String property)
Get a single property of the bean.

 

Parameters:
obj - The object to be accessed.
property - Name of property to get.
Returns:
Value of property. If property was not found, null is returned.

setValues

public static final void setValues(Object obj,                                   Map valueMap,                                   String[] allowedProperties)
Set multiple properties of a bean at once using a Map. Any unknown properties shall be ignored.

 

Parameters:
obj - The object to be manipulated.
valueMap - Map containing property-name (String) / property-value (Object) pairs to set in the object.
allowedProperties - If array is NOT null, only the properties matching names passed here shall be set.

setValues

public static final void setValues(Object obj,                                   Object src,                                   String[] allowedProperties)
Set multiple properties of a bean at once using properties of another bean. The beans may be of different types and any properties not common to both types shall be ignored.

 

Parameters:
obj - The object to be manipulated.
src - The object containing the properties to be copied.
allowedProperties - If array is NOT null, only the properties matching names passed here shall be set.

setValues

public static final void setValues(Object obj,                                   ServletRequest request,                                   String[] allowedProperties)
Set multiple properties of a bean at once using the params passed across from the ServletRequest (useful for mapping HTML forms to beans). Any properties not known shall be ignored.

 

Parameters:
obj - The object to be manipulated.
request - ServletRequest to get params from.
allowedProperties - If array is NOT null, only the properties matching names passed here shall be set.

getValues

public static final Map getValues(Object obj,                                  String[] allowedProperties)
Get Map of property values from a bean.

 

Parameters:
obj - Object to query for properties.
allowedProperties - If array is NOT null, only the properties matching names passed here shall be retrieved.
Returns:
Map containing property-name (String) / property-value (Object) pairs.

Overview  Package   Class  Use  Tree  Deprecated  Index  Help  OSCore Project Page  PREV CLASS   NEXT CLASS FRAMES    NO FRAMES     All Classes SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Copyright 2000-2005 - OpenSymphony

原创粉丝点击