⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsefromtosql.java

📁 一个基于Java的新闻发布系统
💻 JAVA
字号:
/*
 * Title: The  System of kelamayi Downhole Company [PMIP]
 * 
 * Copyright: Copyright 2005 
 * 
 * Company: hope Co., Ltd
 * 
 * All right reserved.
 * 
 * Created on 2005-12-6
 * 
 * JDK version used	:1.4.1
 * 
 * Modification history:
 * 
 * test.com.comm.parse.ParseFromToSql Author :LiuHongDe(刘宏德)
 * 
 * test.com.comm.parse.ParseFromToSql Version 1.0
 */
package com.hope.common.util.parseVO;

import org.apache.log4j.Logger;

import java.lang.reflect.Method;

/**
 * 
 * @version Version 1.0 
 * @author 刘宏德
 */
public class ParseFromToSql {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ParseFromToSql.class);

	/**
	 * 把值对象解析成修改语句
	 * 
	 * @param from   值对象vo
	 * @return   根据值对象vo解析的修改SQL语句
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * String
	 * Method Name:com.hope.common.util.parseVO.upDate<br>
	 * ParseFromToSql.java Version 1.0<br>
	 * ParseFromToSql<br>
	 */
	public static String upDate(Object from) {
		if (from == null)
			return null;
		String sql = "UPDATE ";
		String field = " SET ";
		String table = "";
		String pk = "";
		String pk_value = "";
		String pk_del = "";
		Method[] fMethods = from.getClass().getMethods();
		if (fMethods == null || fMethods.length == 0) {
			return null;
		}
		Method method;
		try {
			method = from.getClass().getMethod("getTable", null);
			table = ValueObjectUtil.invoke(from, method, null).toString();
			/**  info */
			logger.info("~~~~~~~~~~~~~~=" + table);
			if (table == null || !(table.length() > 0)) {
				/**  info */
				logger.info("method is null");
				return null;
			}
			method = from.getClass().getMethod("getPk", null);
			pk = ValueObjectUtil.invoke(from, method, null).toString();
			/**  info */
			logger.info("~~~~~~~~~~~~~~=" + pk);
			if (pk == null || !(pk.length() > 0)) {
				logger.info("from Table-pk is null !!!");
				return null;
			} else {
				pk_del = pk.substring(0, 1).toUpperCase() + pk.substring(1);
			}
			logger.info(
				"@@@"
					+ "get"
					+ pk.substring(0, 1).toUpperCase()
					+ pk.substring(1));
			method =
				from.getClass().getMethod(
					"get" + pk.substring(0, 1).toUpperCase() + pk.substring(1),
					null);
			Class pkType = method.getReturnType();
			pk_value = ValueObjectUtil.invoke(from, method, null).toString();
			if (pk_value != null) {
				if (pkType.equals(int.class)) {
					pk = pk + "=" + pk_value + "";
				} else {
					pk = pk + "='" + pk_value + "'";
				}
			} else {
				return null;
			}
		} catch (SecurityException e1) {
			/**   Aoto catch cake<br>
			 * 说明:<br>
			 */
			e1.printStackTrace();
			return null;
		} catch (NoSuchMethodException e1) {
			/**   Aoto catch cake<br>
			 * 说明:<br>
			 */
			e1.printStackTrace();
			return null;
		} catch (Exception e) {
			/**   Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		}

		for (int i = 0; i < fMethods.length; i++) {
			try {
				String wmName = fMethods[i].getName();
				if (!wmName.startsWith("set") || wmName.endsWith(pk_del)) {
					continue;
				}
				Class cClazz = from.getClass();
				Method cMethod = null;
				cMethod =
					cClazz.getMethod(
						ValueObjectUtil.getMethodName(wmName.substring(3)),
						null);
				if (cMethod == null) {
					System.out.println("null");
					continue;
				}
				Class retType = cMethod.getReturnType();
				Class paramType = fMethods[i].getParameterTypes()[0];

				if (ValueObjectUtil.invoke(from, cMethod, null) != null) {
					String value =
						ValueObjectUtil.invoke(from, cMethod, null).toString();
					System.out.println(value.length());
					if (value.length() != 0 && !value.equalsIgnoreCase("0")) {
						System.out.println(value);
						if (retType.equals(String.class)) {
							field =
								field
									+ wmName.substring(3, 4).toLowerCase()
									+ wmName.substring(4)
									+ "='"
									+ value
									+ "',";
						} else
							if (retType.equals(int.class)) {
								field =
									field
										+ wmName.substring(3, 4).toLowerCase()
										+ wmName.substring(4)
										+ "="
										+ value
										+ ",";
							} else
								if (retType.equals(double.class)) {
									field =
										field
											+ wmName
												.substring(3, 4)
												.toLowerCase()
											+ wmName.substring(4)
											+ "="
											+ value
											+ ",";
								} else {
									continue;
								}
					} else {
						continue;
					}
				}

			} catch (SecurityException e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			} catch (Exception e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			}

		}
		if (field.length() < 6) {
			return null;
		}
		if (field.endsWith("',")) {
			field = field.substring(0, field.length() - 1);
		}
		if (field.endsWith(",")) {
			field = field.substring(0, field.length() - 1);
		}
		sql = sql + table + field + " WHERE " + pk;
		System.out.println("@@@field=" + field);
		System.out.println("@@@sql=" + sql);
		return sql;
	}

	public static void main(String[] args) {
		//ParseFromToSql fromTo = new ParseFromToSql();
		NameFrom from = new NameFrom();
		from.setUserID(44);
		from.setUsername("小马");
		from.setPassword("user");
		from.setDepartmentID("008");
		from.setAge(27);
		//Test test = new Test();
		//ParseFromToSql.createRunSql(test);
		//ParseFromToSql.add(from);
		ParseFromToSql.upDate(from);
	}
	/**
	 * 
	 * 
	 * @param from
	 * @return
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * String
	 * Method Name:com.hope.common.util.parseFrom.add<br>
	 * ParseFromToSql.java Version 1.0<br>
	 * ParseFromToSql<br>
	 */
	public static String add(Object from) {
		if (from == null)
			return null;
		String sql = "insert into ";
		String field = "(";
		String values = " values (";
		String table = "";
		String pk = "";
		Method[] fMethods = from.getClass().getMethods();
		if (fMethods == null || fMethods.length == 0) {
			return null;
		}
		Method method;
		try {
			method = from.getClass().getMethod("getTable", null);
			table = ValueObjectUtil.invoke(from, method, null).toString();
			/**  logger */
			logger.info("from Table is " + table);
			if (table == null || !(table.length() > 0)) {
				/**  logger */
				logger.info("from Table is null !!!");
				return null;
			}
			method = from.getClass().getMethod("getPk", null);
			pk = ValueObjectUtil.invoke(from, method, null).toString();
			/**  info */
			logger.info("~~~~~~~~~~~~~~=" + pk);
			if (pk == null || !(pk.length() > 0)) {
				logger.info("from Table-pk is null !!!");
				return null;
			} else {
				pk = pk.substring(0, 1).toUpperCase() + pk.substring(1);
			}
		} catch (SecurityException e1) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e1.printStackTrace();
			return null;
		} catch (NoSuchMethodException e1) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e1.printStackTrace();
			return null;
		} catch (Exception e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		}
		for (int i = 0; i < fMethods.length; i++) {
			try {
				String wmName = fMethods[i].getName();
				//档我们的表主建是数据库自动生成的时候,在下面的判断语句中我问要加上"|| wmName.endsWith(pk)"判断
				//如果表主建不是数据库自动生成的,而是认为生成的时候,一定要去掉"|| wmName.endsWith(pk)"判断条件
				if (!wmName.startsWith("set")) { //|| wmName.endsWith(pk)
					continue;
				}
				String wmPrex = wmName.substring(0, 3);
				String wfield1 = wmName.substring(4);
				String da = wmName.substring(3, 4);

				Class cClazz = from.getClass();
				Method cMethod = null;
				cMethod =
					cClazz.getMethod(
						ValueObjectUtil.getMethodName(wmName.substring(3)),
						null);
				if (cMethod == null) {
					/**  logger */
					logger.info("cClass cMenthod is null !!!");
					continue;
				}
				Class retType = cMethod.getReturnType();
				Class paramType = fMethods[i].getParameterTypes()[0];

				if (ValueObjectUtil.invoke(from, cMethod, null) != null) {
					String value =
						ValueObjectUtil.invoke(from, cMethod, null).toString();
					System.out.println(value.length());
					System.out.println(wmName + "===" + value);
					if (value.length() != 0 && !value.equalsIgnoreCase("0")) {
						/**  logger */
						logger.info(value);
						field = field + da.toLowerCase() + wfield1 + ",";
						if (retType.equals(String.class)) {
							values = values + "'" + value + "',";
						} else
							if (retType.equals(int.class)) {
								values = values + value + ",";
							} else
								if (retType.equals(double.class)) {
									values = values + value + ",";
								} else {
									continue;
								}
					} else {
						continue;
					}
				}

			} catch (SecurityException e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			} catch (Exception e) {
				/**  Aoto catch cake<br>
				 * 说明:<br>
				 */
				e.printStackTrace();
			}

		}
		logger.info("sql:" + sql);
		logger.info("table:" + table);
		logger.info("field:" + field);
		logger.info("values:" + values);
		if (field.length() < 2) {
			return null;
		}
		if (values.endsWith("',")) {
			values = values.substring(0, values.length() - 1);
			values = values + ")";
		}
		if (values.endsWith(",")) {
			values = values.substring(0, values.length() - 1);
			values = values + ")";
		}
		if (field.endsWith(",")) {
			field = field.substring(0, field.length() - 1);
			field = field + ")";
		}
		sql = sql + table + field + values;
		logger.info("sql=" + sql);
		return sql;
	}

	/**
	 *	根据值对象vo生成没有查询条件的查询SQL语句
	 * 
	 * @param vo 值对象vo
	 * @return 查询SQL语句
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * String
	 * Method Name:com.hope.common.util.parseVO.queryAll<br>
	 * ParseFromToSql.java Version 1.0<br>
	 * ParseFromToSql<br>
	 */
	public static String queryAll(Object vo) {
		if (vo == null)
			return null;
		String sql = "select * from ";
		String table = " ";
		String Order_by = " Order by ";
		Method method;
		try {
			method = vo.getClass().getMethod("getTable", null);
			table = ValueObjectUtil.invoke(vo, method, null).toString();
			/**  logger */
			logger.info("from Table is " + table);
			if (table == null || !(table.length() > 0)) {
				/**  logger */
				logger.info("from Table is null !!!");
				return null;
			}
			method = vo.getClass().getMethod("getOrder_by", null);
			String by = ValueObjectUtil.invoke(vo, method, null).toString();
			/**  logger */
			logger.info("from Order_by is " + table);
			if (by == null || !(by.length() > 0)) {
				/**  logger */
				logger.info("from Table is null !!!");
				Order_by = "";
			} else {
				Order_by = Order_by + by;
			}
		} catch (SecurityException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			Order_by = "";
			logger.info("from Order_by is NULL Exception !!!");
			//e.printStackTrace();
		} catch (Exception e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		}

		sql = sql + table + Order_by;
		logger.info(sql);
		return sql;
	}

	/**
	 * 根据值对象vo生成删除查询SQL语句
	 * 删除条件是表主建
	 * @param vo
	 * @return
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * String
	 * Method Name:com.hope.common.util.parseVO.del<br>
	 * ParseFromToSql.java Version 1.0<br>
	 * ParseFromToSql<br>
	 */
	public static String del(Object vo) {
		if (vo == null)
			return null;
		String sql = "Delete from ";
		String table = " ";
		String where = " where ";
		String pk = "";
		String pk_del = "";
		String pk_value = null;
		Method method;
		try {
			method = vo.getClass().getMethod("getTable", null);
			table = ValueObjectUtil.invoke(vo, method, null).toString();
			/**  logger */
			logger.info("from Table is " + table);
			if (table == null || !(table.length() > 0)) {
				/**  logger */
				logger.info("from Table is null !!!");
				return null;
			}
			method = vo.getClass().getMethod("getPk", null);
			pk = ValueObjectUtil.invoke(vo, method, null).toString();
			/**  info */
			logger.info("~~~~~~~~~~~~~~=" + pk);
			if (pk == null || !(pk.length() > 0)) {
				logger.info("from Table-pk is null !!!");
				return null;
			} else {
				pk_del = pk.substring(0, 1).toUpperCase() + pk.substring(1);
			}
			logger.info(
				"@@@"
					+ "get"
					+ pk.substring(0, 1).toUpperCase()
					+ pk.substring(1));
			method =
				vo.getClass().getMethod(
					"get" + pk.substring(0, 1).toUpperCase() + pk.substring(1),
					null);
			Class pkType = method.getReturnType();
			pk_value = ValueObjectUtil.invoke(vo, method, null).toString();
			if (pk_value != null) {
				if (pkType.equals(int.class)) {
					where = where + pk + "=" + pk_value + "";
				} else {
					where = where + pk + "='" + pk_value + "'";
				}
			} else {
				return null;
			}
		} catch (SecurityException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} catch (Exception e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		}
		sql = sql + table + where;
		logger.info(sql);
		return sql;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -