DruidDataSource 는 연결 을 구축 하여 원생 JDBC 작업 데이터 베 이 스 를 실현 합 니 다.
1375 단어 자바 클래스
public class DbHelper {
private static DbHelper instance = null;
private DbHelper() {
}
/**
* @return
* @Description:
* @date 2017 7 28
*/
public synchronized static DbHelper getInstance() {
if (instance == null) {
instance = new DbHelper();
}
return instance;
}
/**
* @return com.alibaba.druid.pool.DruidDataSource
* @Description:
* @date 2017 8 2
*/
public com.alibaba.druid.pool.DruidDataSource getDbPool() {
com.alibaba.druid.pool.DruidDataSource dataSource = SpringUtils.getBean("dataSource", com.alibaba.druid.pool.DruidDataSource.class);
return dataSource;
}
public DruidPooledConnection getConnection() {
DruidPooledConnection conn = null;
try {
conn = getDbPool().getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
이렇게 하면 우 리 는 연결 을 얻 을 수 있 고 JDBC 를 통 해 데이터 베 이 스 를 조작 할 수 있다.
Connection connection = DbHelper.getInstance().getConnection();