当前位置: 代码迷 >> SQL >> 自各儿无聊写的一个dao方法(本想不用写sql语句)
  详细解决方案

自各儿无聊写的一个dao方法(本想不用写sql语句)

热度:50   发布时间:2016-05-05 13:19:42.0
自己无聊写的一个dao方法(本想不用写sql语句)
不知道大家对于数据库的查询的dao方法是怎么写的

项目之前有个select的查询方法的,需要的参数是一个sql语句,和where后面的条件值,自己最近写sql语句写烦躁了,写一个不用sql语句的方法看看,结果也不是很方便,而且灵活性也不高。



package com.xinnuo;import java.util.Vector;import com.xinnuo.core.dao.BaseDaoSupport;public class Sql extends BaseDaoSupport{//getRecord 为之前的方法	// table为表名,word为要查询的字段,where为条件语句,wherevalue为条件语句的值	public Vector select(String table ,String[] word ,String where,String[] wherevalue ) {		String sql="";		Vector rs=new Vector();			sql="select ";		for (int i = 0; i < word.length; i++) {			sql += word[i]+",";		}		sql=sql.replaceAll(",$", "");		sql += " from "+table+" where "+where;		try {			rs=getRecord(sql,wherevalue);					} catch (Exception e) {					}				System.out.println("sql===="+sql);		System.out.println("rs==="+rs.toString());				return rs;	}		public static void main(String[] args) {		String table="power";		String[] word={"Power_ID","Power_Name"};		String where="Power_ID=?";		String[] wherevalue={"16"};		Sql sql=new Sql();		sql.select(table,word,where,wherevalue);	}}

  相关解决方案