mysql8.0以上版本注册驱动并建立数据库的连接公共代码
String driverName = "com.mysql.jdbc.Driver";
String userName = "用户名";
String userPwd = "密码";
String dbName = "数据库名";
String url1 = "jdbc:mysql://127.0.0.1:3306/" + dbName;
String url2 = "?user=" + userName + "&password=" + userPwd;
String url3 = "&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";//这个很关键
String url = url1 + url2 + url3;
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url);
数据池
<Context>
<Resource name = "jdbc/mysql">
type = "javax.sql.DataSource"
auth = "Container"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/数据库名字"
username = "用户名字"
password = "用户密码"
maxActive = "4"
maxIdle = "2"
maxWait = "6000"/>
</Context>