一、工具类DBUtil——数据库连接
时间:2021-07-01 10:21:17
帮助过:1136人阅读
util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
* 数据库连接工具
*/
public class DBUtil {
static String ip = "127.0.0.1"
;
static int port = 3306
;
static String database = "tmall";
//此处可灵活修改
static String encoding = "UTF-8"
;
static String loginName = "root";
//此处可灵活修改
static String password = "admin";
//此处可灵活修改
static {
try {
Class.forName("com.mysql.jdbc.Driver"
);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection()
throws SQLException {
String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s"
, ip, port, database, encoding);
return DriverManager.getConnection(url, loginName, password);
}
//测试用例
public static void main(String[] args)
throws SQLException {
System.out.println(getConnection());
}
}
一、工具类DBUtil——数据库连接
标签:word mys 数据库 初始 ack odi dbutil 数据库连接 driver