jdbc中如何使用dbcp
答案:2 悬赏:70 手机版
解决时间 2021-02-01 12:55
- 提问者网友:ミ烙印ゝ
- 2021-02-01 08:21
jdbc中如何使用dbcp
最佳答案
- 五星知识达人网友:持酒劝斜阳
- 2021-02-01 08:30
直接使用的情况,真不多。
一般是和框架配合使用,这样配置一下就完成了。
直接使用
public final class JdbcUtils {
private static DataSource myDataSource = null;
private JdbcUtils() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
// myDataSource = new MyDataSource2();
Properties prop = new Properties();
// prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");
// prop.setProperty("user", "user");
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
prop.load(is);
myDataSource = BasicDataSourceFactory.createDataSource(prop);//创建数据源,一句代码就完成
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource() {
return myDataSource;
}
public static Connection getConnection() throws SQLException {
// return DriverManager.getConnection(url, user, password);
return myDataSource.getConnection();
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
// myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 测试
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
Connection conn = JdbcUtils.getConnection();
System.out.println(conn);
JdbcUtils.free(null, null, conn);
}
}
}
一般是和框架配合使用,这样配置一下就完成了。
直接使用
public final class JdbcUtils {
private static DataSource myDataSource = null;
private JdbcUtils() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");
// myDataSource = new MyDataSource2();
Properties prop = new Properties();
// prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");
// prop.setProperty("user", "user");
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
prop.load(is);
myDataSource = BasicDataSourceFactory.createDataSource(prop);//创建数据源,一句代码就完成
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static DataSource getDataSource() {
return myDataSource;
}
public static Connection getConnection() throws SQLException {
// return DriverManager.getConnection(url, user, password);
return myDataSource.getConnection();
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
// myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 测试
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
Connection conn = JdbcUtils.getConnection();
System.out.println(conn);
JdbcUtils.free(null, null, conn);
}
}
}
全部回答
- 1楼网友:洎扰庸人
- 2021-02-01 09:04
datasource ds=(datasource)ctx.lookup("java:comp/env/jdbc");
datasource ds=(datasource)ctx.lookup("java:/comp/env/jdbc");
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