利用线性表进行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() ;
}
//构造一线性表,然后将数据库字段的名称逐一加入该线性表
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 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
BeanUtils()
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).
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
BeanUtils
public BeanUtils()
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
- 利用线性表进行UPDATE操作
- 线性表进行UPDATE操作
- 对关联表进行Update操作
- 利用最小二乘法进行线性拟合
- 利用editToolbar进行操作
- 利用顺序结构实现线性表的基本操作
- 利用Weka API进行线性回归
- 利用weka来进行线性回归
- 利用R进行多元线性回归分析
- spring使用jpa进行update操作
- 利用GridView进行编辑操作
- 利用response进行上传下载操作
- 利用response进行上传下载操作
- 利用TreeSet进行排序操作
- 利用JNI进行对象操作
- 利用JNI进行对象操作
- 利用ext进行级联操作
- 利用NSOperation进行线程操作
- pku3187
- ASP.NET 2.0 中母版页与内容页的控件之间的访问
- ASP.NET 2.0服务器控件开发之复杂属性
- 一个JNI的小例子
- ASP.NET2.0服务器控件之创建复杂属性
- 利用线性表进行UPDATE操作
- DB2 命令大全
- 通过组合条件对DAO进行查询操作
- DataList中如何响应DropDownList的SelectedIndexChanged事件
- 浅谈数据库设计技巧(下)
- Oracle PL/SQL入门之案例实践
- 新手入门 Windows下Oracle安装图解
- 以文字模式启动Ubuntu
- 新手入门 Windows下Oracle安装图解