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


当前第2页 返回上一页

获取数据库连接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语句实现查询的通用方法详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

更多SQL内容来自木庄网络博客


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...