用Maven进行项目管理
package com.ny.hive.hivedemo;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Before;
import org.junit.Test;
public class TestCRUD {
private Connection coon;
@Before
public void iniCoon() throws Exception{
coon = DriverManager.getConnection(//
"jdbc:hive2://node02:10000/default",//
"root",
"123456"//
);
}
/*
* create table users
*/
@Test
public void create() throws Exception{
PreparedStatement ppst = coon.prepareStatement("create table default.users(id int,name string,age int)");
ppst.execute();
ppst.close();
coon.close();
System.out.println("创建成功");
}
/*
* 插入多个
*/
@Test
public void batchInsert() throws Exception{
PreparedStatement ppst = coon.prepareStatement("insert into table default.users(id,name,age) values(?,?,?)");
ppst.setInt(1, 1);
ppst.setString(2, "tom");
ppst.setInt(3, 12);
ppst.executeUpdate();
ppst.setInt(1, 1);
ppst.setString(2, "tom2");
ppst.setInt(3, 13);
ppst.executeUpdate();
ppst.setInt(1, 1);
ppst.setString(2, "tom3");
ppst.setInt(3, 14);
ppst.executeUpdate();
ppst.close();
coon.close();
}
//统计记录
@Test
public void count() throws Exception{
PreparedStatement ppst = coon.prepareStatement("select count(*) from default.users");
ResultSet rs =ppst.executeQuery();
rs.next();
System.out.println(rs.getInt(1));
ppst.close();
coon.close();
}
/*
*删除表
*/
@Test
public void deletetable() throws Exception{
PreparedStatement ppst = coon.prepareStatement("drop table default.users");
ppst.execute();
ppst.close();
coon.close();
System.out.println("删除成功");
}
}