日期:2014-05-16 浏览次数:20514 次
private static Connection conn = null;
public static Connection getConnection() {
if (conn == null) {
String url = "jdbc:sqlite:test.db";
try {
synchronized (this) {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(url);
}
} catch (ClassNotFoundException e) {
conn = null;
} catch (SQLException e) {
conn = null;
}
}
return conn;
}
public static void close(Connection conn) {
// 因为connection只有一个,所以不真正关闭
/*
* if (conn != null) { try { conn.close(); } catch (SQLException e) {
*
* } conn = null; }
*/
}
private static DataSource dataSource;
public static Connection getConnection() {
if (dataSource == null) {
buildDataSource();
}
try {
return dataSource.getConnection();
} catch (SQLException e) {
return null;
}
}
private static synchronized void buildDataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.sqlite.JDBC");
ds.setUrl("jdbc:sqlite:test.db");
dataSource = ds;
}