Przeglądaj źródła

add http, sql runner

zhzhenqin 5 lat temu
rodzic
commit
3aea89cfe4

+ 43 - 0
httpclient/FluentHttpClient.java

@@ -0,0 +1,43 @@
+<dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpcore</artifactId>
+      <version>4.4.10</version>
+      <scope>compile</scope>
+    </dependency>
+
+<dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+      <version>4.5.6</version>
+      <scope>compile</scope>
+    </dependency>
+
+<dependency>
+			<groupId>org.apache.httpcomponents</groupId>
+			<artifactId>httpmime</artifactId>
+			<version>4.5.6</version>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.httpcomponents</groupId>
+			<artifactId>fluent-hc</artifactId>
+			<version>4.5.6</version>
+		</dependency>
+
+
+String port = ConfigurationUtil.getUserConfigSingleValue("DataRelease", "Service", "Publish-Port");
+		String drUrl = "http://" + ip + ":" + port + "/datarelease/api/dr/meta/rowpermission";
+		try {
+			HttpResponse httpResponse = Request.Post(drUrl)
+					.bodyForm(Form.form().add("resApplyId", resApplyId).build())
+					.socketTimeout(3000)
+					.connectTimeout(3000)
+					.execute().returnResponse();
+			if (httpResponse.getStatusLine().getStatusCode() == 200) {
+				return "1";
+			} else {
+				return "-1";
+			}
+		} catch (IOException e) {
+			log.error(e.getMessage(), e);
+			return "-1";
+		}

+ 155 - 0
httpclient/HttpClientRequester.java

