Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mysql8_bug;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.Statement;
- import java.sql.Types;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.TimeZone;
- /**
- ### On my machine (ubuntu 18.04 x86_64):
- $date +"%Z %z" ----> CET +0100
- mysqld --version ----> /usr/sbin/mysqld Ver 8.0.15 for Linux on x86_64 (MySQL Community Server - GPL)
- ### JDBC Driver:
- mysql-connector-java-8.0.15.jar
- ### TABLE Structure
- -- the bug is the same also with DATE data type
- CREATE TABLE `test001` (
- `date` datetime DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- ### MY OUTPUT (serverTimezone no set):
- TimeZone DisplayName: Ora dell'Europa centrale
- TimeZone ID: Europe/Rome
- TimeZone: sun.util.calendar.ZoneInfo[id="Europe/Rome",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=169,lastRule=java.util.SimpleTimeZone[id=Europe/Rome,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
- ------------
- row 0) @@global.time_zone: SYSTEM
- row 0) @@session.time_zone: SYSTEM
- row 0) @@system_time_zone: CET
- ------------
- row 0) date: 1970-07-28 01:00:00 <--- ERROR
- row 1) date: 1977-04-25 23:00:00 <--- ERROR
- row 2) date: 1966-08-04 01:00:00 <--- ERROR
- row 3) date: 1969-06-01 01:00:00 <--- ERROR
- row 4) date: 1982-07-23 00:00:00
- ### MY OUTPUT (serverTimezone=Europe/Rome):
- TimeZone DisplayName: Ora dell'Europa centrale
- TimeZone ID: Europe/Rome
- TimeZone: sun.util.calendar.ZoneInfo[id="Europe/Rome",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=169,lastRule=java.util.SimpleTimeZone[id=Europe/Rome,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
- ------------
- row 0) @@global.time_zone: SYSTEM
- row 0) @@session.time_zone: SYSTEM
- row 0) @@system_time_zone: CET
- ------------
- row 0) date: 1970-07-29 00:00:00
- row 1) date: 1977-04-26 00:00:00
- row 2) date: 1966-08-05 00:00:00
- row 3) date: 1969-06-01 01:00:00 <--- ERROR
- row 4) date: 1982-07-23 00:00:00
- */
- public class Mysql8_bug {
- public static void main(String[] args) throws Exception{
- // print java TimeZone Info
- System.out.println("TimeZone DisplayName: "+TimeZone.getDefault().getDisplayName());
- System.out.println("TimeZone ID: "+TimeZone.getDefault().getID());
- System.out.println("TimeZone: "+TimeZone.getDefault());
- System.out.println(" ------------ ");
- Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sampledb?" +
- "user=root&password=YOUR_PASSWORD_HERE"
- +"&serverTimezone=Europe/Rome"
- // +"&serverTimezone=UTC"
- );
- Statement stmt = conn.createStatement();
- printRs(stmt.executeQuery("SELECT @@global.time_zone, @@session.time_zone, @@system_time_zone;"));
- System.out.println(" ------------ ");
- stmt.executeUpdate("delete from test001");
- PreparedStatement pstmt = conn.prepareStatement("insert into test001 (date) values (?)");
- pstmt.setDate(1, parseDate("1970-07-29"));pstmt.executeUpdate();
- pstmt.setDate(1, parseDate("1977-04-26"));pstmt.executeUpdate();
- pstmt.setDate(1, parseDate("1966-08-05"));pstmt.executeUpdate();
- pstmt.setDate(1, parseDate("1969-06-01"));pstmt.executeUpdate(); // problem also with serverTimezone=Europe/Rome
- pstmt.setDate(1, parseDate("1982-07-23"));pstmt.executeUpdate();// should be not affected
- printRs(stmt.executeQuery("select date from test001"));
- stmt.close();
- conn.close();
- }
- static public void printRs(ResultSet rs) throws Exception{
- ResultSetMetaData m = rs.getMetaData();
- int cols = m.getColumnCount();
- int row = 0;
- while(rs.next()){
- for(int i = 1; i<=cols; i++){
- String val;
- if(m.getColumnType(i) == Types.DATE || m.getColumnType(i) == Types.TIMESTAMP){
- val = formatDate(rs.getDate(i));
- }else{
- val = rs.getString(i);
- }
- System.out.println("row "+row+") "+m.getColumnName(i)+": "+val);
- }
- row++;
- }
- rs.close();
- }
- static public String formatDate(java.sql.Date d) throws ParseException{
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return df.format(d);
- }
- static public String formatDate(Date d) throws ParseException{
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return df.format(d);
- }
- static public java.sql.Date parseDate(String s) throws ParseException{
- SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
- Date d1 = d.parse(s);
- java.sql.Date d2 = new java.sql.Date(d1.getTime());
- //System.out.println("d1: "+formatDate(d1));
- //System.out.println("d2: "+formatDate(d2));
- return d2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement