packagecom.czxk.cjc.utils;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.sql.Statement;/***

*@authorsunzhenyang

**/

public classIoTdbUtil {private final String IOTDB_DRIVER = "cn.edu.tsinghua.iotdb.jdbc.TsfileDriver";public static Connection connection = null;

String url= "jdbc:tsfile://192.168.1.120:6667/"; //:JDBC 要连接的地址

String username = "root"; //:用户名,默认为 root

String password ="root";//:密码,默认为 root

/*** 初始化参数

*@paramurl

*@paramusername

*@parampassword

*@throwsClassNotFoundException

*@throwsSQLException*/

public IoTdbUtil(String url,String username,String password) throwsClassNotFoundException, SQLException {if(!"".equals(StringTools.emptyRetStr(url)))this.url =url;if(!"".equals(StringTools.emptyRetStr(username)))this.username =username;if(!"".equals(StringTools.emptyRetStr(password)))this.password =password;this.createConnection();

}/*** 创建连接

*@throwsClassNotFoundException

*@throwsSQLException*/

private void createConnection() throwsClassNotFoundException, SQLException {try{//注册 IoTDB 的 JDBC 驱动

Class.forName(IOTDB_DRIVER);//使用 DriverManager 连接 IoTDB

connection =DriverManager.getConnection(url, username, password);

}catch(SQLException ex) {//handle any errors

System.out.println("SQLException: " +ex.getMessage());

System.out.println("SQLState: " +ex.getSQLState());

System.out.println("VendorError: " +ex.getErrorCode());

}

}/*** 执行查询 SQL

*@paramgroupName 组名

*@paramtimeName 时间序列名

*@throwsSQLException*/

public void executeSelect(String groupName,String timeName) throwsSQLException {//查询测控 sgcc 集团 wf03 子站 wt01 设备温度

Statement statement = null;

ResultSet resultSet= null;try{

statement=connection.createStatement();//statement.execute("select status from root.ln.wf01");

statement.execute("select "+timeName+" from " +groupName);

resultSet=statement.getResultSet();while(resultSet.next()){//结果集下标从 1 开始

System.out.println(String.format("status %s, value %s", resultSet.getString(1), resultSet.getString(2)));

}

}catch(SQLException ex){//handle any errors

System.out.println("SQLException: " +ex.getMessage());

System.out.println("SQLState: " +ex.getSQLState());

System.out.println("VendorError: " +ex.getErrorCode());

}finally{if(statement != null) {

statement.close();

}

}

}/*** 执行查询 SQL

*@paramsql 传入SQL

*@throwsSQLException*/

public void executeSelect(String sql) throwsSQLException {

PreparedStatement preparedStatement=connection.prepareStatement(sql);try{//ResultSet resultSet = preparedStatement.executeQuery("select * from root ");

ResultSet resultSet =preparedStatement.executeQuery(sql);

ResultSetMetaData resultSetMetaData=resultSet.getMetaData();while(resultSet.next()){

StringBuilder builder= newStringBuilder();for (int i = 1; i <= resultSetMetaData.getColumnCount();i++) {

builder.append(resultSet.getString(i)).append(",");

}

System.out.println(builder);

}

}catch(Exception ex) {

}finally{

preparedStatement.close();

}

}/*** 关闭连接

*@throwsSQLException*/

public void closes() throwsSQLException {if(connection != null) {

connection.close();

}

}/*** 测试

*@paramargs

*@throwsSQLException

*@throwsClassNotFoundException*/

public static void main(String[] args) throwsClassNotFoundException, SQLException {

IoTdbUtil it= new IoTdbUtil(null, null, null);try{//测试添加数据

String sql = "insert into root.cjc(timestamp,cjcdata1,cjcdata2) values(?,?,?)";

PreparedStatement preparedStatement=connection.prepareStatement(sql);

preparedStatement.setLong(1, 123456L);

preparedStatement.setString(2, "haha哈哈");

preparedStatement.setString(3, "haha哈哈");

preparedStatement.execute();//清除上一次的数据新加数据 继续添加

preparedStatement.clearParameters();

preparedStatement.setLong(1, 654321L);

preparedStatement.setString(2, "haha哈哈");

preparedStatement.setString(3, "haha哈哈");

preparedStatement.execute();

preparedStatement.close();//获取刚添加的数据

it.executeSelect("select * from root.cjc");

}catch(Exception e) {

e.printStackTrace();

}finally{

it.closes();

}

}

}

Logo

一站式 AI 云服务平台

更多推荐