change
This commit is contained in:
parent
2839d49757
commit
70bed13745
|
@ -9,6 +9,14 @@
|
|||
<version>SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.18</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -48,12 +56,4 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.18</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -5,7 +5,6 @@ import java.util.Properties;
|
|||
|
||||
public class JDBCDemo {
|
||||
private static String host;
|
||||
private static String driverType = "jni";
|
||||
private static final String dbName = "test";
|
||||
private static final String tbName = "weather";
|
||||
private Connection connection;
|
||||
|
@ -14,17 +13,10 @@ public class JDBCDemo {
|
|||
for (int i = 0; i < args.length; i++) {
|
||||
if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1)
|
||||
host = args[++i];
|
||||
if ("-driverType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
driverType = args[++i];
|
||||
if (!"jni".equalsIgnoreCase(driverType) && !"restful".equalsIgnoreCase(driverType))
|
||||
printHelp();
|
||||
}
|
||||
}
|
||||
|
||||
if (host == null) {
|
||||
printHelp();
|
||||
}
|
||||
|
||||
JDBCDemo demo = new JDBCDemo();
|
||||
demo.init();
|
||||
demo.createDatabase();
|
||||
|
@ -38,15 +30,10 @@ public class JDBCDemo {
|
|||
}
|
||||
|
||||
private void init() {
|
||||
final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
|
||||
// get connection
|
||||
try {
|
||||
String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
|
||||
if (driverType.equals("restful")) {
|
||||
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
|
||||
} else {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
}
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("charset", "UTF-8");
|
||||
properties.setProperty("locale", "en_US.UTF-8");
|
||||
|
@ -70,11 +57,39 @@ public class JDBCDemo {
|
|||
exuete(sql);
|
||||
}
|
||||
|
||||
private void dropTable() {
|
||||
final String sql = "drop table if exists " + dbName + "." + tbName + "";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private void createTable() {
|
||||
final String sql = "create table if not exists " + dbName + "." + tbName + " (ts timestamp, temperature float, humidity int)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private void insert() {
|
||||
final String sql = "insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private void select() {
|
||||
final String sql = "select * from test.weather";
|
||||
executeQuery(sql);
|
||||
}
|
||||
|
||||
private void close() {
|
||||
try {
|
||||
if (connection != null) {
|
||||
this.connection.close();
|
||||
System.out.println("connection closed.");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
|
||||
private void executeQuery(String sql) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
long start = System.currentTimeMillis();
|
||||
|
@ -99,15 +114,6 @@ public class JDBCDemo {
|
|||
}
|
||||
}
|
||||
|
||||
private void insert() {
|
||||
final String sql = "insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private void createTable() {
|
||||
final String sql = "create table if not exists " + dbName + "." + tbName + " (ts timestamp, temperature float, humidity int)";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private void printSql(String sql, boolean succeed, long cost) {
|
||||
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
|
||||
|
@ -125,22 +131,6 @@ public class JDBCDemo {
|
|||
}
|
||||
}
|
||||
|
||||
private void close() {
|
||||
try {
|
||||
if (connection != null) {
|
||||
this.connection.close();
|
||||
System.out.println("connection closed.");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void dropTable() {
|
||||
final String sql = "drop table if exists " + dbName + "." + tbName + "";
|
||||
exuete(sql);
|
||||
}
|
||||
|
||||
private static void printHelp() {
|
||||
System.out.println("Usage: java -jar JdbcDemo.jar -host <hostname> -driverType <jni|restful>");
|
||||
System.exit(0);
|
||||
|
|
|
@ -14,9 +14,9 @@ public class JdbcRestfulDemo {
|
|||
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
|
||||
|
||||
Properties properties = new Properties();
|
||||
// properties.setProperty("charset", "UTF-8");
|
||||
// properties.setProperty("locale", "en_US.UTF-8");
|
||||
// properties.setProperty("timezone", "UTC-8");
|
||||
properties.setProperty("charset", "UTF-8");
|
||||
properties.setProperty("locale", "en_US.UTF-8");
|
||||
properties.setProperty("timezone", "UTC-8");
|
||||
|
||||
Connection conn = DriverManager.getConnection(url, properties);
|
||||
Statement stmt = conn.createStatement();
|
||||
|
|
|
@ -13,7 +13,6 @@ public class SubscribeDemo {
|
|||
public static TSDBConnection getConnection(String host, String database) throws Exception {
|
||||
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");
|
||||
|
|
Loading…
Reference in New Issue