JDBC连接数据库工具类的提取
JDBC连接数据库工具类
因为传统JDBC的开发,注册驱动,获得连接,释放资源这些代码都是重复编写的。所以可以将重复的代码提取到一个类中。
该类需要将MySQL的jar包导入我们的开发工具中去。
JDBC工具类:
import java.sql.*;
/**
* JDBC 工具类
*/
public class JDBCUtils {
private static final String driverClassName;
private static final String url;
private static final String username;
private static final String password;
static {
driverClassName = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://192.168.4.192/wdp-database";
username = "root";
password = "root";
}
/*
注册驱动的方法
*/
public static void loadDriver() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*
获得连接的方法
*/
public static Connection getConnection(){
Connection conn = null;
try {
//将驱动一起注册
loadDriver();
//获得连接
conn = DriverManager.getConnection(url, username, password);
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
/*
释放资源的方法
*/
public static void release(Statement stmt,Connection conn){
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
JDBC工具测试类:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCdemo02 {
/*
JDBC 的工具类 测试类
*/
/*
查询操作使用工具类
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt =null;
ResultSet rs = null;
try{
//获得连接
conn = JDBCUtils.getConnection();
//创建执行SQL语句的对象
stmt = conn.createStatement();
//编写SQL
String sql = "select * from classes";
//执行查询
rs = stmt.executeQuery(sql);
//遍历结果集
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("cname")+" "+rs.getInt("cnum"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
//释放资源
JDBCUtils.release(rs,stmt,conn);
}
}
}
JDBC连接数据库工具类提取到配置文件
配置文件:
属性文件:
格式:扩展名是.properties
内容:key=value
xml文件
提取信息到配置文件:
db.properties文件
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql:///web_test3
username=root
password= abc
在工具类中解析属性文件
获取到具体内容为常量赋值
JDBC工具类;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* JDBC 工具类
*/
public class JDBCUtils {
private static final String driverClassName;
private static final String url;
private static final String username;
private static final String password;
static {
//获取属性文件中的内容
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src/db.properties"));
} catch (IOException e) {
e.printStackTrace();
}
driverClassName = properties.getProperty("driverClassName");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
/*
注册驱动的方法
*/
public static void loadDriver() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/*
获得连接的方法
*/
public static Connection getConnection(){
Connection conn = null;
try {
//将驱动一起注册
loadDriver();
//获得连接
conn = DriverManager.getConnection(url, username, password);
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
/*
释放资源的方法
*/
public static void release(Statement stmt,Connection conn){
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
可以用上面的测试类实现对该工具类的测试!