Advertisement
Guest User

Mysql8 JDBC Date bug

a guest
Mar 29th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mysql8_bug;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.ResultSetMetaData;
  13. import java.sql.Statement;
  14. import java.sql.Types;
  15. import java.text.ParseException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.TimeZone;
  19.  
  20.  
  21. /**
  22.  
  23.   ### On my machine (ubuntu 18.04 x86_64):  
  24.  
  25.      $date +"%Z %z"      ---->    CET +0100
  26.      mysqld --version    ---->    /usr/sbin/mysqld  Ver 8.0.15 for Linux on x86_64 (MySQL Community Server - GPL)
  27.  
  28.  
  29.   ### JDBC Driver:
  30.  
  31.       mysql-connector-java-8.0.15.jar
  32.  
  33.  
  34.   ### TABLE Structure
  35.  
  36.     -- the bug is the same also with DATE data type
  37.  
  38.     CREATE TABLE `test001` (
  39.           `date` datetime DEFAULT NULL
  40.       ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  41.  
  42.  
  43.  
  44.  
  45.  ### MY OUTPUT (serverTimezone no set):
  46.  
  47.     TimeZone DisplayName: Ora dell'Europa centrale
  48.     TimeZone ID: Europe/Rome
  49.     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]]
  50.      ------------
  51.     row 0) @@global.time_zone: SYSTEM
  52.     row 0) @@session.time_zone: SYSTEM
  53.     row 0) @@system_time_zone: CET
  54.      ------------
  55.     row 0) date: 1970-07-28 01:00:00  <--- ERROR
  56.     row 1) date: 1977-04-25 23:00:00  <--- ERROR
  57.     row 2) date: 1966-08-04 01:00:00  <--- ERROR
  58.     row 3) date: 1969-06-01 01:00:00  <--- ERROR
  59.     row 4) date: 1982-07-23 00:00:00
  60.  
  61.  
  62.  ### MY OUTPUT (serverTimezone=Europe/Rome):
  63.  
  64.     TimeZone DisplayName: Ora dell'Europa centrale
  65.     TimeZone ID: Europe/Rome
  66.     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]]
  67.      ------------
  68.     row 0) @@global.time_zone: SYSTEM
  69.     row 0) @@session.time_zone: SYSTEM
  70.     row 0) @@system_time_zone: CET
  71.      ------------
  72.     row 0) date: 1970-07-29 00:00:00
  73.     row 1) date: 1977-04-26 00:00:00
  74.     row 2) date: 1966-08-05 00:00:00
  75.     row 3) date: 1969-06-01 01:00:00  <--- ERROR
  76.     row 4) date: 1982-07-23 00:00:00
  77.  
  78.  
  79.  
  80.  */
  81. public class Mysql8_bug {
  82.  
  83.  
  84.     public static void main(String[] args) throws Exception{
  85.        
  86.  
  87.         // print java TimeZone Info
  88.         System.out.println("TimeZone DisplayName: "+TimeZone.getDefault().getDisplayName());
  89.         System.out.println("TimeZone ID: "+TimeZone.getDefault().getID());
  90.         System.out.println("TimeZone: "+TimeZone.getDefault());
  91.         System.out.println(" ------------ ");
  92.        
  93.        
  94.         Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  95.        
  96.         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sampledb?" +
  97.                                    "user=root&password=YOUR_PASSWORD_HERE"
  98.                                    +"&serverTimezone=Europe/Rome"    
  99.                                    // +"&serverTimezone=UTC"  
  100.              );
  101.        
  102.        
  103.        
  104.         Statement stmt = conn.createStatement();
  105.        
  106.         printRs(stmt.executeQuery("SELECT @@global.time_zone, @@session.time_zone, @@system_time_zone;"));
  107.         System.out.println(" ------------ ");
  108.        
  109.         stmt.executeUpdate("delete from test001");
  110.        
  111.         PreparedStatement pstmt = conn.prepareStatement("insert into test001 (date) values (?)");
  112.        
  113.         pstmt.setDate(1, parseDate("1970-07-29"));pstmt.executeUpdate();
  114.         pstmt.setDate(1, parseDate("1977-04-26"));pstmt.executeUpdate();
  115.         pstmt.setDate(1, parseDate("1966-08-05"));pstmt.executeUpdate();
  116.         pstmt.setDate(1, parseDate("1969-06-01"));pstmt.executeUpdate(); // problem also with serverTimezone=Europe/Rome
  117.  
  118.         pstmt.setDate(1, parseDate("1982-07-23"));pstmt.executeUpdate();// should be not affected
  119.        
  120.      
  121.        
  122.        
  123.         printRs(stmt.executeQuery("select date from test001"));
  124.        
  125.        
  126.        
  127.        
  128.        
  129.        
  130.  
  131.        
  132.        
  133.        
  134.         stmt.close();
  135.         conn.close();
  136.        
  137.        
  138.        
  139.        
  140.     }
  141.  
  142.    
  143.    
  144.     static public void printRs(ResultSet rs) throws Exception{
  145.         ResultSetMetaData m = rs.getMetaData();
  146.         int cols = m.getColumnCount();
  147.         int row = 0;
  148.         while(rs.next()){
  149.             for(int i = 1; i<=cols; i++){
  150.                
  151.                 String val;
  152.                 if(m.getColumnType(i) == Types.DATE || m.getColumnType(i) == Types.TIMESTAMP){
  153.                     val = formatDate(rs.getDate(i));
  154.                 }else{
  155.                     val = rs.getString(i);
  156.                 }
  157.                 System.out.println("row "+row+") "+m.getColumnName(i)+": "+val);
  158.             }
  159.             row++;
  160.         }
  161.         rs.close();
  162.     }    
  163.     static public String formatDate(java.sql.Date d) throws ParseException{
  164.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  165.         return df.format(d);
  166.     }
  167.     static public String formatDate(Date d) throws ParseException{
  168.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  169.         return df.format(d);
  170.     }
  171.    
  172.    
  173.     static public java.sql.Date parseDate(String s) throws ParseException{
  174.         SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
  175.        
  176.         Date d1 = d.parse(s);
  177.         java.sql.Date d2 = new java.sql.Date(d1.getTime());
  178.        
  179.         //System.out.println("d1: "+formatDate(d1));
  180.         //System.out.println("d2: "+formatDate(d2));
  181.        
  182.         return d2;
  183.     }
  184.    
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement