tencent cloud

Feedback

Phoenix JDBC Usage

Last updated: 2022-11-25 16:06:47

    Adding Maven dependencies

    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-core</artifactId>
        <version>${phoenix.version}</version>
    </dependency>
    

    Here, phoenix.version should be consistent with the Phoenix version in the cluster.

    Creating a JDBC object

    Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
           // Connect to the database
       connection = DriverManager.getConnection("jdbc:phoenix:10.0.0.3:2181,10.0.0.5:2181,10.0.0.8:2181");
    

    Running a query

    private static void instertPhoenix(Connection connection)throws Exception{
        String sql="upsert into album_subscribe_log(id,album_id,user_id,op_time,sub_flag,is_optimize,type_parent_id,type_id,host_id,is_pay,user_type,identtity_typ)"
        +" values(?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps=connection.prepareStatement(sql);
        ps.setLong(0,1);
        ps.setLong(1,3);
        ps.setLong(2,1);
        ps.setString(3,"2017-09-05 14:00:00");
        ps.setInt(4,1);
        ps.setString(5,"1");
        ps.setInt(6,3);
        ps.setInt(7,5);
        ps.setInt(8,6);
        ps.setInt(9,7);
        ps.setString(10,"1");
        ps.setString(11,"1");
        ps.setString(12,"1");
        ps.executeUpdate();
        ps.close();
        connection.commit();
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support