提交 efa75445 编写于 作者: Z zyyang

change

上级 42c802bd
package com.taosdata.jdbc;
import com.taosdata.jdbc.utils.TDNodes;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public abstract class BaseTest {
private static boolean testCluster = false;
private static TDNodes nodes = new TDNodes();
@BeforeClass
public static void setupEnv() {
try {
if (nodes.getTDNode(1).getTaosdPid() != null) {
System.out.println("Kill taosd before running JDBC test");
nodes.getTDNode(1).setRunning(1);
nodes.stop(1);
}
nodes.setTestCluster(testCluster);
nodes.deploy(1);
nodes.start(1);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public static void cleanUpEnv() {
nodes.stop(1);
}
}
\ No newline at end of file
...@@ -13,42 +13,37 @@ import java.util.Properties; ...@@ -13,42 +13,37 @@ import java.util.Properties;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class ResultSetTest extends BaseTest { public class ResultSetTest {
static Connection connection = null; static Connection connection;
static Statement statement = null; static Statement statement;
static String dbName = "test"; static String dbName = "test";
static String tName = "t0"; static String tName = "t0";
static String host = "localhost"; static String host = "localhost";
static ResultSet resSet = null; static ResultSet resSet;
@BeforeClass @BeforeClass
public static void createDatabaseAndTable() throws SQLException { public static void createDatabaseAndTable() {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
} catch (ClassNotFoundException e) { Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.execute("use " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k1 int, k2 bigint, k3 float, k4 double, k5 binary(30), k6 smallint, k7 bool, k8 nchar(20))");
} catch (ClassNotFoundException | SQLException e) {
return; return;
} }
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName +
" (ts timestamp, k1 int, k2 bigint, k3 float, k4 double, k5 binary(30), k6 smallint, k7 bool, k8 nchar(20))");
statement.executeQuery("use " + dbName);
} }
@Test @Test
public void testResultSet() { public void testResultSet() {
String sql = null; String sql;
long ts = 1496732686000l; long ts = 1496732686000l;
int v1 = 2147483600; int v1 = 2147483600;
long v2 = ts + 1000; long v2 = ts + 1000;
...@@ -815,13 +810,18 @@ public class ResultSetTest extends BaseTest { ...@@ -815,13 +810,18 @@ public class ResultSetTest extends BaseTest {
assertEquals(res.length, 2); assertEquals(res.length, 2);
statement.clearBatch(); statement.clearBatch();
} }
@AfterClass
public static void close() throws Exception {
statement.executeUpdate("drop database " + dbName);
statement.close();
connection.close();
Thread.sleep(10);
@AfterClass
public static void close() {
try {
statement.executeUpdate("drop database " + dbName);
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
...@@ -16,23 +16,22 @@ public class StatementTest { ...@@ -16,23 +16,22 @@ public class StatementTest {
static String dbName = "test"; static String dbName = "test";
static String tName = "t0"; static String tName = "t0";
static String host = "localhost"; static String host = "localhost";
static ResultSet resSet = null;
@BeforeClass @BeforeClass
public static void createConnection() throws SQLException { public static void createConnection() throws SQLException {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata", properties);
statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
return; return;
} }
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata", properties);
statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
} }
@Test @Test
...@@ -49,7 +48,6 @@ public class StatementTest { ...@@ -49,7 +48,6 @@ public class StatementTest {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test @Test
...@@ -173,12 +171,15 @@ public class StatementTest { ...@@ -173,12 +171,15 @@ public class StatementTest {
} }
@AfterClass @AfterClass
public static void close() throws Exception { public static void close() {
if (!statement.isClosed()) { try {
statement.executeUpdate("drop database if exists " + dbName); statement.execute("drop database if exists " + dbName);
statement.close(); if (statement != null)
connection.close(); statement.close();
Thread.sleep(10); if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
} }
} }
} }
...@@ -19,27 +19,28 @@ public class SubscribeTest { ...@@ -19,27 +19,28 @@ public class SubscribeTest {
String topic = "test"; String topic = "test";
@Before @Before
public void createDatabase() throws SQLException { public void createDatabase() {
try { try {
Class.forName("com.taosdata.jdbc.TSDBDriver"); Class.forName("com.taosdata.jdbc.TSDBDriver");
} catch (ClassNotFoundException e) { Properties properties = new Properties();
return; properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
} properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
Properties properties = new Properties(); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
statement = connection.createStatement(); statement = connection.createStatement();
statement.executeUpdate("create database if not exists " + dbName); statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)"); statement.executeUpdate("create table if not exists " + dbName + "." + tName + " (ts timestamp, k int, v int)");
long ts = System.currentTimeMillis(); long ts = System.currentTimeMillis();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
ts += i; ts += i;
String sql = "insert into " + dbName + "." + tName + " values (" + ts + ", " + (100 + i) + ", " + i + ")"; String sql = "insert into " + dbName + "." + tName + " values (" + ts + ", " + (100 + i) + ", " + i + ")";
statement.executeUpdate(sql); statement.executeUpdate(sql);
}
} catch (ClassNotFoundException | SQLException e) {
return;
} }
} }
...@@ -79,10 +80,16 @@ public class SubscribeTest { ...@@ -79,10 +80,16 @@ public class SubscribeTest {
} }
@After @After
public void close() throws Exception { public void close() {
statement.executeQuery("drop database " + dbName); try {
statement.close(); statement.executeQuery("drop database " + dbName);
connection.close(); if (statement != null)
Thread.sleep(10); statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
\ No newline at end of file
...@@ -6,37 +6,9 @@ import java.sql.*; ...@@ -6,37 +6,9 @@ import java.sql.*;
import java.util.Properties; import java.util.Properties;
public class TSDBDatabaseMetaDataTest { public class TSDBDatabaseMetaDataTest {
private TSDBDatabaseMetaData metaData;
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private Connection connection; private static Connection connection;
private static TSDBDatabaseMetaData metaData;
@BeforeClass
public void before() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties);
metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@AfterClass
public void after() {
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test @Test
public void unwrap() throws SQLException { public void unwrap() throws SQLException {
...@@ -975,4 +947,33 @@ public class TSDBDatabaseMetaDataTest { ...@@ -975,4 +947,33 @@ public class TSDBDatabaseMetaDataTest {
public void generatedKeyAlwaysReturned() throws SQLException { public void generatedKeyAlwaysReturned() throws SQLException {
Assert.assertFalse(metaData.generatedKeyAlwaysReturned()); Assert.assertFalse(metaData.generatedKeyAlwaysReturned());
} }
@BeforeClass
public static void before() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties);
metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@AfterClass
public static void after() {
try {
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
\ No newline at end of file
...@@ -3,9 +3,6 @@ package com.taosdata.jdbc; ...@@ -3,9 +3,6 @@ package com.taosdata.jdbc;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
...@@ -27,54 +24,15 @@ public class TSDBDriverTest { ...@@ -27,54 +24,15 @@ public class TSDBDriverTest {
"jdbc:TAOS://:/test", "jdbc:TAOS://:/test",
"jdbc:TAOS://localhost:0/?user=root&password=taosdata" "jdbc:TAOS://localhost:0/?user=root&password=taosdata"
}; };
private static boolean islibLoaded = false;
private static boolean isTaosdActived;
private Connection conn; private Connection conn;
@BeforeClass
public static void before() {
String osName = System.getProperty("os.name").toLowerCase();
if (!osName.equals("linux"))
return;
// try to load taos lib
try {
System.loadLibrary("taos");
islibLoaded = true;
} catch (UnsatisfiedLinkError error) {
System.out.println("load tdengine lib failed.");
error.printStackTrace();
}
// check taosd is activated
try {
String[] cmd = {"/bin/bash", "-c", "ps -ef | grep taosd | grep -v \"grep\""};
Process exec = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
int lineCnt = 0;
while (reader.readLine() != null) {
lineCnt++;
}
if (lineCnt > 0)
isTaosdActived = true;
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test @Test
public void testConnectWithJdbcURL() { public void testConnectWithJdbcURL() {
final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata"; final String url = "jdbc:TAOS://localhost:6030/log?user=root&password=taosdata";
try { try {
if (islibLoaded && isTaosdActived) { conn = DriverManager.getConnection(url);
conn = DriverManager.getConnection(url); assertNotNull("failure - connection should not be null", conn);
assertNotNull("failure - connection should not be null", conn);
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
fail("failure - should not throw Exception"); fail("failure - should not throw Exception");
...@@ -89,10 +47,8 @@ public class TSDBDriverTest { ...@@ -89,10 +47,8 @@ public class TSDBDriverTest {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try { try {
if (islibLoaded && isTaosdActived) { conn = DriverManager.getConnection(jdbcUrl, connProps);
conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn);
assertNotNull("failure - connection should not be null", conn);
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
fail("failure - should not throw Exception"); fail("failure - should not throw Exception");
...@@ -107,10 +63,8 @@ public class TSDBDriverTest { ...@@ -107,10 +63,8 @@ public class TSDBDriverTest {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try { try {
if (islibLoaded && isTaosdActived) { conn = DriverManager.getConnection(jdbcUrl, connProps);
conn = DriverManager.getConnection(jdbcUrl, connProps); assertNotNull("failure - connection should not be null", conn);
assertNotNull("failure - connection should not be null", conn);
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
fail("failure - should not throw Exception"); fail("failure - should not throw Exception");
...@@ -207,4 +161,14 @@ public class TSDBDriverTest { ...@@ -207,4 +161,14 @@ public class TSDBDriverTest {
assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger()); assertNull("failure - getParentLogger should be be null", new TSDBDriver().getParentLogger());
} }
@BeforeClass
public static void before() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} }
\ No newline at end of file
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import com.taosdata.jdbc.TSDBDriver;
import com.taosdata.jdbc.lib.TSDBCommon; import com.taosdata.jdbc.lib.TSDBCommon;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.sql.Connection; import java.sql.*;
import java.sql.ResultSet; import java.util.Properties;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
...@@ -18,30 +17,47 @@ import static org.junit.Assert.assertEquals; ...@@ -18,30 +17,47 @@ import static org.junit.Assert.assertEquals;
public class BatchInsertTest { public class BatchInsertTest {
static String host = "localhost"; static String host = "127.0.0.1";
static String dbName = "test"; static String dbName = "test";
static String stbName = "meters"; static String stbName = "meters";
static int numOfTables = 30; static int numOfTables = 30;
final static int numOfRecordsPerTable = 1000; final static int numOfRecordsPerTable = 1000;
static long ts = 1496732686000l; static long ts = 1496732686000l;
final static String tablePrefix = "t"; final static String tablePrefix = "t";
private Connection connection; private Connection connection;
@Before @Before
public void before() { public void before() {
try { try {
connection = TSDBCommon.getConn(host); Class.forName("com.taosdata.jdbc.TSDBDriver");
TSDBCommon.createDatabase(connection, dbName); Properties properties = new Properties();
TSDBCommon.createStable(connection, stbName); properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
TSDBCommon.createTables(connection, numOfTables, stbName, tablePrefix); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
Statement statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("use " + dbName);
// create stable
String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))";
statement.executeUpdate(createTableSql);
// create tables
for(int i = 0; i < numOfTables; i++) {
String loc = i % 2 == 0 ? "beijing" : "shanghai";
String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')";
statement.executeUpdate(createSubTalbesSql);
}
statement.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Test @Test
public void testBatchInsert(){ public void testBatchInsert() {
ExecutorService executorService = Executors.newFixedThreadPool(numOfTables); ExecutorService executorService = Executors.newFixedThreadPool(numOfTables);
for (int i = 0; i < numOfTables; i++) { for (int i = 0; i < numOfTables; i++) {
final int index = i; final int index = i;
...@@ -63,7 +79,7 @@ public class BatchInsertTest { ...@@ -63,7 +79,7 @@ public class BatchInsertTest {
statement.addBatch(sb.toString()); statement.addBatch(sb.toString());
statement.executeBatch(); statement.executeBatch();
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds"); System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds");
connection.commit(); connection.commit();
statement.close(); statement.close();
} catch (Exception e) { } catch (Exception e) {
...@@ -80,7 +96,7 @@ public class BatchInsertTest { ...@@ -80,7 +96,7 @@ public class BatchInsertTest {
e.printStackTrace(); e.printStackTrace();
} }
try{ try {
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from meters"); ResultSet rs = statement.executeQuery("select * from meters");
int num = 0; int num = 0;
...@@ -89,7 +105,7 @@ public class BatchInsertTest { ...@@ -89,7 +105,7 @@ public class BatchInsertTest {
} }
assertEquals(num, numOfTables * numOfRecordsPerTable); assertEquals(num, numOfTables * numOfRecordsPerTable);
rs.close(); rs.close();
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
...@@ -102,7 +118,6 @@ public class BatchInsertTest { ...@@ -102,7 +118,6 @@ public class BatchInsertTest {
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
package com.taosdata.jdbc.lib;
import com.taosdata.jdbc.TSDBDriver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class TSDBCommon {
public static Connection getConn(String host) throws SQLException, ClassNotFoundException {
Class.forName("com.taosdata.jdbc.TSDBDriver");
Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
return DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
}
public static void createDatabase(Connection connection, String dbName) throws SQLException {
Statement statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("use " + dbName);
statement.close();
}
public static void createStable(Connection connection, String stbName) throws SQLException {
Statement statement = connection.createStatement();
String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))";
statement.executeUpdate(createTableSql);
statement.close();
}
public static void createTables(Connection connection, int numOfTables, String stbName,String tablePrefix) throws SQLException {
Statement statement = connection.createStatement();
for(int i = 0; i < numOfTables; i++) {
String loc = i % 2 == 0 ? "beijing" : "shanghai";
String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')";
statement.executeUpdate(createSubTalbesSql);
}
statement.close();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册