每日随笔——连接hbase数据库
今天尝试连接hbase数据库,在之前完成了hbase、zookeeper和phoenix三个组件,现在尝试连接hbase数据库
连接phoenix
package database; import java.sql.*; import java.util.Properties; public class PhoenixClient { public static Connection getConnection() throws SQLException { // 标准的 JDBC 代码 // 1.添加链接 // "jdbc:phoenix:node1,node2,node3:2181"; String url = "jdbc:phoenix:node1,node2,node3:2181"; // 2. 创建配置 // 没有需要添加的必要配置 因为 Phoenix 没有账号密码 Properties properties = new Properties(); // 3. 获取连接 Connection connection = DriverManager.getConnection(url, properties); // // 5.编译 SQL 语句 // PreparedStatement preparedStatement = // connection.prepareStatement("select * from JISHU"); // // 6.执行语句 // ResultSet resultSet = preparedStatement.executeQuery(); // // 7.输出结果 // while (resultSet.next()){ // System.out.println(resultSet.getString(1) + ":" + // resultSet.getString(2) + ":" + resultSet.getString(3)+ ":" + resultSet.getString(4)); // } // // 8.关闭资源 // connection.close(); // 由于 Phoenix 框架内部需要获取一个 HBase 连接,所以会延迟关闭 // 不影响后续的代码执行 return connection; } public static void main(String[] args) throws SQLException { getConnection(); } }
bean类
package bean; import database.PhoenixClient; import java.sql.*; import java.util.List; import java.util.Map; import java.util.Properties; public class Guanli { private String usertype; private String usernumber; private String userpassword; @Override public String toString() { return "Guanli{" + "usertype='" + usertype + '\'' + ", usernumber='" + usernumber + '\'' + ", userpassword='" + userpassword + '\'' + '}'; } public String getUsertype() { return usertype; } public void setUsertype(String usertype) { this.usertype = usertype; } public String getUsernumber() { return usernumber; } public void setUsernumber(String usernumber) { this.usernumber = usernumber; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } public Guanli(String usertype, String usernumber, String userpassword) { this.usertype = usertype; this.usernumber = usernumber; this.userpassword = userpassword; } public static Connection getConnection() throws SQLException { // 标准的 JDBC 代码 // 1.添加链接 // "jdbc:phoenix:node1,node2,node3:2181"; String url = "jdbc:phoenix:node1,node2,node3:2181"; // 2. 创建配置 // 没有需要添加的必要配置 因为 Phoenix 没有账号密码 Properties properties = new Properties(); // 3. 获取连接 Connection connection = DriverManager.getConnection(url, properties); return connection; } public Guanli() { } public void insert(String str1,String str2,String str3,String str4,String str5,String str6,String str7,String str8,String str9,String str10,String str11,String str12,String str13) throws SQLException { Connection conn = getConnection(); try { Long a = System.currentTimeMillis(); Statement statement = conn.createStatement(); for (int i = 1; i <= 10000; i++) { String sql_insert = "upsert into teacher(jigou,guanlibumen,tongxundizhi,suozaidiyu,wangzhi,youxiang,daibiao,bianma,lianxiren,dianhua,shouji,chuanzhen,shuxing)values(" + str1 + "," + str2 + "," + str3 + "," + str4 + "," + str5 + "," + str6 + "," + str7 + "," + str8 + "," + str9 + "," + str10 + "," + str11 + "," + str12 + "," + str13 + ")"; statement.addBatch(sql_insert); } statement.executeBatch(); conn.commit(); Long b = System.currentTimeMillis(); System.out.println("运行时间:" + (b - a)); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { } } } public void delete(String str) throws SQLException {//删除 Connection connection = getConnection(); PreparedStatement preparedStatement=null; try { String sql = "delete from JI where jigou = ?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,str); preparedStatement.executeUpdate(); connection.commit(); System.out.println("删除成功"); } catch (SQLException e) { e.printStackTrace(); }finally{ } } public void add(String str1,String str2,String str3,String str4,String str5,String str6,String str7,String str8,String str9,String str10,String str11,String str12,String str13) throws SQLException { Connection connection = getConnection(); PreparedStatement preparedStatement=null; try { String sql = "upsert into JI (jigou,guanlibumen,tongxundizhi,suozaidiyu,wangzhi,youxiang,daibiao,bianma,lianxiren,dianhua,shouji,chuanzhen,shuxing) values (?,?,?,?,?,?,?,?,?,?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,str1); preparedStatement.setString(2,str2); preparedStatement.setString(3,str3); preparedStatement.setString(4,str4); preparedStatement.setString(5,str5); preparedStatement.setString(6,str6); preparedStatement.setString(7,str7); preparedStatement.setString(8,str8); preparedStatement.setString(9,str9); preparedStatement.setString(10,str10); preparedStatement.setString(11,str11); preparedStatement.setString(12,str12); preparedStatement.setString(13,str13); preparedStatement.executeUpdate(); connection.commit(); System.out.println("添加成功"); } catch (SQLException e) { e.printStackTrace(); }finally{ } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!