简体   繁体   中英

package com.datastax.driver.core does not exist

I'm begginner in cassandra, I'm trying to connect to cassandra database and insert some rows in netbeans but I have error under the import: package com.datastax.driver.core does not exist any solutions?


import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class CassandraConnection {
    public static void main(String args[])
    {
        System.out.println("Cassandra Java Connection");
        Cluster cluster;
        Session session;
        //Connect to the cluster and Keyspace ecommerce
        cluster=Cluster.builder().addContactPoint("localhost").build();
        session=cluster.connect("ecommerce");
        System.out.println("Inserting Data in Cassandra");
        session.execute("INSERT INTO products (pdt_id, cat_id, pdt_name, pdt_desc, price, shipping) VALUES (001,104, 'Danby 0.7 cu. ft. Microwave Oven', 'Capacity of 0.7 cu. ft.10 different power levels', 54.00, 'Expedited')");
        String pdtid = null, pdtname = null, pdtdesc = null;
        float price = 0;
        ResultSet resultset=session.execute("select * from products where pdt_id=005");
        for(Row row :resultset){
            pdtid = Integer.toString(row.getInt("pdt_id"));
            pdtname = row.getString("pdt_name");
            pdtdesc = row.getString("pdt_desc");
            price = row.getFloat("price");
            
            
        System.out.println("Product ID: " + pdtid);
        System.out.println("Name: " + pdtname);
        System.out.println("Description: " + pdtdesc);
        System.out.println("Price: "+ price);


        }
        cluster.close();
        }

}```

It looks like you haven't configured the dependencies correctly so it can't find the Java driver package. As Andreas also pointed out, it appears you're using old code which has been refactored in Java driver v4.x.

Since your new to Cassandra, I recommend having a look at https://www.datastax.com/dev where we have free code samples and interactive tutorials using Katacoda.

For example, learn CRUD operations on Cassandra with the Java driver in this hands-on tutorial which launches a Cassandra cluster + a dev environment where you can code -- all in a single browser window: https://www.datastax.com/dev/scenario/datastax-java-driver-apache-cassandratm-quickstart . All free with no login required.

You can also have a look at the code samples on Astra and learn how to develop apps using REST API, GraphQL API or Document API without any Cassandra knowledge. Cheers!

try to add maven dependency (or jar file to your project)

<!-- https://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-mapping -->
        <dependency>
            <groupId>com.datastax.cassandra</groupId>
            <artifactId>cassandra-driver-mapping</artifactId>
            <version>3.11.0</version>
        </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM