使用properties类配置JDBC
Properties类代码:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/Demo?characterEncoding=utf8
user=root
password=123456
JDBCUtils代码:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
//获取connection对象
public static Connection getConn(){
Connection conn=null;
try {
//创建Properties集合
Properties pro=new Properties();
//明确数据源
FileReader fr=new FileReader("src/com/oracle/tools/db.properties");
//将Properties文件中的数据读取到Properties集合中
pro.load(fr);
//1.注册驱动
Class.forName(pro.getProperty("driver"));
//2.获取连接对象
String url=pro.getProperty("url");
String user=pro.getProperty("user");
String password=pro.getProperty("password");
conn=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//增删改释放资源
public static void close(Connection conn,Statement pst){
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询释放资源
public static void close(Connection conn,Statement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//获取connection对象
public static Connection getConn(){
Connection conn=null;
try {
//创建Properties集合
Properties pro=new Properties();
//明确数据源
FileReader fr=new FileReader("src/com/oracle/tools/db.properties");
//将Properties文件中的数据读取到Properties集合中
pro.load(fr);
//1.注册驱动
Class.forName(pro.getProperty("driver"));
//2.获取连接对象
String url=pro.getProperty("url");
String user=pro.getProperty("user");
String password=pro.getProperty("password");
conn=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//增删改释放资源
public static void close(Connection conn,Statement pst){
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询释放资源
public static void close(Connection conn,Statement pst,ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
posted on 2020-04-24 11:50 煎饼果子不要果子谢谢 阅读(247) 评论(0) 编辑 收藏 举报