CreateTableTask.java 1.4 KB
Newer Older
Z
zyyang 已提交
1
package com.taosdata.example.jdbcTaosdemo.task;
2

Z
zyyang 已提交
3
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
Z
zyyang 已提交
4 5
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTableTask implements Runnable {

    private static Logger logger = Logger.getLogger(CreateTableTask.class);
    private final JdbcTaosdemoConfig config;
    private final int startIndex;
    private final int tableNumber;

    public CreateTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber) {
        this.config = config;
        this.startIndex = startIndex;
        this.tableNumber = tableNumber;
    }

    @Override
    public void run() {
        try {
Z
zyyang 已提交
28
            Connection connection = ConnectionFactory.build(config);
29 30
            for (int i = startIndex; i < startIndex + tableNumber; i++) {
                Statement statement = connection.createStatement();
Z
zyyang 已提交
31 32
                String sql = SqlSpeller.createTableSQL(i + 1, config.getDbName(), config.getStbName());
                statement.execute(sql);
33 34 35 36 37 38 39 40 41 42
                statement.close();
                logger.info(">>> " + sql);
            }
            connection.close();
        } catch (SQLException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }
}