1.使用properties配置文件
开发中获得连接的4个参数(驱动、URL、用户名、密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可。
通常情况下,我们习惯使用properties文件,此文件我们将做如下要求:
1. 文件位置:任意,建议src下
2. 文件名称:任意,扩展名为properties
3. 文件内容:一行一组数据,格式是“key=value”.
a) key命名自定义,如果是多个单词,习惯使用点分隔。例如:jdbc.driver
b) value值不支持中文,如果需要使用非英文字符,将进行unicode转换。
2.创建配置文件
在项目跟目录下,创建文件,输入“db.properties”文件名。
文件中的内容
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=root
3.加载配置文件:Properties对象
对应properties文件处理,开发中也使用Properties对象进行。我们将采用加载properties文件获得流,然后使用Properties对象进行处理。
JDBCUtils.java中编写代码
package cn.hiluna.jdbcutil;
/*
* 编写JDBC的工具类,获取数据库的连接
* 采用读取配置文件的方式
* 读取配置文件,获取连接,执行一次,static{}
*/
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Statement;
public class JDBCUtilsConfig {
private static Connection connection;
private static String driverClass;
private static String url;
private static String username;
private static String password;
static {
try {
readConfig();
Class.forName(driverClass);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
throw new RuntimeException("数据库连接失败");
}
}
private static void readConfig() throws Exception{
InputStream inputStream = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
Properties properties = new Properties();
properties.load(inputStream);
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
}
/*关闭数据库连接*/
public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
}
public static void close(Connection connection,Statement statement,ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
}
//关闭
public static void close(Connection connection,PreparedStatement preparedStatement) {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
}
public static void close(Connection connection,Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO: handle exception
}
}
}
public static Connection getConnection() {
return connection;
}
}