我的密码是
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.datastax.driver.core.BatchStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.LocalDate;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
public class DataPortCluster {
private static Cluster cluster;
private static Session session;
public static Cluster connect(String node) {
return Cluster.builder().addContactPoint(node).build();
}
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stm = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://localhost:3306/ksfedata";
String uName = "root";
String uPass= "root";
conn= DriverManager.getConnection(host, uName, uPass);
stm = conn.createStatement();
String sql = "SELECT c.id, c.branch, c.firstName, c.lastName, c.dob, c.uniqueId, c.mobilePhone, c.isKSFEEmployee "
+ ",CONCAT_WS('',offaddress.line1,offaddress.line2, offaddress.pinCode) AS Official,"
+ "CONCAT_WS('',peraddress.line1,peraddress.line2, peraddress.pinCode) AS Permanent,"
+ "CONCAT_WS('',resaddress.line1,resaddress.line2, resaddress.pinCode) AS Residential,"
+ "CONCAT_WS('',tempaddress.line1,tempaddress.line2, tempaddress.pinCode) AS Temporary,"
+ "CONCAT_WS('',commaddress.line1,commaddress.line2, commaddress.pinCode) AS Communication"
+ " FROM customerdup c "
+ "LEFT JOIN customeraddressdup as offaddress ON (offaddress.customerId =c.id AND offaddress.customerBranch = c.branch AND offaddress.addresstype=0) "
+ "LEFT JOIN customeraddressdup as peraddress ON (peraddress.customerId =c.id AND peraddress.customerBranch = c.branch AND peraddress.addresstype=1) "
+ "LEFT JOIN customeraddressdup as resaddress ON (resaddress.customerId =c.id AND resaddress.customerBranch = c.branch AND resaddress.addresstype=2) "
+ "LEFT JOIN customeraddressdup as tempaddress ON (tempaddress.customerId =c.id AND tempaddress.customerBranch = c.branch AND tempaddress.addresstype=3) "
+ "LEFT JOIN customeraddressdup as commaddress ON (commaddress.customerId =c.id AND commaddress.customerBranch = c.branch AND commaddress.addresstype=4)";
// String sql = "Select ID,FIRSTNAME,DOB,MOBILEPHONE,EMAIL From customerdup LIMIT 3";
ResultSet rst;
rst = stm.executeQuery(sql);
String serverIp = "localhost";
String keyspace = "events";
cluster = Cluster.builder().addContactPoint(serverIp).build();
session = cluster.connect(keyspace);
PreparedStatement statement = session.prepare("insert into customer (branch, uniqueid, communication,customerid,dob,firstname,isksfeemp,lastname,mobile,official,permanent,residential,temporary) "
+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?) ;");
//BoundStatement boundStatement = new BoundStatement(statement);
BatchStatement batchStatement = new BatchStatement();
long startTime = System.nanoTime();
while (rst.next()) {
int id = rst.getInt("id");
String branch = rst.getString("branch");
String firstname = rst.getString("firstname");
String lastname = rst.getString("lastname");
Date datecheck = rst.getDate("dob");
LocalDate dob = LocalDate.fromMillisSinceEpoch(datecheck.getTime());
String uniqueId = rst.getString("uniqueId");
String mobile = rst.getString("mobilePhone");
int isKSFEEmployee = rst.getInt("isKSFEEmployee");
String Official = rst.getString("Official");
String Permanent = rst.getString("Permanent");
String Residential = rst.getString("Residential");
String Temporary = rst.getString("Temporary");
String Communication = rst.getString("Communication");
batchStatement.add(statement.bind(branch,uniqueId,Communication,id,dob,firstname,isKSFEEmployee,lastname,mobile,Official,Permanent,Residential,Temporary));
//session.execute(boundStatement.bind(id,name,Dob,mobNo,email));
}
session.execute(batchStatement);
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime + "++++++++++++++555555555555555555555555555555555555555555555");
String cqlStatement = "SELECT * FROM customer";
for (Row row : session.execute(cqlStatement)) {
System.out.println(row.toString());
}
}catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
if (conn != null && stm != null) {
try {
conn.close();
stm.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
session.close();
cluster.close();
}
}
}
当我试着调试我的代码时,我遇到了一个错误
Exception in thread "main" java.lang.NullPointerException
at DataPortCluster.main(DataPortCluster.java:121)
在那条线上
cluster = Cluster.builder().addContactPoint(serverIp).build();
不知道是什么问题。如果有人能帮忙的话,那将是非常值得赞赏的。在我看来,问题是发生在连接Cassandra。为什么会在上面的行中抛出错误。我给出的serverip是localhost。
提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!