openGauss3.0.0之Java API基本操作
-
- 一、环境说明
- 二、创建新的数据库用户
- 三、添加客户端IP至pg_hba.conf
- 四、编写程序测试
一、环境说明
- 虚拟机VMWareWorkstation15+
- JDK1.8+
- Maven
- IDEA
- 使用
omm
用户登录虚拟机,并登录openGaussgsql -d postgres -p 15400
- 创建数据库新用户如suben
create user suben grant all privileges to suben
- 创建数据库如suben_db01
create database suben_db01 owner suben;
- 查看本机IP地址,cmd执行ipconfig
文章图片
- 使用omm用户编辑pg_hba.conf,将客户端(本机)IP地址添加至其中
vim /opt/huawei/install/data/dn/pg_hba.conf
文章图片
- 【openGauss|【openGauss3.0.0之Java API基本操作】】重启openGauss服务
gs_om -t stop gs_om -t start
- 创建maven
- 添加openGauss数据库驱动依赖
org.opengauss opengauss-jdbc 3.0.0
- 编写程序,实现数据库表的简单操作
import java.sql.*; import java.util.Properties; public class OpenGaussDemo { public static void main(String[] args) throws Exception { Connection conn = getConnect("suben_db01","suben","Gauss_123"); // 创建表 //createTable(conn,"create table test01(id int,name varchar(10))"); // 插入数据 // insert(conn,"insert into test01 values(1,'苏江明')"); // 查询数据 // select(conn,"select * from test01 where id=1","id","name"); selectDynamicParameter(conn,"1","id","name"); }public static Connection getConnect(String db,String user,String pwd) { String driver = "org.opengauss.Driver"; // 需要创建新的数据库用户,使用新的数据库用户连接 String sourceURL = "jdbc:opengauss://192.168.76.150:15400/"+db+"?"; Properties properties = new Properties(); properties.setProperty("user",user); properties.setProperty("password",pwd); Connection conn = null; try { Class.forName(driver); } catch (Exception var9) { var9.printStackTrace(); return null; } try { conn = DriverManager.getConnection(sourceURL,properties); System.out.println("连接成功! "); return conn; } catch (Exception var8) { var8.printStackTrace(); return null; } }public static boolean createTable(Connection conn,String sql) throws SQLException { Statement statement = conn.createStatement(); 容易产生SQL注入 return statement.execute(sql); }public static boolean insert(Connection conn,String sql) throws SQLException { Statement statement = conn.createStatement(); return statement.execute(sql); }public static void select(Connection conn,String sql,String... cols) throws SQLException { PreparedStatement preparedStatement=conn.prepareStatement(sql); ResultSet resultSet=preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getObject(cols[0])+" "+ resultSet.getObject(cols[1])); } if (preparedStatement != null){ preparedStatement.close(); } if (conn != null){ conn.close(); } }public static void selectDynamicParameter(Connection conn,String... cols) throws SQLException { PreparedStatement preparedStatement=conn.prepareStatement("select * from test01 where id=?"); preparedStatement.setObject(1,Integer.valueOf(cols[0])); ResultSet resultSet=preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getObject(cols[1])+" "+ resultSet.getObject(cols[2])); } if (preparedStatement != null){ preparedStatement.close(); } if (conn != null){ conn.close(); } } }
到此,已经简单完成了使用Java api实现连接openGauss数据库、创建表、插入数据、查询数据等操作。您也尝试下吧!如对您有用,欢迎给博主点点赞~~~~~