栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java连接ClickHouse实现数据库基本增删查

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java连接ClickHouse实现数据库基本增删查

引言

ClickHose作为一个新的列式数据库,其在实时数仓中作为结果存储的数据库。对于Java程序员来说,我们需要对于ClickHouse进行数据的增删查改,进而支持后续的处理业务。我使用面向对象的思想,实现Java操作ClickHouse的增删改,希望对于学习和使用ClickHouse的你有些帮助。

一、依赖导入

	ru.yandex.clickhouse
	clickhouse-jdbc
	0.1.52

二、连接实体类
public class ConnEntiy {
    // 驱动器
    String driverName;
    // 连接对象地址
    String url;
    // 用户
    String user;
    // 密码
    String password;

    public ConnEntiy() {
    }

    public ConnEntiy(String driverName, String url, String user, String password) {
        this.driverName = driverName;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setDriverName(String driverName) {
        this.driverName = driverName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
三、接口类
public  interface Utils {
    public Connection connection(ConnEntiy connEntiy);

    public void  close(AutoCloseable... closes);
    public boolean insert(Connection connection,String sql,String...params);
    public boolean delete(Connection connection,String sql,String...params);
    public ResultSet QueryResultSet(Connection connection,String sql,String...params);
}
四、接口实现类
public class ClickHouseUtils implements Utils{

    @Override
    public  Connection connection(ConnEntiy connEntiy) {
        Connection conn=null;
        try {
            Class.forName(connEntiy.getDriverName());
            conn= DriverManager.getConnection(connEntiy.getUrl(),connEntiy.getUser(),connEntiy.getPassword());
        }catch(Exception e){
            System.out.println("connection fail ,please check your entities");
        }
        return conn;
    }

    @Override
    public void close(AutoCloseable... closes) {
        for (AutoCloseable close : closes) {
            if (close!=null) {
                try {
                    close.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    close=null;
                }
            }
        }
    }

    @Override
    public boolean  insert(Connection connection, String sql, String... params) {
        boolean b =false;
        ClickHousePreparedStatement pst=null;
        if (connection==null) {
            System.out.println("connection is empty");
            System.exit(-1);
        }
        try {
            pst= (ClickHousePreparedStatement) connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i+1,params[i]);
            }
            b= pst.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            close(pst,connection);
        }

        return b;
    }

    @Override
    public boolean delete(Connection connection, String sql, String... params) {
        boolean b =false;
        ClickHousePreparedStatement pst=null;
        if (connection==null) {
            System.out.println("connection is empty");
            System.exit(-1);
        }
        try {
            pst= (ClickHousePreparedStatement) connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i+1,params[i]);
            }
            b= pst.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            close(pst,connection);
        }

        return b;
    }

    @Override
    public ResultSet QueryResultSet(Connection connection, String sql, String... params) {
        ResultSet rst=null;
        ClickHousePreparedStatement pst=null;
        if (connection==null) {
            System.out.println("connection is empty");
            System.exit(-1);
        }
        try {
            pst= (ClickHousePreparedStatement) connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i+1,params[i]);
            }
            rst = pst.executeQuery();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            close(rst,pst,connection);
        }
        return rst;
    }
}

五、测试类
public class test {
    public static void main(String[] args)  {
        String driverName="ru.yandex.clickhouse.ClickHouseDriver";
        String url="jdbc:clickhouse://192.168.50.100:9000/test";
        String user="root";
        String password="root";
        String sql="select * from test;";
        String[] params={};
        ConnEntiy connEntiy = new ConnEntiy(driverName, url, user, password);
        Utils utils = new ClickHouseUtils();
        Connection conn = utils.connection(connEntiy);
        utils.insert(conn,sql,params);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/851109.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号