package com.oracle.utils;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtils {
private static final String USER_NAME="root";
private static final String PASSWORD="root";
private static final String URL="jdbc:mysql://192.168.6.39:3306/test?useUnicode=true&characterEncoding=UTF-8";//连接字符串 ->连接的 地址 端口 实例名
private static final String DRIVER="com.mysql.jdbc.Driver";//数据库驱动
private static Connection conn=null;
/**
*
* 获取一个连接
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConn() throws SQLException, ClassNotFoundException{
if(conn==null||conn.isClosed()){
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL, USER_NAME, PASSWORD);
}
return conn;
}
/**
* conn关闭
* @throws SQLException
*/
public static void closeConn() throws SQLException{
if(conn!=null&&!conn.isClosed()){
conn.close();
}
}