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

📄 sqlserverpersistencemanager.java

📁 软件设计课做的一个类似Hibernate的O/R Mapping的框架
💻 JAVA
字号:
package cn.edu.nju.software.sd.torm.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.edu.nju.software.sd.torm.PersistenceException;
import cn.edu.nju.software.sd.torm.PersistenceManager;
import cn.edu.nju.software.sd.torm.cfg.PersistenceClass;
import cn.edu.nju.software.sd.torm.cfg.PersistenceProperty;
import cn.edu.nju.software.sd.torm.exception.FieldAccessException;
import cn.edu.nju.software.sd.torm.exception.UnRegisteredClassException;
import cn.edu.nju.software.sd.torm.util.IDGeneratorFactory;

/**
 * The SQLServerPersistenceManager class is the SQLServer implementation
 * of the <tt>PersistenceManager<tt>. Which supports ID auto-generation,
 * object buffering. And it's thread safe. That means, it is safe in the
 * cross-thread situation. For example. Two threads loaded the same record
 * and one of the threads modified the properties of the object. The other
 * thread will immediately receive the modification of the object.
 * 
 * @author YinfeiXU
 * @version 1.0.0
 *
 */
/**
 * @author Devin
 *
 */
public class SQLServerPersistenceManager extends DefaultPersistenceManager {


	/**
	 * Create an new instance of SQLServerPersistenceManager.
	 */
	public SQLServerPersistenceManager() {
	}



	/* (non-Javadoc)
	 * @see cn.edu.nju.software.sd.torm.impl.DefaultPersistenceManager#getPropertyValue(java.lang.Class, java.lang.Object, cn.edu.nju.software.sd.torm.cfg.PersistenceProperty)
	 */
	protected Object getPropertyValue(Class c, Object obj, PersistenceProperty p) {
		String name = p.getPropertyName();
		Object o = null;
		try {
			Field f = c.getDeclaredField(name);
			f.setAccessible(true);
			o = f.get(obj);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			throw new FieldAccessException("The field " + name
					+ " can't be accessed.");
		} catch (IllegalAccessException e) {
			throw new FieldAccessException("The field " + name
					+ " can't be accessed.");
		} catch (NoSuchFieldException e) {
			throw new cn.edu.nju.software.sd.torm.exception.NoSuchFieldException(
					"The field " + name + " can't be found.");
		}

		return o;
	}


	/* (non-Javadoc)
	 * @see cn.edu.nju.software.sd.torm.impl.DefaultPersistenceManager#setPropertyValue(java.lang.Class, java.lang.Object, cn.edu.nju.software.sd.torm.cfg.PersistenceProperty, java.lang.Object)
	 */
	protected void setPropertyValue(Class c, Object obj, PersistenceProperty p,
			Object value) {
		if (value == null)
			return;
		String name = p.getPropertyName();
		try {
			Field f = c.getDeclaredField(name);
			f.setAccessible(true);
			f.set(obj, value);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			throw new FieldAccessException("The field " + name
					+ " can't be accessed.");
		} catch (IllegalAccessException e) {
			throw new FieldAccessException("The field " + name
					+ " can't be accessed.");
		} catch (NoSuchFieldException e) {
			throw new cn.edu.nju.software.sd.torm.exception.NoSuchFieldException(
					"The field " + name + " can't be found.");
		}

	}
	

	/* (non-Javadoc)
	 * @see cn.edu.nju.software.sd.torm.impl.DefaultPersistenceManager#getConnection()
	 */
	protected Connection getConnection() throws SQLException {
		
		Map<String, String> cfg = configuration.getConfiguration();
		try {
			
			Class.forName(cfg.get(
					"torm.connection.driver_class").trim());
		} catch (ClassNotFoundException e) {
			throw new PersistenceException("Driver class "
					+ cfg.get("torm.connection.driver_class")
							.trim() + " not found.");
		}
		return java.sql.DriverManager.getConnection(cfg
				.get("torm.connection.url"), cfg
				.get("torm.connection.username"), cfg
				.get("torm.connection.password"));
	}

}

⌨️ 快捷键说明

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