package Util;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class SqlParameter<T>
{
private String Matcher_Value = "@\\w+";
private String SQL;
public String getSQL() {
return SQL;
}
public void setSQL(String sQL) {
SQL = sQL;
}
public List<String> GetPreparedStatement(String SQL_,T t)
{
List<String> MyResult = new ArrayList<String>();
List<String> list = JavaModel(t);
List<dbSqlParameter> list_ment = GetSqlParment(SQL_);
for(int i =0;i<list_ment.size();i++)
{
if(list.indexOf(list_ment.get(i).getValue())>=0)
{
try
{
String Value = BeanUtils.getProperty(t, list_ment.get(i).getValue());
//ment.setObject(list_ment.get(i).index, Value);
MyResult.add(Value);
} catch (Exception e) {e.printStackTrace();}
}
}
return MyResult;
}
private List<dbSqlParameter> GetSqlParment(String SQL_)
{
List<dbSqlParameter> list = new ArrayList<dbSqlParameter>();
Matcher mat = Pattern.compile(Matcher_Value).matcher(SQL_);
int index = 0;
while(mat.find())
{
dbSqlParameter model = new dbSqlParameter();
String value = mat.group();
model.setIndex(index);
model.setValue(value.replace("@",""));
list.add(model);
index++;
}
SQL = mat.replaceAll("?");
return list;
}
private List<String> JavaModel(T t)
{
List<String> list = new ArrayList<String>();
PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(t);
for(int i = 0;i<pd.length;i++)
{
if(pd[i].getWriteMethod()==null)continue;
list.add(pd[i].getName());
}
return list;
}
class dbSqlParameter
{
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
Value = value;
}
int index;
String Value;
}
}
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class SqlParameter<T>
{
private String Matcher_Value = "@\\w+";
private String SQL;
public String getSQL() {
return SQL;
}
public void setSQL(String sQL) {
SQL = sQL;
}
public List<String> GetPreparedStatement(String SQL_,T t)
{
List<String> MyResult = new ArrayList<String>();
List<String> list = JavaModel(t);
List<dbSqlParameter> list_ment = GetSqlParment(SQL_);
for(int i =0;i<list_ment.size();i++)
{
if(list.indexOf(list_ment.get(i).getValue())>=0)
{
try
{
String Value = BeanUtils.getProperty(t, list_ment.get(i).getValue());
//ment.setObject(list_ment.get(i).index, Value);
MyResult.add(Value);
} catch (Exception e) {e.printStackTrace();}
}
}
return MyResult;
}
private List<dbSqlParameter> GetSqlParment(String SQL_)
{
List<dbSqlParameter> list = new ArrayList<dbSqlParameter>();
Matcher mat = Pattern.compile(Matcher_Value).matcher(SQL_);
int index = 0;
while(mat.find())
{
dbSqlParameter model = new dbSqlParameter();
String value = mat.group();
model.setIndex(index);
model.setValue(value.replace("@",""));
list.add(model);
index++;
}
SQL = mat.replaceAll("?");
return list;
}
private List<String> JavaModel(T t)
{
List<String> list = new ArrayList<String>();
PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(t);
for(int i = 0;i<pd.length;i++)
{
if(pd[i].getWriteMethod()==null)continue;
list.add(pd[i].getName());
}
return list;
}
class dbSqlParameter
{
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
Value = value;
}
int index;
String Value;
}
}