初始化与关闭流

    1. private Connection connection;
    2. private PreparedStatement preparedStatement;
    3. @Before
    4. public void init() throws ClassNotFoundException, SQLException {
    5. // 加载驱动
    6. //Driver,import org.apache.phoenix.queryserver.client.Driver;
    7. Class.forName("org.apache.phoenix.queryserver.client.Driver");
    8. // 获取连接
    9. String url = ThinClientUtil.getConnectionUrl("hadoop102", 8765);
    10. connection = DriverManager.getConnection(url);
    11. connection.setAutoCommit(true);
    12. }
    13. public void close() throws SQLException {
    14. // 执行
    15. // 关闭
    16. if (preparedStatement != null) {
    17. preparedStatement.close();
    18. }
    19. if (connection!=null) {
    20. connection.close();
    21. }
    22. }

    DML增删改查

    1. @Test
    2. public void upsert() throws SQLException {
    3. // preparestatement有预编译功能,后面需要用?作为参数,防止sql注入
    4. preparedStatement = connection.prepareStatement("upsert into student2 values(?,?,?)");
    5. // setString设置上面的?参数
    6. preparedStatement.setString(1,"1002");
    7. preparedStatement.setString(2,"ldc");
    8. preparedStatement.setString(3,"33");
    9. preparedStatement.executeUpdate();
    10. }
    11. @Test
    12. public void upsertBatch() throws SQLException {
    13. PreparedStatement preparedStatement = connection.prepareStatement("upsert into student2 values(?,?,?)");
    14. for (int i=0;i<1200;i++) {
    15. preparedStatement.setString(1,"1003"+i);
    16. preparedStatement.setString(2,"ldc"+i);
    17. preparedStatement.setString(3,"12"+1);
    18. preparedStatement.addBatch();
    19. if (i%500==0){
    20. preparedStatement.executeBatch();
    21. preparedStatement.clearBatch();
    22. }
    23. }
    24. // 提交最后一个不满500条数据的批次
    25. preparedStatement.executeBatch();
    26. }
    27. @Test
    28. public void query() throws SQLException {
    29. PreparedStatement preparedStatement = connection.prepareStatement("select * from student2 where id > ?");
    30. preparedStatement.setString(1,"1003");
    31. ResultSet resultSet = preparedStatement.executeQuery();
    32. while (resultSet.next()) {
    33. String id = resultSet.getString("id");
    34. String name = resultSet.getString("name");
    35. String age = resultSet.getString("age");
    36. System.out.println("id="+id+"name="+name+"age="+age);
    37. }
    38. }
    39. @Test
    40. public void delete() throws SQLException {
    41. PreparedStatement preparedStatement = connection.prepareStatement("delete from student2 where id > ?");
    42. preparedStatement.setString(1,"100310");
    43. preparedStatement.executeUpdate();
    44. }

    DDL

    1. @Test
    2. public void createTable() throws ClassNotFoundException, SQLException {
    3. // 获取statement对象
    4. preparedStatement = connection.prepareStatement("create table student2(" +
    5. "id varchar primary key," +
    6. "name varchar," +
    7. "age varchar)COLUMN_ENCODED_BYTES=0");
    8. // 参数赋值
    9. //执行
    10. preparedStatement.execute();
    11. }
    12. @Test
    13. public void dropTable() throws SQLException {
    14. PreparedStatement preparedStatement = connection.prepareStatement("drop table student2");
    15. preparedStatement.execute();
    16. }