未验证 提交 059a94c8 编写于 作者: H huolibo 提交者: GitHub

[TD-11515]<fix>:delete test database && fix use databae in restful (#9109)

* remove test database

* delete test database && fix use databae in restful

* restore

* restore

* Delete useless code

* restore code
上级 664b2a46
...@@ -14,13 +14,43 @@ import java.math.BigDecimal; ...@@ -14,13 +14,43 @@ import java.math.BigDecimal;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
public class RestfulResultSet extends AbstractResultSet implements ResultSet { public class RestfulResultSet extends AbstractResultSet implements ResultSet {
public static DateTimeFormatter rfc3339Parser = null;
{
rfc3339Parser = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendValue(ChronoField.YEAR, 4)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 2, 9, true)
.optionalEnd()
.appendOffset("+HH:MM", "Z").toFormatter()
.withResolverStyle(ResolverStyle.STRICT)
.withChronology(IsoChronology.INSTANCE);
}
private final Statement statement; private final Statement statement;
// data // data
private final List<List<Object>> resultSet = new ArrayList<>(); private final List<List<Object>> resultSet = new ArrayList<>();
...@@ -187,25 +217,41 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet { ...@@ -187,25 +217,41 @@ public class RestfulResultSet extends AbstractResultSet implements ResultSet {
} }
case UTC: { case UTC: {
String value = row.getString(colIndex); String value = row.getString(colIndex);
long epochSec = Timestamp.valueOf(value.substring(0, 19).replace("T", " ")).getTime() / 1000; if (value.lastIndexOf(":") > 19) {
int fractionalSec = Integer.parseInt(value.substring(20, value.length() - 5)); ZonedDateTime parse = ZonedDateTime.parse(value, rfc3339Parser);
long nanoAdjustment; long nanoAdjustment;
if (value.length() > 31) { if (value.length() > 32) {
// ns timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSSSSS+0x00 // ns timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSSSSS+0x:00
nanoAdjustment = fractionalSec; this.timestampPrecision = TimestampPrecision.NS;
this.timestampPrecision = TimestampPrecision.NS; } else if (value.length() > 29) {
} else if (value.length() > 28) { // ms timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSS+0x:00
// ms timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSS+0x00 this.timestampPrecision = TimestampPrecision.US;
nanoAdjustment = fractionalSec * 1000L; } else {
this.timestampPrecision = TimestampPrecision.US; // ms timestamp: yyyy-MM-ddTHH:mm:ss.SSS+0x:00
this.timestampPrecision = TimestampPrecision.MS;
}
return Timestamp.from(parse.toInstant());
} else { } else {
// ms timestamp: yyyy-MM-ddTHH:mm:ss.SSS+0x00 long epochSec = Timestamp.valueOf(value.substring(0, 19).replace("T", " ")).getTime() / 1000;
nanoAdjustment = fractionalSec * 1000_000L; int fractionalSec = Integer.parseInt(value.substring(20, value.length() - 5));
this.timestampPrecision = TimestampPrecision.MS; long nanoAdjustment;
if (value.length() > 32) {
// ns timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSSSSS+0x00
nanoAdjustment = fractionalSec;
this.timestampPrecision = TimestampPrecision.NS;
} else if (value.length() > 29) {
// ms timestamp: yyyy-MM-ddTHH:mm:ss.SSSSSS+0x00
nanoAdjustment = fractionalSec * 1000L;
this.timestampPrecision = TimestampPrecision.US;
} else {
// ms timestamp: yyyy-MM-ddTHH:mm:ss.SSS+0x00
nanoAdjustment = fractionalSec * 1000_000L;
this.timestampPrecision = TimestampPrecision.MS;
}
ZoneOffset zoneOffset = ZoneOffset.of(value.substring(value.length() - 5));
Instant instant = Instant.ofEpochSecond(epochSec, nanoAdjustment).atOffset(zoneOffset).toInstant();
return Timestamp.from(instant);
} }
ZoneOffset zoneOffset = ZoneOffset.of(value.substring(value.length() - 5));
Instant instant = Instant.ofEpochSecond(epochSec, nanoAdjustment).atOffset(zoneOffset).toInstant();
return Timestamp.from(instant);
} }
case STRING: case STRING:
default: { default: {
......
...@@ -72,6 +72,7 @@ public class RestfulStatement extends AbstractStatement { ...@@ -72,6 +72,7 @@ public class RestfulStatement extends AbstractStatement {
} }
this.database = sql.trim().replace("use", "").trim(); this.database = sql.trim().replace("use", "").trim();
this.conn.setCatalog(this.database); this.conn.setCatalog(this.database);
this.conn.setClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME, this.database);
result = false; result = false;
} else if (SqlSyntaxValidator.isDatabaseUnspecifiedQuery(sql)) { } else if (SqlSyntaxValidator.isDatabaseUnspecifiedQuery(sql)) {
executeOneQuery(sql); executeOneQuery(sql);
......
...@@ -128,8 +128,8 @@ public class ParameterBindTest { ...@@ -128,8 +128,8 @@ public class ParameterBindTest {
@After @After
public void after() { public void after() {
try { try {
// Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
// stmt.execute("drop database if exists test_pd"); stmt.execute("drop database if exists test_pd");
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
......
...@@ -187,7 +187,6 @@ public class SchemalessInsertTest { ...@@ -187,7 +187,6 @@ public class SchemalessInsertTest {
public void after() { public void after() {
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists " + dbname); stmt.execute("drop database if exists " + dbname);
stmt.close();
conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -380,8 +380,12 @@ public class TSDBConnectionTest { ...@@ -380,8 +380,12 @@ public class TSDBConnectionTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists test");
statement.close();
conn.close(); conn.close();
}
} }
} }
\ No newline at end of file
package com.taosdata.jdbc; package com.taosdata.jdbc;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.sql.*; import java.sql.*;
......
...@@ -7,6 +7,7 @@ import org.junit.Test; ...@@ -7,6 +7,7 @@ import org.junit.Test;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean; import java.lang.management.RuntimeMXBean;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
...@@ -19,115 +20,111 @@ public class TSDBJNIConnectorTest { ...@@ -19,115 +20,111 @@ public class TSDBJNIConnectorTest {
private static TSDBResultSetRowData rowData; private static TSDBResultSetRowData rowData;
@Test @Test
public void test() { public void test() throws SQLException {
try { try {
try { //change sleepSeconds when debugging with attach to process to find PID
//change sleepSeconds when debugging with attach to process to find PID int sleepSeconds = -1;
int sleepSeconds = -1; if (sleepSeconds > 0) {
if (sleepSeconds > 0) { RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); String jvmName = runtimeBean.getName();
String jvmName = runtimeBean.getName(); long pid = Long.valueOf(jvmName.split("@")[0]);
long pid = Long.valueOf(jvmName.split("@")[0]); System.out.println("JVM PID = " + pid);
System.out.println("JVM PID = " + pid);
Thread.sleep(sleepSeconds * 1000);
Thread.sleep(sleepSeconds * 1000);
}
} catch (Exception e) {
e.printStackTrace();
} }
} catch (Exception e) {
e.printStackTrace();
}
// init // init
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR, "/etc/taos"); properties.setProperty(TSDBDriver.PROPERTY_KEY_CONFIG_DIR, "/etc/taos");
TSDBJNIConnector.init(properties); TSDBJNIConnector.init(properties);
// connect // connect
TSDBJNIConnector connector = new TSDBJNIConnector(); TSDBJNIConnector connector = new TSDBJNIConnector();
connector.connect("127.0.0.1", 6030, null, "root", "taosdata"); connector.connect("127.0.0.1", 6030, null, "root", "taosdata");
// setup // setup
String setupSqlStrs[] = {"create database if not exists d precision \"us\"", String setupSqlStrs[] = {"create database if not exists d precision \"us\"",
"create table if not exists d.t(ts timestamp, f int)", "create table if not exists d.t(ts timestamp, f int)",
"create database if not exists d2", "create database if not exists d2",
"create table if not exists d2.t2(ts timestamp, f int)", "create table if not exists d2.t2(ts timestamp, f int)",
"insert into d.t values(now+100s, 100)", "insert into d.t values(now+100s, 100)",
"insert into d2.t2 values(now+200s, 200)" "insert into d2.t2 values(now+200s, 200)"
}; };
for (String setupSqlStr : setupSqlStrs) { for (String setupSqlStr : setupSqlStrs) {
long setupSql = connector.executeQuery(setupSqlStr); long setupSql = connector.executeQuery(setupSqlStr);
assertEquals(0, connector.getResultTimePrecision(setupSql)); assertEquals(0, connector.getResultTimePrecision(setupSql));
if (connector.isUpdateQuery(setupSql)) { if (connector.isUpdateQuery(setupSql)) {
connector.freeResultSet(setupSql); connector.freeResultSet(setupSql);
}
} }
}
{ {
long sqlObj1 = connector.executeQuery("select * from d2.t2"); long sqlObj1 = connector.executeQuery("select * from d2.t2");
assertEquals(0, connector.getResultTimePrecision(sqlObj1)); assertEquals(0, connector.getResultTimePrecision(sqlObj1));
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
int code = connector.getSchemaMetaData(sqlObj1, columnMetaDataList); int code = connector.getSchemaMetaData(sqlObj1, columnMetaDataList);
rowData = new TSDBResultSetRowData(columnMetaDataList.size()); rowData = new TSDBResultSetRowData(columnMetaDataList.size());
assertTrue(next(connector, sqlObj1)); assertTrue(next(connector, sqlObj1));
assertEquals(0, connector.getResultTimePrecision(sqlObj1)); assertEquals(0, connector.getResultTimePrecision(sqlObj1));
connector.freeResultSet(sqlObj1); connector.freeResultSet(sqlObj1);
} }
// executeQuery // executeQuery
long pSql = connector.executeQuery("select * from d.t"); long pSql = connector.executeQuery("select * from d.t");
if (connector.isUpdateQuery(pSql)) { if (connector.isUpdateQuery(pSql)) {
connector.freeResultSet(pSql); connector.freeResultSet(pSql);
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_WITH_EXECUTEQUERY);
} }
assertEquals(1, connector.getResultTimePrecision(pSql)); assertEquals(1, connector.getResultTimePrecision(pSql));
// get schema // get schema
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(); List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
int code = connector.getSchemaMetaData(pSql, columnMetaDataList); int code = connector.getSchemaMetaData(pSql, columnMetaDataList);
if (code == TSDBConstants.JNI_CONNECTION_NULL) { if (code == TSDBConstants.JNI_CONNECTION_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL);
} }
if (code == TSDBConstants.JNI_RESULT_SET_NULL) { if (code == TSDBConstants.JNI_RESULT_SET_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL);
} }
if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) { if (code == TSDBConstants.JNI_NUM_OF_FIELDS_0) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_NUM_OF_FIELDS_0);
} }
assertEquals(1, connector.getResultTimePrecision(pSql));
int columnSize = columnMetaDataList.size();
// print metadata
for (int i = 0; i < columnSize; i++) {
// System.out.println(columnMetaDataList.get(i));
}
rowData = new TSDBResultSetRowData(columnSize);
// iterate resultSet
for (int i = 0; next(connector, pSql); i++) {
assertEquals(1, connector.getResultTimePrecision(pSql)); assertEquals(1, connector.getResultTimePrecision(pSql));
int columnSize = columnMetaDataList.size();
// print metadata
for (int i = 0; i < columnSize; i++) {
System.out.println(columnMetaDataList.get(i));
}
rowData = new TSDBResultSetRowData(columnSize);
// iterate resultSet
for (int i = 0; next(connector, pSql); i++) {
assertEquals(1, connector.getResultTimePrecision(pSql));
System.out.println();
}
// close resultSet
code = connector.freeResultSet(pSql);
if (code == TSDBConstants.JNI_CONNECTION_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL);
} else if (code == TSDBConstants.JNI_RESULT_SET_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL);
}
// close statement
connector.executeQuery("use d");
String[] lines = new String[]{
"st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
"st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000"};
connector.insertLines(lines, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO_SECONDS);
// close connection
connector.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
} }
// close resultSet
code = connector.freeResultSet(pSql);
if (code == TSDBConstants.JNI_CONNECTION_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL);
} else if (code == TSDBConstants.JNI_RESULT_SET_NULL) {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_RESULT_SET_NULL);
}
// close statement
connector.executeQuery("use d");
String[] lines = new String[]{
"st,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000",
"st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000"};
connector.insertLines(lines, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO_SECONDS);
// close connection
connector.executeQuery("drop database if exists d");
connector.executeQuery("drop database if exists d2");
connector.closeConnection();
} }
private static boolean next(TSDBJNIConnector connector, long pSql) throws SQLException { private static boolean next(TSDBJNIConnector connector, long pSql) throws SQLException {
......
...@@ -17,6 +17,7 @@ public class TSDBParameterMetaDataTest { ...@@ -17,6 +17,7 @@ public class TSDBParameterMetaDataTest {
private static PreparedStatement pstmt_select; private static PreparedStatement pstmt_select;
private static ParameterMetaData parameterMetaData_insert; private static ParameterMetaData parameterMetaData_insert;
private static ParameterMetaData parameterMetaData_select; private static ParameterMetaData parameterMetaData_select;
private static final String dbname = "test_pstmt";
@Test @Test
public void getParameterCount() throws SQLException { public void getParameterCount() throws SQLException {
...@@ -152,9 +153,9 @@ public class TSDBParameterMetaDataTest { ...@@ -152,9 +153,9 @@ public class TSDBParameterMetaDataTest {
try { try {
conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata");
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_pstmt"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists test_pstmt"); stmt.execute("create database if not exists " + dbname);
stmt.execute("use test_pstmt"); stmt.execute("use " + dbname);
stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))"); stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))");
stmt.execute("create table t1 using weather tags('beijing')"); stmt.execute("create table t1 using weather tags('beijing')");
} }
...@@ -190,8 +191,12 @@ public class TSDBParameterMetaDataTest { ...@@ -190,8 +191,12 @@ public class TSDBParameterMetaDataTest {
pstmt_insert.close(); pstmt_insert.close();
if (pstmt_select != null) if (pstmt_select != null)
pstmt_select.close(); pstmt_select.close();
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -1233,6 +1233,7 @@ public class TSDBPreparedStatementTest { ...@@ -1233,6 +1233,7 @@ public class TSDBPreparedStatementTest {
try { try {
Statement statement = conn.createStatement(); Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname); statement.execute("drop database if exists " + dbname);
statement.execute("drop database if exists dbtest");
statement.close(); statement.close();
if (conn != null) if (conn != null)
conn.close(); conn.close();
......
...@@ -668,8 +668,12 @@ public class TSDBResultSetTest { ...@@ -668,8 +668,12 @@ public class TSDBResultSetTest {
rs.close(); rs.close();
if (stmt != null) if (stmt != null)
stmt.close(); stmt.close();
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists restful_test");
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.Assert; import org.junit.*;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import java.sql.*; import java.sql.*;
...@@ -16,36 +13,32 @@ public class InsertDbwithoutUseDbTest { ...@@ -16,36 +13,32 @@ public class InsertDbwithoutUseDbTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private static Properties properties; private static Properties properties;
private static final Random random = new Random(System.currentTimeMillis()); private static final Random random = new Random(System.currentTimeMillis());
private static final String dbname = "inWithoutDb";
@Test @Test
public void case001() throws SQLException { public void case001() throws SQLException {
// prepare schema // prepare schema
String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata"; String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata";
Connection conn = DriverManager.getConnection(url, properties); Connection conn = DriverManager.getConnection(url, properties);
try (Statement stmt = conn.createStatement()) { Statement stmt = conn.createStatement();
stmt.execute("drop database if exists inWithoutDb"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists inWithoutDb"); stmt.execute("create database if not exists " + dbname);
stmt.execute("create table inWithoutDb.weather(ts timestamp, f1 int)"); stmt.execute("create table " + dbname + ".weather(ts timestamp, f1 int)");
}
conn.close(); conn.close();
// execute insert // execute insert
url = "jdbc:TAOS://127.0.0.1:6030/inWithoutDb?user=root&password=taosdata"; url = "jdbc:TAOS://127.0.0.1:6030/" + dbname + "?user=root&password=taosdata";
conn = DriverManager.getConnection(url, properties); conn = DriverManager.getConnection(url, properties);
try (Statement stmt = conn.createStatement()) { stmt = conn.createStatement();
int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")"); int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")");
Assert.assertEquals(1, affectedRow); Assert.assertEquals(1, affectedRow);
boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")"); boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")");
Assert.assertEquals(false, flag); Assert.assertEquals(false, flag);
ResultSet rs = stmt.executeQuery("select count(*) from weather"); ResultSet rs = stmt.executeQuery("select count(*) from weather");
rs.next(); rs.next();
int count = rs.getInt("count(*)"); int count = rs.getInt("count(*)");
Assert.assertEquals(2, count); Assert.assertEquals(2, count);
} catch (SQLException e) {
e.printStackTrace();
}
conn.close(); conn.close();
} }
...@@ -54,28 +47,25 @@ public class InsertDbwithoutUseDbTest { ...@@ -54,28 +47,25 @@ public class InsertDbwithoutUseDbTest {
// prepare the schema // prepare the schema
final String url = "jdbc:TAOS-RS://" + host + ":6041/inWithoutDb?user=root&password=taosdata"; final String url = "jdbc:TAOS-RS://" + host + ":6041/inWithoutDb?user=root&password=taosdata";
Connection conn = DriverManager.getConnection(url, properties); Connection conn = DriverManager.getConnection(url, properties);
try (Statement stmt = conn.createStatement()) { Statement stmt = conn.createStatement();
stmt.execute("drop database if exists inWithoutDb"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists inWithoutDb"); stmt.execute("create database if not exists " + dbname);
stmt.execute("create table inWithoutDb.weather(ts timestamp, f1 int)"); stmt.execute("create table " + dbname + ".weather(ts timestamp, f1 int)");
} stmt.close();
conn.close();
// execute // execute
conn = DriverManager.getConnection(url, properties); stmt = conn.createStatement();
try (Statement stmt = conn.createStatement()) { int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")");
int affectedRow = stmt.executeUpdate("insert into weather(ts, f1) values(now," + random.nextInt(100) + ")"); Assert.assertEquals(1, affectedRow);
Assert.assertEquals(1, affectedRow); boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")");
boolean flag = stmt.execute("insert into weather(ts, f1) values(now + 10s," + random.nextInt(100) + ")"); Assert.assertEquals(false, flag);
Assert.assertEquals(false, flag); ResultSet rs = stmt.executeQuery("select count(*) from weather");
ResultSet rs = stmt.executeQuery("select count(*) from weather"); rs.next();
rs.next(); int count = rs.getInt("count(*)");
int count = rs.getInt("count(*)"); Assert.assertEquals(2, count);
Assert.assertEquals(2, count); stmt.execute("drop database if exists " + dbname);
stmt.close();
} catch (SQLException e) { conn.close();
e.printStackTrace();
}
} }
@BeforeClass @BeforeClass
......
...@@ -427,8 +427,12 @@ public class InsertSpecialCharacterJniTest { ...@@ -427,8 +427,12 @@ public class InsertSpecialCharacterJniTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbName);
statement.close();
conn.close(); conn.close();
}
} }
} }
...@@ -391,8 +391,12 @@ public class InsertSpecialCharacterRestfulTest { ...@@ -391,8 +391,12 @@ public class InsertSpecialCharacterRestfulTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists "+ dbName);
statement.close();
conn.close(); conn.close();
}
} }
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.AfterClass;
import org.junit.Test; import org.junit.Test;
import java.sql.*; import java.sql.*;
public class JDBCTypeAndTypeCompareTest { public class JDBCTypeAndTypeCompareTest {
private static Connection conn;
private static final String dbname = "test";
@Test @Test
public void test() throws SQLException { public void test() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:TAOS://192.168.17.156:6030/", "root", "taosdata"); conn = DriverManager.getConnection("jdbc:TAOS://127.0.0.1:6030/", "root", "taosdata");
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute("drop database if exists test"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists test"); stmt.execute("create database if not exists " + dbname);
stmt.execute("use test"); stmt.execute("use " + dbname);
stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(10), f9 nchar(10) )"); stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(10), f9 nchar(10) )");
stmt.execute("insert into weather values(now, 1, 2, 3.0, 4.0, 5, 6, true, 'test','test')"); stmt.execute("insert into weather values(now, 1, 2, 3.0, 4.0, 5, 6, true, 'test','test')");
...@@ -29,6 +32,19 @@ public class JDBCTypeAndTypeCompareTest { ...@@ -29,6 +32,19 @@ public class JDBCTypeAndTypeCompareTest {
} }
stmt.close(); stmt.close();
conn.close(); }
@AfterClass
public static void afterClass() {
try {
if (null != conn) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
...@@ -47,7 +47,7 @@ public class MicroSecondPrecisionJNITest { ...@@ -47,7 +47,7 @@ public class MicroSecondPrecisionJNITest {
Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos); Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos);
ts = rs.getLong(1); ts = rs.getLong(1);
Assert.assertEquals(timestamp1, ts); Assert.assertEquals(timestamp2, ts);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -79,8 +79,13 @@ public class MicroSecondPrecisionJNITest { ...@@ -79,8 +79,13 @@ public class MicroSecondPrecisionJNITest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database " + ms_timestamp_db);
statement.execute("drop database " + us_timestamp_db);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -47,7 +47,7 @@ public class MicroSecondPrecisionRestfulTest { ...@@ -47,7 +47,7 @@ public class MicroSecondPrecisionRestfulTest {
Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos); Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos);
ts = rs.getLong(1); ts = rs.getLong(1);
Assert.assertEquals(timestamp1, ts); Assert.assertEquals(timestamp2, ts);
} }
} }
...@@ -77,7 +77,7 @@ public class MicroSecondPrecisionRestfulTest { ...@@ -77,7 +77,7 @@ public class MicroSecondPrecisionRestfulTest {
Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos); Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos);
ts = rs.getLong(1); ts = rs.getLong(1);
Assert.assertEquals(timestamp1, ts); Assert.assertEquals(timestamp2, ts);
} }
} }
...@@ -107,7 +107,7 @@ public class MicroSecondPrecisionRestfulTest { ...@@ -107,7 +107,7 @@ public class MicroSecondPrecisionRestfulTest {
Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos); Assert.assertEquals(timestamp2 % 1000_000l * 1000, nanos);
ts = rs.getLong(1); ts = rs.getLong(1);
Assert.assertEquals(timestamp1, ts); Assert.assertEquals(timestamp2, ts);
} }
} }
...@@ -142,12 +142,21 @@ public class MicroSecondPrecisionRestfulTest { ...@@ -142,12 +142,21 @@ public class MicroSecondPrecisionRestfulTest {
} }
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() {
if (conn1 != null) try {
conn1.close(); if (conn1 != null) {
if (conn2 != null) Statement statement = conn1.createStatement();
conn2.close(); statement.execute("drop database if exists " + ms_timestamp_db);
if (conn3 != null) statement.execute("drop database if exists " + us_timestamp_db);
conn3.close(); statement.close();
conn1.close();
}
if (conn2 != null)
conn2.close();
if (conn3 != null)
conn3.close();
}catch (SQLException e){
e.printStackTrace();
}
} }
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -90,4 +91,16 @@ public class MultiConnectionWithDifferentDbTest { ...@@ -90,4 +91,16 @@ public class MultiConnectionWithDifferentDbTest {
} }
} }
@After
public void after() {
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
try (Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement()) {
statement.execute("drop database if exists " + db1);
statement.execute("drop database if exists " + db2);
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.Assert; import org.junit.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
...@@ -83,9 +80,9 @@ public class NanoSecondTimestampJNITest { ...@@ -83,9 +80,9 @@ public class NanoSecondTimestampJNITest {
// then // then
long actual = rs.getLong(1); long actual = rs.getLong(1);
Assert.assertEquals(ms, actual); Assert.assertEquals(ns, actual);
actual = rs.getLong("ts"); actual = rs.getLong("ts");
Assert.assertEquals(ms, actual); Assert.assertEquals(ns, actual);
} }
@Test @Test
...@@ -160,4 +157,18 @@ public class NanoSecondTimestampJNITest { ...@@ -160,4 +157,18 @@ public class NanoSecondTimestampJNITest {
} }
} }
@AfterClass
public static void afterClass(){
try {
if (null != conn){
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
} }
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.Assert; import org.junit.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
...@@ -83,9 +80,9 @@ public class NanoSecondTimestampRestfulTest { ...@@ -83,9 +80,9 @@ public class NanoSecondTimestampRestfulTest {
// then // then
long actual = rs.getLong(1); long actual = rs.getLong(1);
Assert.assertEquals(ms, actual); Assert.assertEquals(ns, actual);
actual = rs.getLong("ts"); actual = rs.getLong("ts");
Assert.assertEquals(ms, actual); Assert.assertEquals(ns, actual);
} }
@Test @Test
...@@ -160,4 +157,14 @@ public class NanoSecondTimestampRestfulTest { ...@@ -160,4 +157,14 @@ public class NanoSecondTimestampRestfulTest {
} }
} }
@AfterClass
public static void afterClass() throws SQLException {
if (conn != null){
try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists " + dbname);
}
conn.close();
}
}
} }
...@@ -45,7 +45,11 @@ public class NullValueInResultSetJNITest { ...@@ -45,7 +45,11 @@ public class NullValueInResultSetJNITest {
@After @After
public void after() throws SQLException { public void after() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists test_null");
statement.close();
conn.close(); conn.close();
}
} }
} }
...@@ -47,7 +47,11 @@ public class NullValueInResultSetRestfulTest { ...@@ -47,7 +47,11 @@ public class NullValueInResultSetRestfulTest {
@After @After
public void after() throws SQLException { public void after() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists test_null");
statement.close();
conn.close(); conn.close();
}
} }
} }
...@@ -85,7 +85,11 @@ public class NullValueInResultSetTest { ...@@ -85,7 +85,11 @@ public class NullValueInResultSetTest {
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn_restful != null) if (conn_restful != null)
conn_restful.close(); conn_restful.close();
if (conn_jni != null) if (conn_jni != null) {
Statement statement = conn_jni.createStatement();
statement.execute("drop database if exists test_null");
statement.close();
conn_jni.close(); conn_jni.close();
}
} }
} }
...@@ -62,8 +62,12 @@ public class TimestampPrecisionInNanoInJniTest { ...@@ -62,8 +62,12 @@ public class TimestampPrecisionInNanoInJniTest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + ns_timestamp_db);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -83,7 +87,7 @@ public class TimestampPrecisionInNanoInJniTest { ...@@ -83,7 +87,7 @@ public class TimestampPrecisionInNanoInJniTest {
int nanos = rs.getTimestamp(1).getNanos(); int nanos = rs.getTimestamp(1).getNanos();
Assert.assertEquals(ts % 1000_000_000l, nanos); Assert.assertEquals(ts % 1000_000_000l, nanos);
long test_ts = rs.getLong(1); long test_ts = rs.getLong(1);
Assert.assertEquals(ts / 1000_000l, test_ts); Assert.assertEquals(ts, test_ts);
} }
@Test @Test
......
...@@ -62,8 +62,12 @@ public class TimestampPrecisonInNanoRestTest { ...@@ -62,8 +62,12 @@ public class TimestampPrecisonInNanoRestTest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + ns_timestamp_db);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -83,7 +87,7 @@ public class TimestampPrecisonInNanoRestTest { ...@@ -83,7 +87,7 @@ public class TimestampPrecisonInNanoRestTest {
int nanos = rs.getTimestamp(1).getNanos(); int nanos = rs.getTimestamp(1).getNanos();
Assert.assertEquals(ts % 1000_000_000l, nanos); Assert.assertEquals(ts % 1000_000_000l, nanos);
long test_ts = rs.getLong(1); long test_ts = rs.getLong(1);
Assert.assertEquals(ts / 1000_000l, test_ts); Assert.assertEquals(ts, test_ts);
} }
@Test @Test
......
...@@ -156,8 +156,12 @@ public class UnsignedNumberJniTest { ...@@ -156,8 +156,12 @@ public class UnsignedNumberJniTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists unsign_jni");
statement.close();
conn.close(); conn.close();
}
} }
} }
...@@ -14,6 +14,7 @@ public class UnsignedNumberRestfulTest { ...@@ -14,6 +14,7 @@ public class UnsignedNumberRestfulTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private static Connection conn; private static Connection conn;
private static long ts; private static long ts;
private static final String dbname = "unsign_restful";
@Test @Test
public void testCase001() throws SQLException { public void testCase001() throws SQLException {
...@@ -148,9 +149,9 @@ public class UnsignedNumberRestfulTest { ...@@ -148,9 +149,9 @@ public class UnsignedNumberRestfulTest {
final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"; final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
conn = DriverManager.getConnection(url, properties); conn = DriverManager.getConnection(url, properties);
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute("drop database if exists unsign_restful"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists unsign_restful"); stmt.execute("create database if not exists " + dbname);
stmt.execute("use unsign_restful"); stmt.execute("use " + dbname);
stmt.execute("create table us_table(ts timestamp, f1 tinyint unsigned, f2 smallint unsigned, f3 int unsigned, f4 bigint unsigned)"); stmt.execute("create table us_table(ts timestamp, f1 tinyint unsigned, f2 smallint unsigned, f3 int unsigned, f4 bigint unsigned)");
stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + ts + ", 127, 32767,2147483647, 9223372036854775807)"); stmt.executeUpdate("insert into us_table(ts,f1,f2,f3,f4) values(" + ts + ", 127, 32767,2147483647, 9223372036854775807)");
stmt.close(); stmt.close();
...@@ -162,8 +163,12 @@ public class UnsignedNumberRestfulTest { ...@@ -162,8 +163,12 @@ public class UnsignedNumberRestfulTest {
@AfterClass @AfterClass
public static void afterClass() { public static void afterClass() {
try { try {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package com.taosdata.jdbc.cases; package com.taosdata.jdbc.cases;
import org.junit.AfterClass;
import org.junit.Test; import org.junit.Test;
import java.sql.*; import java.sql.*;
...@@ -8,7 +9,7 @@ import static org.junit.Assert.assertEquals; ...@@ -8,7 +9,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class UseNowInsertTimestampTest { public class UseNowInsertTimestampTest {
String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata"; private static String url = "jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata";
@Test @Test
public void millisec() throws SQLException { public void millisec() throws SQLException {
...@@ -55,13 +56,14 @@ public class UseNowInsertTimestampTest { ...@@ -55,13 +56,14 @@ public class UseNowInsertTimestampTest {
@Test @Test
public void nanosec() throws SQLException { public void nanosec() throws SQLException {
long now_time = System.currentTimeMillis() * 1000_000L + System.nanoTime() % 1000_000L;
try (Connection conn = DriverManager.getConnection(url)) { try (Connection conn = DriverManager.getConnection(url)) {
Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement();
stmt.execute("drop database if exists test"); stmt.execute("drop database if exists test");
stmt.execute("create database if not exists test precision 'ns'"); stmt.execute("create database if not exists test precision 'ns'");
stmt.execute("use test"); stmt.execute("use test");
stmt.execute("create table weather(ts timestamp, f1 int)"); stmt.execute("create table weather(ts timestamp, f1 int)");
stmt.execute("insert into weather values(now, 1)"); stmt.execute("insert into weather values(" + now_time + ", 1)");
ResultSet rs = stmt.executeQuery("select * from weather"); ResultSet rs = stmt.executeQuery("select * from weather");
rs.next(); rs.next();
...@@ -74,4 +76,15 @@ public class UseNowInsertTimestampTest { ...@@ -74,4 +76,15 @@ public class UseNowInsertTimestampTest {
stmt.execute("drop database if exists test"); stmt.execute("drop database if exists test");
} }
} }
@AfterClass
public static void afterClass() {
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test");
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
...@@ -45,7 +45,11 @@ public class BadLocaleSettingTest { ...@@ -45,7 +45,11 @@ public class BadLocaleSettingTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database " + dbName);
statement.close();
conn.close(); conn.close();
}
} }
} }
\ No newline at end of file
...@@ -79,4 +79,15 @@ public class BatchFetchTest { ...@@ -79,4 +79,15 @@ public class BatchFetchTest {
} }
return builder.toString(); return builder.toString();
} }
@AfterClass
public static void afterClass(){
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test");
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
package com.taosdata.jdbc.confprops; package com.taosdata.jdbc.confprops;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -37,4 +38,18 @@ public class CharsetTest { ...@@ -37,4 +38,18 @@ public class CharsetTest {
} }
} }
@AfterClass
public static void afterClass(){
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
try (Connection conn = DriverManager.getConnection(url, props);
Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test");
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
...@@ -29,7 +29,7 @@ public class TimeZoneTest { ...@@ -29,7 +29,7 @@ public class TimeZoneTest {
} }
@Test @Test
public void taosTimeZone() { public void taosTimeZone() throws SQLException {
// given // given
Properties props = new Properties(); Properties props = new Properties();
props.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); props.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
...@@ -39,7 +39,7 @@ public class TimeZoneTest { ...@@ -39,7 +39,7 @@ public class TimeZoneTest {
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
stmt.execute("drop database if exists timezone_test"); stmt.execute("drop database if exists timezone_test");
stmt.execute("create database if not exists timezone_test keep 365000"); stmt.execute("create database if not exists timezone_test keep 36500");
stmt.execute("use timezone_test"); stmt.execute("use timezone_test");
stmt.execute("create table weather(ts timestamp, temperature float)"); stmt.execute("create table weather(ts timestamp, temperature float)");
...@@ -51,7 +51,7 @@ public class TimeZoneTest { ...@@ -51,7 +51,7 @@ public class TimeZoneTest {
System.out.println("ts: " + ts.getTime() + "," + ts); System.out.println("ts: " + ts.getTime() + "," + ts);
} }
stmt.execute("insert into timezone_test.weather(ts, temperature, humidity) values('1970-01-02 00:00:00', 1.0, 2.0)"); stmt.execute("insert into timezone_test.weather(ts, temperature) values('1970-01-02 00:00:00', 1.0)");
rs = stmt.executeQuery("select * from timezone_test.weather"); rs = stmt.executeQuery("select * from timezone_test.weather");
while (rs.next()) { while (rs.next()) {
...@@ -63,8 +63,6 @@ public class TimeZoneTest { ...@@ -63,8 +63,6 @@ public class TimeZoneTest {
stmt.execute("drop database if exists timezone_test"); stmt.execute("drop database if exists timezone_test");
stmt.close(); stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} }
} }
......
package com.taosdata.jdbc.confprops; package com.taosdata.jdbc.confprops;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
import org.junit.Assert; import org.junit.*;
import org.junit.Before;
import org.junit.Test;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.util.Calendar;
import java.util.Properties; import java.util.Properties;
public class TimestampFormatTest { public class TimestampFormatTest {
private static final String host = "127.0.0.1"; private static final String host = "127.0.0.1";
private long ts = Instant.now().toEpochMilli(); private long ts = Instant.now().toEpochMilli();
private Connection conn;
@Test @Test
public void string() throws SQLException { public void string() throws SQLException {
...@@ -154,13 +154,27 @@ public class TimestampFormatTest { ...@@ -154,13 +154,27 @@ public class TimestampFormatTest {
@Before @Before
public void before() throws SQLException { public void before() throws SQLException {
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"; String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(url); conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) { Statement stmt = conn.createStatement();
stmt.execute("drop database if exists test"); stmt.execute("drop database if exists test");
stmt.execute("create database if not exists test"); stmt.execute("create database if not exists test");
stmt.execute("use test"); stmt.execute("use test");
stmt.execute("create table weather(ts timestamp, temperature nchar(10))"); stmt.execute("create table weather(ts timestamp, temperature nchar(10))");
stmt.execute("insert into weather values(" + ts + ", '北京')"); stmt.execute("insert into weather values(" + ts + ", '北京')");
stmt.close();
}
@After
public void after() {
try {
if (null != conn) {
Statement stmt = conn.createStatement();
stmt.execute("drop database if exists test");
stmt.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} }
} }
......
...@@ -33,7 +33,6 @@ public class DatabaseSpecifiedTest { ...@@ -33,7 +33,6 @@ public class DatabaseSpecifiedTest {
String loc = rs.getString("loc"); String loc = rs.getString("loc");
assertEquals("beijing", loc); assertEquals("beijing", loc);
} }
connection.close();
} }
@Before @Before
...@@ -59,8 +58,12 @@ public class DatabaseSpecifiedTest { ...@@ -59,8 +58,12 @@ public class DatabaseSpecifiedTest {
@After @After
public void after() { public void after() {
try { try {
if (connection != null) if (connection != null) {
Statement statement = connection.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
connection.close(); connection.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -383,8 +383,12 @@ public class RestfulConnectionTest { ...@@ -383,8 +383,12 @@ public class RestfulConnectionTest {
@AfterClass @AfterClass
public static void afterClass() throws SQLException { public static void afterClass() throws SQLException {
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists test");
statement.close();
conn.close(); conn.close();
}
} }
} }
\ No newline at end of file
...@@ -18,6 +18,7 @@ public class RestfulParameterMetaDataTest { ...@@ -18,6 +18,7 @@ public class RestfulParameterMetaDataTest {
private static PreparedStatement pstmt_select; private static PreparedStatement pstmt_select;
private static ParameterMetaData parameterMetaData_insert; private static ParameterMetaData parameterMetaData_insert;
private static ParameterMetaData parameterMetaData_select; private static ParameterMetaData parameterMetaData_select;
private static final String dbname = "test_pstmt";
@Test @Test
public void getParameterCount() throws SQLException { public void getParameterCount() throws SQLException {
...@@ -148,9 +149,9 @@ public class RestfulParameterMetaDataTest { ...@@ -148,9 +149,9 @@ public class RestfulParameterMetaDataTest {
Class.forName("com.taosdata.jdbc.rs.RestfulDriver"); Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"); conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata");
try (Statement stmt = conn.createStatement()) { try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_pstmt"); stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists test_pstmt"); stmt.execute("create database if not exists " + dbname);
stmt.execute("use test_pstmt"); stmt.execute("use " + dbname);
stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))"); stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))");
stmt.execute("create table t1 using weather tags('beijing')"); stmt.execute("create table t1 using weather tags('beijing')");
} }
...@@ -186,8 +187,12 @@ public class RestfulParameterMetaDataTest { ...@@ -186,8 +187,12 @@ public class RestfulParameterMetaDataTest {
pstmt_insert.close(); pstmt_insert.close();
if (pstmt_select != null) if (pstmt_select != null)
pstmt_select.close(); pstmt_select.close();
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -400,8 +400,12 @@ public class RestfulPreparedStatementTest { ...@@ -400,8 +400,12 @@ public class RestfulPreparedStatementTest {
pstmt_select.close(); pstmt_select.close();
if (pstmt_without_parameters != null) if (pstmt_without_parameters != null)
pstmt_without_parameters.close(); pstmt_without_parameters.close();
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists test_pstmt");
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -15,6 +15,7 @@ public class RestfulResultSetMetaDataTest { ...@@ -15,6 +15,7 @@ public class RestfulResultSetMetaDataTest {
private static Statement stmt; private static Statement stmt;
private static ResultSet rs; private static ResultSet rs;
private static ResultSetMetaData meta; private static ResultSetMetaData meta;
private static final String dbname = "restful_test";
@Test @Test
public void getColumnCount() throws SQLException { public void getColumnCount() throws SQLException {
...@@ -206,8 +207,12 @@ public class RestfulResultSetMetaDataTest { ...@@ -206,8 +207,12 @@ public class RestfulResultSetMetaDataTest {
rs.close(); rs.close();
if (stmt != null) if (stmt != null)
stmt.close(); stmt.close();
if (conn != null) if (conn != null) {
Statement statement = conn.createStatement();
statement.execute("drop database if exists " + dbname);
statement.close();
conn.close(); conn.close();
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册