jar包


连接数据库配置文件



创建DBUtil
package com.zyd.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DBUtil {
/**
* 得到数据库连接
* @return
*/
public static Connection getConnection(){
Connection connection = null;
try {
//加载配置文件,字节输入流注入配置文件,路径在classpath下
InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
//得到属性文件
Properties properties = new Properties();
//将输入流加载到属性文件对象中,通过properties对象的load()方法
properties.load(inputStream);
//通过properties的getProperty()方法得到属性文件中的value
String jdbcName = properties.getProperty("jdbcName");
String url = properties.getProperty("url");
String dbName = properties.getProperty("dbName");
String dbPwd = properties.getProperty("dbPwd");
//加载驱动
Class.forName(jdbcName);
//得到数据库连接
connection = DriverManager.getConnection(url,dbName,dbPwd);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){
//判断对象不为空,并关闭对象
try {
if (resultSet!= null) {
resultSet.close();
}
if (preparedStatement !=null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection connection = DBUtil.getConnection();
System.out.println(connection);
}
}

连接成功
















