javabean mysql 增删改查_根据JavaBean生成数据库增删改查的SQL语句

本文介绍了如何使用Java类SQLUtil根据JavaBean对象生成针对MySQL数据库的增删改查SQL语句。提供了genInsertSQL、genUpdateSQL、genDeleteSQL和genFindAllSQL等方法,简化了数据库操作。
摘要由CSDN通过智能技术生成

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 根据JavaBean生成数据库增删改查的SQL语句。 1,genInsertSQL(Object bean); 2,genUpdateSQL(Object bean); 3,genDeleteSQL(Object bean); 4,genFindAllSQL(String tableName); 5,genFindAllSQL(String

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

根据JavaBean生成数据库增删改查的SQL语句。

1,genInsertSQL(Object bean);

2,genUpdateSQL(Object bean);

3,genDeleteSQL(Object bean);

4,genFindAllSQL(String tableName);

5,genFindAllSQL(String tableName)

import java.io.Serializable;

import java.lang.reflect.Field;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.Hashtable;

import java.util.List;

import com.domain.AG;

/**

* FOR JDBC RUID Operation,This class can generate SQL by parameter of

*  Object(update/insert/delete)

*  or Class(select)

*

*@category com.util

*@class SQLUtil

*@author zhusheng3@126.com

*@date 2008-6-7 下午04:08:40

*@see

*/

public class SQLUtil

{

private final static String database_table_prefix="database.table.prefix";

private String pkColumnName="SN";

public SQLUtil()

{

//pkColumnName is vary of  Table,so mothods which need pkColumn can not use static

this.pkColumnName="SN";

}

public SQLUtil(String pkColumnName)

{

//pkColumnName is vary of  Table,so mothods which need pkColumn can not use static

this.pkColumnName=pkColumnName;

}

/**

* @param Object  the object for insert

* @return an insert String like below:

*  insert into vmm_ag(sn,domainname,modifydate,testDouble)

*  values(5,'domainname5',to_date('20080121','YYYYMMDD'),22.4)

*/

public static String genInsertSQL(Object bean)

{

List fields=BeanUtil.getFields(bean);

List values=BeanUtil.getValues2String(bean);

Hashtable fieldsTypes = BeanUtil.getFieldsAndTypes(bean);

//System.out.println("fields.size():"+fields.size()+"--values.size():"+values.size());

if(fields.size() !=values.size())

{

String error="ERROR:genInsertSQL(bean)->fields's size does not match values' size,bean:"+bean+

"\n fields.size():"+fields.size()+"--values.size():"+values.size();

System.err.println(error);

return error;

}

//get database table Name

String tableName=getTableName(bean);

String insertSQL="insert into "+tableName+" (";

for (int i = 0; i < fields.size(); i++)

{

//if value of this field is null,then ignore this field

if(values.get(i) !=null)

{

insertSQL+=fields.get(i);

//if not the last field,then add ","

if(i!=fields.size()-1)

{

insertSQL+=",";

}

}

}

insertSQL+=")";

insertSQL+="values (";

for (int j = 0; j

{

String value="";

String typeOfThisField=(String) fieldsTypes.get(fields.get(j));

if(typeOfThisField.equalsIgnoreCase("String"))

{   //when String ,add '' around the string ,like:'tempString'

value="'"+values.get(j)+"'";

}

[1] [2] [3]

test.jsp?url=http%3A%2F%2Fwww.chinaitlab.com%2Fcms%2Fimages%2Flogo14.gif&refer=http%3A%2F%2Fjava.chinaitlab.com%2Fadvance%2F755518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值