连接工厂
程序员文章站
2022-07-13 13:41:58
...
//JDBC编程练习
public class DBHandle {
/** 驱动类 */
private static String SQL_DRIVER = "";
/** 数据库连接串 */
private static String SQL_URL = "";
/** 用户名 */
private static String SQL_USERNAME = "";
/** 密码 */
private static String SQL_PASSWORD = "";
private static Connection connection = null;
// 加载初始化Connection对象
static {
try {
getPropertiesInfo();
Class.forName(SQL_DRIVER);
connection = DriverManager.getConnection(SQL_URL, SQL_USERNAME,
SQL_PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 私有的构造器
*/
private DBHandle() {
super();
// TODO Auto-generated constructor stub
}
/**
* 从配置文件获取连接属性
*/
private static void getPropertiesInfo() {
try {
FileInputStream fis = new FileInputStream(
"D:\\config\\db.properties");
Properties prop = new Properties();
// 获得相应的键值对
prop.load(fis);
// 根据相应的键获得对应的值
SQL_DRIVER = prop.getProperty("driver");
SQL_URL = prop.getProperty("url");
SQL_USERNAME = prop.getProperty("username");
SQL_PASSWORD = prop.getProperty("password");
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午08:58:07
* @Description 该方法用来加载驱动,并获得数据库的连接对象
* @return
*/
public static Connection getConnection() {
try {
if (connection == null || connection.equals(null)
|| connection.isClosed()) {
Class.forName(SQL_DRIVER);
connection = DriverManager.getConnection(SQL_URL, SQL_USERNAME,
SQL_PASSWORD);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:27
* @Description 关闭
* @param rs
* @param ps
*/
@SuppressWarnings("null")
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement) {
try {
if (resultSet != null || !(resultSet.isClosed()))
resultSet.close();
if (preparedStatement != null || !(preparedStatement.isClosed()))
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:19
* @Description Description
* @param rs
* @param ps
* @param connection
*/
@SuppressWarnings("null")
public static void close(PreparedStatement preparedStatement,
Connection connection) {
try {
if (preparedStatement != null || !(preparedStatement.isClosed()))
preparedStatement.close();
if (connection != null || !(connection.isClosed()))
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:19
* @Description Description
* @param rs
* @param ps
* @param connection
*/
@SuppressWarnings("null")
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement, Connection connection) {
try {
close(resultSet, preparedStatement);
if (connection != null || !(connection.isClosed()))
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
附件是源码!
public class DBHandle {
/** 驱动类 */
private static String SQL_DRIVER = "";
/** 数据库连接串 */
private static String SQL_URL = "";
/** 用户名 */
private static String SQL_USERNAME = "";
/** 密码 */
private static String SQL_PASSWORD = "";
private static Connection connection = null;
// 加载初始化Connection对象
static {
try {
getPropertiesInfo();
Class.forName(SQL_DRIVER);
connection = DriverManager.getConnection(SQL_URL, SQL_USERNAME,
SQL_PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 私有的构造器
*/
private DBHandle() {
super();
// TODO Auto-generated constructor stub
}
/**
* 从配置文件获取连接属性
*/
private static void getPropertiesInfo() {
try {
FileInputStream fis = new FileInputStream(
"D:\\config\\db.properties");
Properties prop = new Properties();
// 获得相应的键值对
prop.load(fis);
// 根据相应的键获得对应的值
SQL_DRIVER = prop.getProperty("driver");
SQL_URL = prop.getProperty("url");
SQL_USERNAME = prop.getProperty("username");
SQL_PASSWORD = prop.getProperty("password");
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午08:58:07
* @Description 该方法用来加载驱动,并获得数据库的连接对象
* @return
*/
public static Connection getConnection() {
try {
if (connection == null || connection.equals(null)
|| connection.isClosed()) {
Class.forName(SQL_DRIVER);
connection = DriverManager.getConnection(SQL_URL, SQL_USERNAME,
SQL_PASSWORD);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:27
* @Description 关闭
* @param rs
* @param ps
*/
@SuppressWarnings("null")
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement) {
try {
if (resultSet != null || !(resultSet.isClosed()))
resultSet.close();
if (preparedStatement != null || !(preparedStatement.isClosed()))
preparedStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:19
* @Description Description
* @param rs
* @param ps
* @param connection
*/
@SuppressWarnings("null")
public static void close(PreparedStatement preparedStatement,
Connection connection) {
try {
if (preparedStatement != null || !(preparedStatement.isClosed()))
preparedStatement.close();
if (connection != null || !(connection.isClosed()))
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @author hwzhang
* @date 2012-12-4 下午09:35:19
* @Description Description
* @param rs
* @param ps
* @param connection
*/
@SuppressWarnings("null")
public static void close(ResultSet resultSet,
PreparedStatement preparedStatement, Connection connection) {
try {
close(resultSet, preparedStatement);
if (connection != null || !(connection.isClosed()))
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
附件是源码!
下一篇: JavaWeb中重定向和转发小解