hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package frame.persist;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class DBDate {
 
    private static DataBaseType dbType;
    private Date date;
    
    static {
        dbType = DataBaseType.MySQL;
    }
 
    public DBDate() {
        date = new Date();
    }
    
    public DBDate(Date date) {
        this.date = date;
    }
    
    public String toSQLString() throws Exception {
        String result = null;
        
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String value = dateFormat.format(date);
        
        if (DataBaseType.Oracle == dbType) {
            result = "to_date('" + value + "','YYYY-MM-DD')";
        }
        else if (DataBaseType.SQLServer == dbType) {
            result = "('" + value + "')";
        }
        else if (DataBaseType.MySQL == dbType) {
            result = "('" + value + "')";
        }
        else {
            throw new Exception("unknown database type");
        }
        
        return result;
    }
    
}