package
com.yitong.cn.dao;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.SQLException;
public
class
ConnectionDB {
private
static
final
String DBDRIVER =
"com.mysql.jdbc.Driver"
;
private
static
final
String DBUSER =
"root"
;
private
static
final
String DBPASSWORD =
"mysqladmin"
;
private
static
final
String DBURL =
"jdbc:mysql://localhost:3306/guoxinglei"
;
private
static
Connection conn ;
//定义全局变量
public
static
Connection getConnection(){
try
{
// 加载驱动程序
Class.forName(DBDRIVER);
// 连续数据库
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
}
catch
(SQLException e) {
e.printStackTrace();
}
return
conn;
}
public
static
void
closeConnection(Connection conn){
if
(conn!=
null
){
try
{
conn.close();
}
catch
(Exception e) {
}
}
}
public
static
void
main(String[] args) {
Connection conn = ConnectionDB.getConnection();
System.out.println(conn);
if
(conn!=
null
){
System.out.println(
"数据库连接成功"
);
ConnectionDB.closeConnection(conn);
}
}
}