最近开发用到了递归算法,花了一天时间研究了一下,个人理解递归需注意以下几点
一、首先注意更新条件:即递归调用需要再调用的时候将传入条件更新
二、注意实现自己操作的位置是否合适,因为递归即为循环操作,应该找到适合自己操作的位置,否则会出现重复或者落项
三、递归需要保证每个流程必须完全一致,不存在特殊处理(也不是绝对)
自己写的例子:
需求为:数据库有个字典表,里面存的是所有表的结构(树形,以一个表为根节点,向外延伸)。
现在给出某个表中的一条数据的id,表名和主键,根据这些条件将其所有子表的该条记录的子数据(字段名,字段类型,该字段的值)取出(子表的外键是有一定规律的)
/**
* 递归遍历所有的子表()
* @param conn 数据库连接
* @param tablename:父表表明
* @param id 记录id
* @param rs 记录数据resultSet
* @param list 结果list
* @return
* @throws SQLException
*
*/
public List digui(String tablename, String id,List list,Connection conn ,String primaryKey,int j ) throws SQLException
{
String parent_id = "";
Statement st = conn.createStatement();
String sql_t_dictionary = "select id from t_dictionary where d_table = '"+tablename+"'";//得到表的id
ResultSet rs_t_dictionary = st.executeQuery(sql_t_dictionary);
if(rs_t_dictionary.next())
{
parent_id = rs_t_dictionary.getString(1);
}
String sql = "select * from " + tablename + " where " + primaryKey + "='" + id + "'";
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
while(rs.next()){
List list_clomun = new ArrayList();
if(j==0)//条件有变动
{
id = id;
}else
{
id = rs.getString("ID");
}
int count = rsmd.getColumnCount();
for(int i=1; i<=count; i++)
{
ExchangeData exchangeObj = new ExchangeData();//所需数据been
String columns_code = rsmd.getTableName(i);
int columns_type = rsmd.getColumnType(i);
String columns_name = rsmd.getColumnLabel(i);
exchangeObj.setColumns_code(columns_code);
exchangeObj.setColumns_name(columns_name);
exchangeObj.setColumns_type(columns_type);
exchangeObj.setTable_name(tablename);
// exchangeObj = excuExchangeData(columns_type, rs, exchangeObj,i);
//exchangeObj.setColumns_data向对象set数据
if (columns_type == Types.VARCHAR)
{
exchangeObj.setColumns_data(rs.getString(i));
}else if (columns_type == Types.CHAR)
{
exchangeObj.setColumns_data(rs.getString(i));
}else if (columns_type == Types.INTEGER)
{
exchangeObj.setColumns_data(String.valueOf(rs.getInt(i)));
}else if (columns_type == Types.NUMERIC)
{
exchangeObj.setColumns_data(String.valueOf(rs.getDouble(i)));
}else if (columns_type == Types.TIME)
{
if(rs.getTime(i) != null)
{
exchangeObj.setColumns_data(String.valueOf(timeFormat.format(rs.getTime(i))));
}else
{
exchangeObj.setColumns_data("");
}
}
else if (columns_type == Types.DATE)
{
if(rs.getTime(i) != null)
{
exchangeObj.setColumns_data(String.valueOf(timeFormat.format(rs.getDate(i))));
}else
{
exchangeObj.setColumns_data("");
}
}
else if (columns_type == Types.TIMESTAMP)
{
Timestamp time = rs.getTimestamp(i);
if (time != null)
{
exchangeObj.setColumns_data(String.valueOf(timeFormat.format(time)));
}
else
{
exchangeObj.setColumns_data("");
}
}
else
{
logger.warn("未知的字段类型:" + columns_type + ",未找到适合的处理方法,默认按照字符串处理");
exchangeObj.setColumns_data(String.valueOf(rs.getString(i)));
}
list_clomun.add(i-1,exchangeObj);
}
list.add(list_clomun);
// 子表的外键
primaryKey = tablename+"_ID";
// 找到需要遍历的子表
String sql_info = "select d_table, d_parent,id,d_name from t_dictionary where d_parent = '"+parent_id+"'";
ResultSet rs_info = st.executeQuery(sql_info);//找到所有的需要遍历的子表
//遍历子表
while(rs_info.next())
{
tablename = rs_info.getString(1);
j++;
digui( tablename,id,list, conn,primaryKey,j);
}
}
return list;
}