数据库连接池都要实现DataSource接口
driverClassName=com.MySQL.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbc username=root passWord=
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
defaultReadOnly=
defaultTransactionIsolation=READ_COMMITTED
private static DataSource ds=null; static{ try{ InputStream in=JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); Properties prop=new Properties(); prop.load(in); BasicDataSourceFactory factory=new BasicDataSourceFactory(); ds=factory.createDataSource(prop); }catch(Exception e){ throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException{ return ds.getConnection(); }配置文件:
使用方法:
private static ComboPooledDataSource ds = null; static{ try{ ds = new ComboPooledDataSource(); }catch (Exception e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException{ return ds.getConnection(); }Jndi容器
配置文件:
<Context> <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/day16" initialSize="10" maxActive="30" maxIdle="4"/></Context>不要忘记将数据库驱动JAR包加入到Tomcat的lib目录中 使用方法:
private static DataSource ds; static { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); } catch (Exception e) { throw new RuntimeException(e); } } public static Connection getConnection() throws SQLException{ return ds.getConnection(); }元数据:数据库,表,列的定义信息。 Connection.getDatabaseMetaData();
PreparedStatement.getParameterMetaData();//获得代表PreparedStatement元数据的ParameterMetaData对象
ResultSet.getMetaData();//获得代表ResultSet对象元数据的ResultSetMetaData对象。
public class JdbcUtils { private static DataSource ds = null; static{ try{ InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); Properties prop = new Properties(); prop.load(in); BasicDataSourceFactory factory = new BasicDataSourceFactory(); ds = factory.createDataSource(prop); }catch (Exception e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException{ return ds.getConnection(); } public static void release(Connection conn,Statement st,ResultSet rs){ if(rs!=null){ try{ rs.close(); //throw new }catch (Exception e) { e.printStackTrace(); } rs = null; } if(st!=null){ try{ st.close(); }catch (Exception e) { e.printStackTrace(); } st = null; } if(conn!=null){ try{ conn.close(); }catch (Exception e) { e.printStackTrace(); } } } //String sql = "insert into account(id,name,money) values(?,?,?)" object[]{1,"aaa","10000"}; public static void update(String sql,Object params[]) throws SQLException{ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = getConnection(); st = conn.prepareStatement(sql); for(int i=0;i<params.length;i++){ st.setObject(i+1,params[i]); } st.executeUpdate(); }finally{ release(conn, st, rs); } } // public static Object query(String sql,Object params[],ResultSetHandler handler) throws SQLException{ Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try{ conn = getConnection(); st = conn.prepareStatement(sql); for(int i=0;i<params.length;i++){ st.setObject(i+1,params[i]); } rs = st.executeQuery(); return handler.handler(rs); }finally{ release(conn, st, rs); } }} interface ResultSetHandler{ public Object handler(ResultSet rs);}class BeanHandler implements ResultSetHandler{ private Class clazz; public BeanHandler(Class clazz){ this.clazz = clazz; } public Object handler(ResultSet rs) { try{ if(!rs.next()){ return null; } //创建封装结果集的bean Object bean = clazz.newInstance(); //得到结果集的元数据,以获取结果集的信息 ResultSetMetaData meta = rs.getMetaData(); int count = meta.getColumnCount(); for(int i=0;i<count;i++){ String name = meta.getColumnName(i+1); //获取到结果集每列的列名 id Object value = rs.getObject(name); //1 //反射出bean上与列名相应的属性 Field f = bean.getClass().getDeclaredField(name); f.setaccessible(true); f.set(bean, value); } return bean; }catch (Exception e) { throw new RuntimeException(e); } }}class BeanListHandler implements ResultSetHandler{ private Class clazz; public BeanListHandler(Class clazz){ this.clazz = clazz; } public Object handler(ResultSet rs) { List list = new ArrayList(); try{ while(rs.next()){ Object bean = clazz.newInstance(); ResultSetMetaData meta = rs.getMetaData(); int count = meta.getColumnCount(); for(int i=0;i<count;i++){ String name = meta.getColumnName(i+1); Object value = rs.getObject(name); Field f = bean.getClass().getDeclaredField(name); f.setAccessible(true); f.set(bean, value); } list.add(bean); } }catch (Exception e) { throw new RuntimeException(e); } return list; }}新闻热点
疑难解答