@@ -0,0 +1,155 @@
+package com.sdyc.jise.fetch.crawler.requester;
+
+import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;
+import cn.edu.hfut.dmic.webcollector.net.HttpResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.Header;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.URI;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * <pre>
+ *
+ * Created by zhenqin.
+ * User: zhenqin
+ * Date: 17/4/25
+ * Time: 16:26
+ * Vendor: NowledgeData
+ * To change this template use File | Settings | File Templates.
+ *
+ * </pre>
+ *
+ * @author zhenqin
+ */
+@Slf4j
+public class HttpClientRequester extends JavaNativeRequester implements Closeable {
+
+
+    protected final CloseableHttpClient httpClient;
+
+
+    final PoolingHttpClientConnectionManager cm;
+
+    public HttpClientRequester() {
+        try {
+            //设置协议http和https对应的处理socket链接工厂的对象
+            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+                    .register("http", PlainConnectionSocketFactory.INSTANCE)
+                    .register("https", new SSLConnectionSocketFactory(createIgnoreVerifySSL()))
+                    .build();
+
+            cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
+            httpClient = HttpClients.custom()
+                    .setConnectionManager(cm)
+                    .setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
+                    .build();
+        } catch (Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
+
+    /**
+     * 绕过验证
+     *
+     * @return
+     * @throws NoSuchAlgorithmException
+     * @throws KeyManagementException
+     */
+    public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
+        SSLContext sc = SSLContext.getInstance("SSLv3");
+
+        // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
+        X509TrustManager trustManager = new X509TrustManager() {
+            @Override
+            public void checkClientTrusted(
+                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
+                    String paramString) throws CertificateException {
+            }
+
+            @Override
+            public void checkServerTrusted(
+                    java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
+                    String paramString) throws CertificateException {
+            }
+
+            @Override
+            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                return null;
+            }
+        };
+
+        sc.init(null, new TrustManager[] { trustManager }, null);
+        return sc;
+    }
+
+    /**
+     * 覆盖父类的方法,使用 HttpClient 实现
+     * @param crawlDatum
+     * @return
+     * @throws Exception
+     */
+    @Override
+    public HttpResponse getResponse(CrawlDatum crawlDatum) throws Exception {
+        HttpGet get = new HttpGet(crawlDatum.url());
+        if(StringUtils.isNotBlank(cookie)) {
+            header.put("Cookie", cookie);
+        }
+
+        if(StringUtils.isNotBlank(userAgent)) {
+            header.put("User-Agent", userAgent);
+        }
+
+        if(!header.isEmpty()) {
+            for (Map.Entry<String, String> entry : header.entrySet()) {
+                get.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        URI url = new URI(crawlDatum.url());
+        org.apache.http.HttpResponse resp = httpClient.execute(new HttpGet(url));
+        HttpResponse response = new HttpResponse(url.toURL());
+        log.debug("fetch url {} return code {}", crawlDatum.url(), resp.getStatusLine().getStatusCode());
+        Header[] allHeaders = resp.getAllHeaders();
+        if(allHeaders != null) {
+            for (Header header : allHeaders) {
+                response.addHeader(header.getName(), header.getValue());
+            }
+        }
+        response.setNotFound(Objects.equals(404, resp.getStatusLine().getStatusCode()));
+        response.setRedirect(Objects.equals(302, resp.getStatusLine().getStatusCode()));
+        response.code(resp.getStatusLine().getStatusCode());
+        response.setHtml(EntityUtils.toString(resp.getEntity()));
+        EntityUtils.consumeQuietly(resp.getEntity());
+        return response;
+    }
+
+
+    @Override
+    public void close() throws IOException {
+        httpClient.close();
+        log.info("closed http client.");
+    }
+}

+ 143 - 0
httpclient/JavaNativeRequester.java

@@ -0,0 +1,143 @@
+package com.sdyc.jise.fetch.crawler.requester;
+
+import cn.edu.hfut.dmic.webcollector.model.CrawlDatum;
+import cn.edu.hfut.dmic.webcollector.net.HttpRequest;
+import cn.edu.hfut.dmic.webcollector.net.HttpResponse;
+import cn.edu.hfut.dmic.webcollector.net.Requester;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * <pre>
+ *
+ * Created by zhenqin.
+ * User: zhenqin
+ * Date: 17/4/25
+ * Time: 16:26
+ * Vendor: NowledgeData
+ * To change this template use File | Settings | File Templates.
+ *
+ * </pre>
+ *
+ * @author zhenqin
+ */
+public class JavaNativeRequester implements Requester {
+
+    /**
+     * Http Cookie
+     */
+    protected String cookie;
+
+    /**
+     * UserAgent
+     */
+    protected String userAgent;
+
+    /**
+     * 访问超时时间
+     */
+    protected int connectTimeout = -1;
+
+    /**
+     * Http Header
+     */
+    protected final Map<String, String> header = new HashMap<String, String>(5);
+
+    /**
+     * 日志系统
+     */
+    protected static Logger LOG = LoggerFactory.getLogger(JavaNativeRequester.class);
+
+
+
+    public JavaNativeRequester() {
+    }
+
+
+
+    @Override
+    public HttpResponse getResponse(CrawlDatum crawlDatum) throws Exception {
+        HttpRequest request = new HttpRequest(crawlDatum);
+        if(StringUtils.isNotBlank(cookie)) {
+            request.setCookie(cookie);
+        }
+
+        if(StringUtils.isNotBlank(userAgent)) {
+            request.setUserAgent(userAgent);
+        }
+
+        if(connectTimeout > 0) {
+            request.setTimeoutForConnect(connectTimeout);
+        }
+
+        if(!header.isEmpty()) {
+            for (Map.Entry<String, String> entry : header.entrySet()) {
+                request.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        LOG.info("fetch url: {}", crawlDatum.url());
+        HttpResponse response = null;
+        int retry = 0;
+        do {
+            try {
+                response = request.response();
+                break;
+            } catch (Exception e) {
+                retry++;
+                LOG.info("不知道是否IP发生切换,发送抓取异常, 稍等 " + (retry * 2) + "s ,进行重试。");
+                Thread.sleep(retry * 2 * 1000);
+                LOG.info("等待后,重试开始, 当前重试第 " + retry + " 次。");
+            }
+        } while(retry < 5);
+
+        if(retry >= 5){
+            response = new HttpResponse(new URL(crawlDatum.url()));
+            response.setNotFound(true);
+            response.setRedirect(false);
+            response.code(404);
+            response.setHtml("");
+        }
+
+        return response;
+    }
+
+
+    public String getCookie() {
+        return cookie;
+    }
+
+    public void setCookie(String cookie) {
+        this.cookie = cookie;
+    }
+
+    public String getUserAgent() {
+        return userAgent;
+    }
+
+    public void setUserAgent(String userAgent) {
+        this.userAgent = userAgent;
+    }
+
+    public int getConnectTimeout() {
+        return connectTimeout;
+    }
+
+    public void setConnectTimeout(int connectTimeout) {
+        this.connectTimeout = connectTimeout;
+    }
+
+    public Map<String, String> getHeader() {
+        return header;
+    }
+
+
+    public String addHeader(String key, String value) {
+        return header.put(key, value);
+    }
+}

+ 120 - 0
sqlrunner/QueryResult.java

@@ -0,0 +1,120 @@
+package com.primeton.dsp.dataservice.utils;
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ *
+ * 执行查询 SQL 输出的结果类。 返回查询行数、字段表头、数据行。
+ *
+ * <pre>
+ *
+ * Created by zhaopx.
+ * User: zhaopx
+ * Date: 2020-03-26
+ * Time: 10:20
+ *
+ * </pre>
+ *
+ * @author zhaopx
+ */
+public class QueryResult {
+
+	/**
+	 * 是否成功
+	 */
+	boolean success = true;
+
+	/**
+	 * 当前是否成功
+	 */
+	String message = "OK";
+
+
+	/**
+	 * 执行的 SQL
+	 */
+    String sql;
+
+    /**
+     * 执行查询结果集的行数
+     */
+    final int rows;
+
+
+    /**
+     * 表头,和类型
+     */
+    final List<Map<String, Object>> fields;
+
+
+    /**
+     * 数据集,多行
+     */
+    final List<Map<String, Object>> dataset;
+
+
+    public QueryResult(List<Map<String, Object>> fields, List<Map<String, Object>> dataset) {
+        this.fields = fields;
+        this.dataset = dataset;
+        this.rows = dataset.size();
+    }
+
+
+    public int getRows() {
+        return rows;
+    }
+
+    public List<Map<String, Object>> getFields() {
+        return fields;
+    }
+
+    public List<Map<String, Object>> getDataset() {
+        return dataset;
+    }
+
+    /**
+     * 返回一个结果
+     * @return
+     */
+    public Map<String, Object> getOne() {
+        return dataset.size() > 0 ? dataset.get(0) : null;
+    }
+
+    public boolean isSuccess() {
+		return success;
+	}
+
+
+	public String getMessage() {
+		return message;
+	}
+
+
+	public void setSuccess(boolean success) {
+		this.success = success;
+	}
+
+
+	public void setMessage(String message) {
+		this.message = message;
+	}
+
+
+	public String getSql() {
+        return sql;
+    }
+
+    public void setSql(String sql) {
+        this.sql = sql;
+    }
+
+
+    @Override
+    public String toString() {
+    	return JSONObject.toJSONString(this);
+    }
+}

+ 64 - 0
sqlrunner/RSTypeHandler.java

@@ -0,0 +1,64 @@
+package com.primeton.dsp.dataservice.utils;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.dbutils.handlers.MapListHandler;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * SQL 查询,返回待类型的
+ *
+ * <pre>
+ *
+ * Created by zhaopx.
+ * User: zhaopx
+ * Date: 2020/4/2
+ * Time: 12:12
+ *
+ * </pre>
+ *
+ * @author zhaopx
+ */
+public class RSTypeHandler extends MapListHandler {
+
+
+    /**
+     * 返回的数据类型
+     */
+    private List<Map<String, Object>> types = new ArrayList<>();
+
+    @Override
+    protected Map<String, Object> handleRow(ResultSet rs) throws SQLException {
+        ResultSetMetaData rsmd = rs.getMetaData();
+        int cols = rsmd.getColumnCount();
+        for (int i = 1; i <= cols; i++) {
+            String columnName = rsmd.getColumnLabel(i);
+            if (null == columnName || 0 == columnName.length()) {
+                columnName = rsmd.getColumnName(i);
+            }
+
+            Map<String, Object> type = new HashMap<>();
+            type.put("name", columnName);
+            type.put("type", rsmd.getColumnTypeName(i));
+            types.add(type);
+        }
+        return super.handleRow(rs);
+    }
+
+
+    /**
+     * 返回数据类型
+     * @return
+     */
+    public List<Map<String, Object>> getTypes() {
+        // 返回不可修改的类型
+        return ImmutableList.copyOf(types);
+    }
+}

+ 352 - 0
sqlrunner/SQLRunner.java

@@ -0,0 +1,352 @@
+package com.primeton.dsp.dataservice.utils;
+
+import lombok.NonNull;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.apache.commons.dbutils.DbUtils;
+import org.apache.commons.dbutils.QueryRunner;
+import org.apache.commons.dbutils.handlers.MapListHandler;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+
+import javax.sql.DataSource;
+import java.io.Closeable;
+import java.io.IOException;
+import java.sql.*;
+import java.sql.Date;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ *
+ * 数据库执行类。主要封装了查询,更新,批量更新等一些执行SQL的方法。
+ *
+ * <pre>
+ *
+ * Created by zhaopx.
+ * User: zhaopx
+ * Date: 2020-03-26
+ * Time: 10:20
+ *
+ * </pre>
+ *
+ * @author zhaopx
+ */
+@Slf4j
+public class SQLRunner implements Closeable {
+
+    protected final DataSource ds;
+
+
+    public SQLRunner(Properties config) {
+        this(config.getProperty("jdbc.driverClassName"),
+                config.getProperty("jdbc.url"),
+                config.getProperty("jdbc.user"),
+                config.getProperty("jdbc.password"));
+    }
+
+
+    public SQLRunner(String driverClass,
+                     String url,
+                     String user,
+                     String password) {
+        BasicDataSource dataSource = new BasicDataSource();
+        dataSource.setMaxTotal(2);
+        dataSource.setMinIdle(1);
+        dataSource.setInitialSize(1);
+        dataSource.setDriverClassName(driverClass);
+        dataSource.setUrl(url);
+        dataSource.setUsername(user);
+        dataSource.setPassword(password);
+        this.ds = dataSource;
+    }
+
+    public SQLRunner(@NonNull DataSource ds) {
+        this.ds = ds;
+    }
+
+
+    public int update(String sql) throws SQLException {
+        if(sql == null) {
+            throw new SQLException("Null SQL statement");
+        }
+        Connection conn = ds.getConnection();
+        if(conn == null) {
+            throw new SQLException("Null connection");
+        }
+        Statement statement = null;
+        int r = 0;
+        try {
+            statement = conn.createStatement();
+            r = statement.executeUpdate(sql);
+        } finally {
+            close(conn, statement, null);
+        }
+        return r;
+    }
+
+
+    public int update(String sql, Object[] params) throws SQLException {
+        QueryRunner runner = new QueryRunner(this.ds);
+        return runner.update(sql, params);
+    }
+
+
+    /**
+     * 批量更新
+     * @param sql SQL 值 ? 代替
+     * @param params 参数数组
+     * @return
+     * @throws SQLException
+     */
+    public int[] updateBatch(String sql, List<Object[]> params) throws SQLException {
+        QueryRunner runner = new QueryRunner(this.ds);
+        return runner.batch(sql, params.toArray(new Object[params.size()][]));
+    }
+
+
+    /**
+     * 查询 sql,带参数
+     * @param sql
+     * @param params
+     * @return
+     * @throws SQLException
+     */
+    public QueryResult query(String sql, Object[] params) throws SQLException {
+        if(params.length == 0) {
+            // 不带参数的查询
+            return query(sql);
+        }
+
+        // 没有参数,QueryRunner 会报错
+        QueryRunner runner = new QueryRunner(this.ds);
+        RSTypeHandler typeHandler = new RSTypeHandler();
+        List<Map<String, Object>> list = runner.query(sql, typeHandler, params);
+        QueryResult queryResult = new QueryResult(typeHandler.getTypes(), list);
+        queryResult.setSql(sql);
+        return queryResult;
+    }
+
+
+    /**
+     * 无参数的查询
+     * @param sql
+     * @return
+     * @throws SQLException
+     */
+    public QueryResult query(String sql) throws SQLException {
+        Connection conn = ds.getConnection();
+        if(conn == null) {
+            throw new SQLException("Null connection");
+        } else if(sql == null) {
+            DbUtils.close(conn);
+            throw new SQLException("Null SQL statement");
+        } else {
+            Statement statement = null;
+            ResultSet rs = null;
+            try {
+                statement = conn.createStatement();
+                rs = statement.executeQuery(sql);
+                RSTypeHandler typeHandler = new RSTypeHandler();
+                List<Map<String, Object>> list = typeHandler.handle(rs);
+                QueryResult queryResult = new QueryResult(typeHandler.getTypes(), list);
+                queryResult.setSql(sql);
+                return queryResult;
+            } catch (SQLException e) {
+                // 没有返回值
+                if(e.getMessage().contains("not generate a result set")) {
+                    log.warn("execute sql: '"+sql+"' success, no result set.", e);
+                    QueryResult queryResult = new QueryResult(Collections.<Map<String, Object>>emptyList(), Collections.<Map<String, Object>>emptyList());
+                    queryResult.setSql(sql);
+                    return queryResult;
+                }
+                throw e;
+            } finally {
+                close(conn, statement, rs);
+            }
+        }
+    }
+
+
+
+    /**
+     * 查询 sql,带参数
+     * @param sql
+     * @param params
+     * @return
+     * @throws SQLException
+     */
+    public QueryResult queryOne(String sql, Object[] params) throws SQLException {
+        if(params.length == 0) {
+            // 不带参数的查询
+            return query(sql);
+        }
+
+        // 没有参数,QueryRunner 会报错
+        QueryRunner runner = new QueryRunner(this.ds);
+        RSTypeHandler typeHandler = new RSTypeHandler();
+        List<Map<String, Object>> list = runner.query(sql, typeHandler, params);
+        QueryResult queryResult = new QueryResult(typeHandler.getTypes(), list);
+        queryResult.setSql(sql);
+        return queryResult;
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            ((BasicDataSource)ds).close();
+        } catch (SQLException e) {
+
+        }
+    }
+
+
+    /**
+     * 安静的关闭数据库链接。
+     *
+     * @param conn
+     * @param ps
+     * @param rs
+     */
+    public static void close(Connection conn, Statement ps, ResultSet rs) {
+        try {
+            if (rs != null) {
+                rs.close();
+            }
+        } catch (Exception e) { }
+        try {
+            if (ps != null) {
+                ps.close();
+            }
+        } catch (Exception e) {}
+        try {
+            if (conn != null) {
+                conn.close();
+            }
+        } catch (Exception e) {}
+    }
+
+    /**
+     * 根据 SQL 的 ${field} 按照 map 中的参数替换,组成一个新的 SQL 返回。
+     * @param sql 支持 ${field} 的变量替换
+     * @param params 参数,如果没有参数则会原路返回
+     * @return 返回新的变量替换的 SQL
+     */
+    public static String getMatchSQL(String sql, Map<String, Object> params){
+        Matcher matcher = Pattern.compile("(\\$\\{\\w+\\})").matcher(sql);
+        StringBuffer sb = new StringBuffer();
+        while (matcher.find()) {
+            String group = matcher.group();
+            String field = StringUtils.trim(group.substring(2, group.length() - 1));
+            String val = (String)params.get(field);
+            if(val == null) {
+                continue;
+            }
+            matcher.appendReplacement(sb, val);
+        }
+        matcher.appendTail(sb);
+
+        return sb.toString();
+    }
+
+
+
+    /**
+     * 获取一个 sql 内 from table 的table 名称,如果是join sql 则获取多个from的表名
+     * @param sql
+     * @return
+     */
+    public static String[] getSQLTable(String sql) {
+        //select sql 里 from 后的字符和join后的字符,都是表名
+        //正则表达式,不区分大小写的模式
+        Pattern compile = Pattern.compile("(TABLE|JOIN|FROM){1}\\s+(\\w+)", Pattern.CASE_INSENSITIVE);
+        Matcher matcher = compile.matcher(sql);
+        List<String> names = new ArrayList<>(3);
+        while (matcher.find()) {
+            String name = matcher.group(2);
+            names.add(name);
+        }
+        return names.toArray(new String[names.size()]);
+    }
+
+
+    /**
+     *
+     * 把值 val 转为 type 的类型, type 为数据库的类型
+     *
+     * @param val 字段值
+     * @param type 字段类型
+     * @return
+     */
+    public static Object convertVal(Object val, String type) {
+        if(val == null) {
+            return null;
+        }
+        type = type.toUpperCase();
+        switch (type){
+            case "VARCHAR":
+            case "VARCHAR2":
+            case "TEXT":
+            case "BLOB":
+                return val instanceof String ? (String)val : String.valueOf(val);
+
+            case "TINYINT":
+            case "SMALLINT":
+            case "INT":
+                return val instanceof Number ? ((Number)val).intValue() : Integer.parseInt(String.valueOf(val));
+
+            case "DECIMAL":
+            case "FLOAT":
+                return val instanceof Number ? ((Number)val).floatValue() : Float.parseFloat(String.valueOf(val));
+
+            case "DOUBLE":
+                return val instanceof Number ? ((Number)val).doubleValue() : Float.parseFloat(String.valueOf(val));
+
+            case "BIGINT":
+                return val instanceof Number ? ((Number)val).longValue() : Long.parseLong(String.valueOf(val));
+
+            case "DATETIME":
+                if(val instanceof java.util.Date){
+                    return new Timestamp(((java.util.Date)val).getTime());
+                } else if(val instanceof Number) {
+                    return new Timestamp(((Number) val).longValue());
+                }
+
+                try {
+                    return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(val.toString()).getTime());
+                } catch (ParseException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            case "DATE":
+                if(val instanceof java.util.Date){
+                    return new Date(((java.util.Date)val).getTime());
+                } else if(val instanceof Number) {
+                    return new Date(((Number) val).longValue());
+                }
+
+                try {
+                    return new Date(new SimpleDateFormat("yyyy-MM-dd").parse(val.toString()).getTime());
+                } catch (ParseException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            case "TIME":
+                if(val instanceof java.util.Date){
+                    return new Time(((java.util.Date)val).getTime());
+                } else if(val instanceof Number) {
+                    return new Time(((Number) val).longValue());
+                }
+
+                try {
+                    return new Time(new SimpleDateFormat("HH:mm:ss").parse(val.toString()).getTime());
+                } catch (ParseException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            default:
+                return val instanceof String ? (String)val : String.valueOf(val);
+        }
+    }
+
+}