JDBC獲取連接時的時間問題
附源代碼:
Connection conn=null; PreparedStatement psPreparedStatement=null; ResultSet rs=null; try { Class.forName("com.mysql.cj.jdbc.Driver");//注意要引入外來mysql包 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hanxiao","root",""); String sql="select job,sal from emp where sal=?"; psPreparedStatement=conn.prepareStatement(sql); psPreparedStatement.setString(1, "800.00"); rs=psPreparedStatement.executeQuery();
出現的問題:
java.sql.SQLException: 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. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
出現這個問題是時區的問題,要么調整自己電腦的時間,或者在獲取連接的url后面加上serverTimezone=GMT,這樣就可以運行了
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hanxiao?serverTimerzone=GMT","root","");