love_Java123
love_Java123
采纳率42.9%
2017-06-15 08:22 阅读 1.8k

用jdbc连接hive时resultset里面查不到数据,row是null

代码如下:

public class ExtractJob {

public static void main(String[] args) {

    String driverName = "org.apache.hive.jdbc.HiveDriver"; 
    String url = "jdbc:hive2://***.***.***.***:10000/default";
    Connection conn = null;
    Statement state = null; 
    ResultSet rs = null;




    try {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url,"hive","hive");
        state = conn.createStatement();





        state.execute("use test");
        rs = state.executeQuery("select * from test1");






        int columnCount = rs.getMetaData().getColumnCount();

        String str = "";
        while(rs.next()){
            for(int i = 0;i<columnCount;i++){
                str+=rs.getString(i);
            }
            System.out.println(str);
        }








    } catch (SQLException e) {
        e.printStackTrace();
    }catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally{
        try {
            rs.close();
            state.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            rs = null;
            state = null;
            conn = null;
        }
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • strutce 丵鹰 2017-06-15 08:42

    你这里 String url = "jdbc:hive2://***.***.***.***:10000/default";连接的数据库是default;但在这里state.execute("use test");你把数据库切换到了test

    点赞 评论 复制链接分享

相关推荐