I'm trying to execute a Java file, but when I do I get an error NullPointerException in this part of the program.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HiveClient {
// JDBC driver required for Hive connections
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static Connection con;
private static Connection getConnection(String ip, String port, String user, String password) {
try {
// dynamically load the Hive JDBC driver
Class.forName(driverName);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
return null;
} // try catch
try {
// return a connection based on the Hive JDBC driver
return DriverManager.getConnection("jdbc:hive://" + ip + ":" + port + "/default?user=" + user + "&password=" + password);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
} // try catch
} // getConnection
public static void main(String[] args) {
try {
// from here on, everything is SQL!
con = getConnection("130.206.80.46", "10000", "myuser", "mypasswd");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select column1,column2,otherColumns " + "from mytable where column1='whatever' and columns2 like '%whatever%'");
// iterate on the result
while (res.next()) {
String column1 = res.getString(1);
Integer column2 = res.getInt(2);
// whatever you want to do with this row, here
} // while
// close everything
res.close();
stmt.close();
con.close();
} catch (SQLException ex) {
System.exit(0);
} // try catch
}
// doQuery
} // HiveClient
This is the message:
Exception in thread "main" java.lang.NullPointerException
at HiveClient.main(HiveClient.java:34)
Java Result: 1
The line the exception is at:
Statement stmt = con.createStatement();
I've checked the java.sql.Connection package and createStatement() is allowed in the Method Summary.