日期:2014-05-20 浏览次数:20923 次
/** * FileName: ClazzeDao.java * CreationTime: 2011-8-14 * Author: yjd * EMail: 908599713@qq.com * Site: http://hi.csdn.net/tjcyjd */ package com.tjcyjd.dao; /** * 班级接口 * * @author yjd */ public interface ClazzeDao { /** 根据id删除对应的班级 */ public void deleteClazzeByClazzeId(int clazzeId) throws DaoException; }
/** * FileName: StudentDao.java * CreationTime: 2011-8-14 * Author: yjd * EMail: 908599713@qq.com * Site: http://hi.csdn.net/tjcyjd */ package com.tjcyjd.dao; /** * 学生接口 * * @author yjd */ public interface StudentDao { /** 根据班级id删除该班级下的所有学生 */ public void deleteStudentByClazzeId(int clazzeId) throws DaoException; }
/** * FileName: ConnectionFactory.java * CreationTime: 2011-8-14 * Author: yjd * EMail: 908599713@qq.com * Site: http://hi.csdn.net/tjcyjd */ package com.tjcyjd.commom; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import com.mchange.v2.c3p0.DataSources; /** * 数据库连接工厂 * * @author yjd */ public class ConnectionFactory { private static Properties prop = new Properties(); // 数据源 private static DataSource ds = null; // 用来把数据库连接绑定到当前线程上的变量 private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static { try { prop.load(Thread.currentThread().getContextClassLoader() .getResourceAsStream("jdbc.properties")); } catch (IOException e) { e.printStackTrace(); System.out.println("在classpath下没有找到jdbc.properties文件"); } // 这里使用的是c3p0连接 try { Class.forName("com.mysql.jdbc.Driver"); DataSource unpooled = DataSources.unpooledDataSource(prop .getProperty("url"), prop.getProperty("user"), prop .getProperty("password")); ds = DataSources.pooledDataSource(unpooled); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /** * 获取数据库的Connection对象 * * @return */ public static synchronized Connection getConnection() { Connection conn = tl.get(); // 当前线程取出连接实例 if (null == conn) { try { conn = ds.getConnection(); // 从连接池中取出一个连接实例 tl.set(conn); // 把它绑定到当前线程上 } catch (SQLException e) { e.printStackTrace(); } } return conn; } public static synchronized TransactionManager getTranManager() { return new TransactionManager(getConnection()); } }