当前位置:   article > 正文

cassandra 1.2 jdbc 实例_org.apache.cassandra.cql.jdbc.cassandradriver pom

org.apache.cassandra.cql.jdbc.cassandradriver pom


import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;


public class CassandraCql {
    private static java.sql.Connection con = null;


    public static void main(String[] a) {
        try {
            Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
            con = DriverManager
                    .getConnection("jdbc:cassandra://192.168.2.14:9160/mykeyspace");
            CassandraCql sample = new CassandraCql();
            String Columnname = "subject";


            /* -- Functions to perform on Keyspace -- */
            createColumnFamily();
            pouplateData();
        //    deleteData();
        //    updateData();
            listData();
            dropColumnFamily("news");


        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void createColumnFamily() throws SQLException {
        


        String data = "CREATE columnfamily news (key int primary key, category text , linkcounts int ,url text)";
        Statement st = con.createStatement();
        st.execute(data);
    }


    public static void dropColumnFamily(String name) throws SQLException {
        String data = "drop columnfamily " + name + ";";
        Statement st = con.createStatement();
        st.execute(data);
    }


    public static void pouplateData() throws SQLException {
        String data = "BEGIN BATCH \n"
                + "insert into news (key, category, linkcounts,url) values (5,'class',71,'news.com') \n"
                + "insert into news (key, category, linkcounts,url) values (6,'education',15,'tech.com') \n"
                + "insert into news (key, category, linkcounts,url) values (7,'technology',415,'ba.com') \n"
                + "insert into news (key, category, linkcounts,url) values (8,'travelling',45,'google.com/teravel') \n"
                + "APPLY BATCH;";
        Statement st = con.createStatement();
        st.executeUpdate(data);
    }


    public static void deleteData() throws SQLException {
        String data = "BEGIN BATCH \n"
                + "delete from  news where key=5 \n"
                + "delete  category from  news where key=2 \n"
                + "APPLY BATCH;";
        Statement st = con.createStatement();
        st.executeUpdate(data);
    }


    public static void updateData() throws SQLException {
        String t = "update news set category='sports', linkcounts=1 where key=5";
        Statement st = con.createStatement();
        st.executeUpdate(t);
    }


    public static void listData() throws SQLException {
        String t = "SELECT * FROM news";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(t);
        while (rs.next()) {
            //System.out.println(rs.getInt("KEY"));
            for (int j = 1; j < rs.getMetaData().getColumnCount() + 1; j++) {
                System.out.println(rs.getMetaData().getColumnName(j) + " : "
                        + rs.getString(rs.getMetaData().getColumnName(j)));
            }
        }
    }


}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/126431
推荐阅读
相关标签
  

闽ICP备14008679号