jsp连接mysql数据库报时区错误

<%  
//驱动程序名   
String driverName = "com.mysql.jdbc.Driver";  
//数据库用户名   
String userName = "root";  
//密码   
String userPasswd = "1";  
//数据库名   
String dbName = "test";  
//表名   
String tableName = "user";  
//联结字符串   
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="  
        + userName + "&password=" + userPasswd ;  
Class.forName(driverName).newInstance();  
Connection connection = DriverManager.getConnection(url);  
Statement statement = connection.createStatement(); 

String sql = "SELECT * FROM " + tableName;  
ResultSet rs = statement.executeQuery(sql);  

        while(rs.next()) {%>
        	数据库“user”表中字段1: <%=rs.getString(1)%><br />
       		 数据库“user”表中字段2: <%=rs.getString(2)%><br />
         	 数据库“user”表中字段3: <%=rs.getString(3)%><br />
         	 <br />
        <%}%>
        <hr />
        <%out.print("数据库操作成功,恭喜你");%><br />
        <%rs.close();
        statement.close();
        connection.close();
    %>

jsp链接mysql数据库的代码如上所示:

调试的时候报错

错误提示如下:

Stacktrace:] with root cause

com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

我用的是mysql-connector-java-6.0.3-bin,百度之后说换了个 5.1.18 OK(我没有试过),也可以在连接的url后加一个参数 serverTimezone=GMT (加了这个参数之后就能顺利链接数据库了),这里的时区可以根据自己数据库的设定来设置。

之后还可能会出现另一个错误,mysql新的安全性设置要求SSL连接,此处可以加一个参数userSSL=false,或者自己设置SSL也可以 

另外,6.0.2版本的driverClassName不再是原来的路径,改成com.mysql.cj.jdbc.Driver了


猜你喜欢

转载自blog.csdn.net/sinat_33201781/article/details/51830688