当前位置: 移动技术网 > IT编程>开发语言>Java > java执行SQL语句实现查询的通用方法详解

java执行SQL语句实现查询的通用方法详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

完成sql查询 并将查询结果放入vector容器,以便其他程序使用

/* 
   * 执行sql查询语句 
   */
public static <t> vector<t> executequery(class<t> clazz, string sql, object... args) {
	connection conn = null;
	preparedstatement preparedstatement = null;
	resultset rs = null;
	vector<t> vecrs = new vector<t>();
	t obj = null;
	try {
		conn = jdbctools.getconnection();
		preparedstatement = conn.preparestatement(sql);
		// 通过sql语句来判断选择了那些列 
		for (int i = 0; i < args.length; i++) {
			preparedstatement.setobject(i + 1, args[i]);
		}
		// 利用sql查询获取结果集 
		// 利用反射创建实体类的对象 
		// 获取结果街的别名stud_id 获取jdbc的元数据 
		// 获取结果集每一列的值,结合上一步得到一个map键值对 
		// 键:列的别名 值:列的值 
		// 在利用反射对实体类对象的属性赋值 
		// 属性为map的键 值为map的值 
		rs = preparedstatement.executequery();
		// 获取元数据 
		resultsetmetadata rsmd = rs.getmetadata();
		map<string, object> mapmetadata = new hashmap<string, object>();
		// 打印一列的列名 
		while (rs.next()) {
			//获取数据表中满足要求的一行数据,并放入map中 
			for (int i = 0; i < rsmd.getcolumncount(); i++) {
				string columnlabel = rsmd.getcolumnlabel(i + 1);
				object columnvalue = rs.getobject(columnlabel);
				// system.out.println(columnlabel); 
				mapmetadata.put(columnlabel, columnvalue);
			}
			//将map中的数据通过反射初始化t类型对象 
			if (mapmetadata.size() > 0) {
				obj = clazz.newinstance();
				for (map.entry<string, object> entry : mapmetadata.entryset()) {
					string fieldkey = entry.getkey();
					object fieldvalue = entry.getvalue();
					// system.out.println(fieldkey + ":" + fieldvalue); 
					reflectionutils.setfieldvalue(obj, fieldkey, fieldvalue);
					//通过反射赋值
				}
			}
			//将对象装入vector容器 
			vecrs.add(obj);
		}
	}
	catch (exception e) {
		e.printstacktrace();
	}
	return vecrs;
}

其中使用到的工具类方法

获取数据库连接jdbctools.getconnection()

/* 
   * 获取数据库的连接 
   */
public static connection getconnection() throws exception {
	connection conn = null;
	string driver = null;
	string jdbcurl = null;
	string username = null;
	string password = null;
	// 获取properties对象 
	properties properties = new properties();
	inputstream in = jdbctools.class.getclassloader().getresourceasstream("jdbc.properties");
	properties.load(in);
	driver = properties.getproperty("driver");
	jdbcurl = properties.getproperty("jdbcurl");
	username = properties.getproperty("user");
	password = properties.getproperty("password");
	class.forname(driver);
	conn = drivermanager.getconnection(jdbcurl, username, password);
	return conn;
}

reflectionutils.setfieldvalue(obj,fieldkey,fieldvalue);

将obj对象的fieldkey属性赋值为fieldvalue

//设置对象的属性 
public static void setfieldvalue(object obj,string fieldname,object value){
	field field=getdeclaredfield(obj, fieldname);
	if(field==null){
		throw new illegalargumentexception("could not find field["+ 
		    fieldname+"] on target ["+obj+"]");
	}
	makeaccessiable(field);
	try{
		field.set(obj, value);
	}
	catch(illegalaccessexception e){
		system.out.println("不可能抛出的异常");
	}
}
//判断field的修饰符是否是public,并据此改变field的访问权限 
public static void makeaccessiable(field field){
	if(!modifier.ispublic(field.getmodifiers())){
		field.setaccessible(true);
	}
}
//获取field属性,属性有可能在父类中继承 
public static field getdeclaredfield(object obj,string fieldname){
	for (class<?> clazz=obj.getclass(); clazz!=object.class; clazz=clazz.getsuperclass()){
		try{
			return clazz.getdeclaredfield(fieldname);
		}
		catch(exception e){
		}
	}
	return null;
}

总结

以上就是本文关于java执行sql语句实现查询的通用方法详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网