根據JavaBean生成數據庫增刪改查的SQL語句
import java
import java
import java
import java
import java
import java
import java
import com
/**
* FOR JDBC RUID Operation
* Object(update/insert/delete)
* or Class(select)
*
*@category com
*@class SQLUtil
*@author
*@date
*@see
*/
public class SQLUtil
{
private final static String database_table_prefix=
private String pkColumnName=
public SQLUtil()
{
//pkColumnName is vary of Table
this
}
public SQLUtil(String pkColumnName)
{
//pkColumnName is vary of Table
this
}
/**
* @param Object the object for insert
* @return an insert String like below:
* insert into vmm_ag(sn
* values(
*/
public static String genInsertSQL(Object bean)
{
List <String> fields=BeanUtil
List <String> values=BeanUtil
Hashtable fieldsTypes = BeanUtil
//System
if(fields
{
String error=
System
return error;
}
//get database table Name
String tableName=getTableName(bean);
String insertSQL=
for (int i =
{
//if value of this field is null
if(values
{
insertSQL+=fields
//if not the last field
if(i!=fields
{
insertSQL+=
}
}
}
insertSQL+=
insertSQL+=
for (int j =
{
String value=
String typeOfThisField=(String) fieldsTypes
if(typeOfThisField
{ //when String
value=
}
else if(typeOfThisField
{ // select to_date(
value=
}
else
{ //other types like long
value=(String) values
}
insertSQL +=value;
if(j!=fields
{
insertSQL +=
}
}
insertSQL +=
//System
return insertSQL;
}
public String genUpdateSQL(Object bean)
{
List <String> fields=BeanUtil
List <String> values=BeanUtil
Hashtable fieldsTypes = BeanUtil
if(fields
{
String error=
System
return error;
}
//get database table Name
String tableName=getTableName(bean);
String updateSQL=
String pkColumnValue=
for (int i =
{
String setSQL=fields
String typeOfThisField=(String) fieldsTypes
String value=
if(typeOfThisField
{ //when String
value=
setSQL+=value;
if(pkColumnName
{
pkColumnValue=value;
}
}
else if(typeOfThisField
{ // select to_date(
value=
setSQL+=value;
if(pkColumnName
{
pkColumnValue=value;
}
}
else
{ //other types like long
value=(String) values
setSQL+=value;
if(pkColumnName
{
pkColumnValue=value;
}
}
if(i!=fields
{
setSQL +=
}
updateSQL +=setSQL;
}
updateSQL +=
return updateSQL;
}
/**
* @param Object the object for insert
* @return an insert String like below:
* insert into vmm_ag(sn
* values(
*/
public String genDeleteSQL(Object bean)
{
List <String> fields=BeanUtil
List <String> values=BeanUtil
Hashtable fieldsTypes = BeanUtil
//get database table Name
String tableName=getTableName(bean);
String pkType=(String) fieldsTypes
String pkValue=
for (int i =
{ //find the pk column
if(pkColumnName
{
pkValue=values
}
}
//if the pkType is String
//number
if (
{
pkValue=
}
//String deleteSQL =
String deleteSQL=genRemoveByIdSQL(tableName
return deleteSQL;
}
/**
* @param Object the object for select
* @return an select String like below:
* select * from vmm_ag
*/
public static String genFindAllSQL(String tableName)
{
String findAllSQL =
return findAllSQL;
}
/**
* @param Object the object for select
* @return an select String like below:
* select * from vmm_ag where sn=
*/
public String genFindByIdSQL(String tableName
{
String findByIdSQL =
return findByIdSQL;
}
/**
* @param Object the object for select
* @return an select String like below:
* select * from vmm_ag where sn=
*/
public String genRemoveByIdSQL(String tableName
{
String findByIdSQL =
return findByIdSQL;
}
/**
* TableName=tablePrefix+ClassName
* eg
* the
* the
* so
* @param bean
* @return tableName
*/
public static String getTableName(Object bean)
{
String className=BeanUtil
//get tablePrefix from config file
String tablePrefix=PropertiesUtil
//get database table Name
String tableName=tablePrefix+className;
return tableName;
}
public static String getTableName(String className)
{
//get tablePrefix from config file
String tablePrefix=PropertiesUtil
//get database table Name
String tableName=tablePrefix+className;
return tableName;
}
/**
*
*
* (
* (
* (
* @param args
*/
public static void main(String[] args)
{
Date aDate = new Date();
AG ag = new AG();
String sql=genInsertSQL(ag);
System
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25899.html