ITPub博客

首页 > 数据库 > 数据库开发技术 > openGauss学习笔记-162 openGauss 数据库运维-备份与恢复-导入数据-通过INSERT语句直接写入数据

openGauss学习笔记-162 openGauss 数据库运维-备份与恢复-导入数据-通过INSERT语句直接写入数据

原创 数据库开发技术 作者:shuchaoyang 时间:2023-09-12 07:00:00 0 删除 编辑

openGauss学习笔记-162 openGauss 数据库运维-备份与恢复-导入数据-通过INSERT语句直接写入数据162.1 使用openGauss数据库提供的客户端工具向openGauss数据库写入数据162.2 通过JDBC/ODBC驱动连接数据库执行INSERT语句向openGauss数据库写入数据162.2.1 函数原型162.2.2 参数162.2.3 示例

openGauss学习笔记-162 openGauss 数据库运维-备份与恢复-导入数据-通过INSERT语句直接写入数据

用户可以通过以下方式执行INSERT语句直接向openGauss数据库写入数据:

162.1 使用openGauss数据库提供的客户端工具向openGauss数据库写入数据

请参见openGauss学习笔记-68 openGauss 数据库管理-创建和管理普通表-向表中插入数据。

162.2 通过JDBC/ODBC驱动连接数据库执行INSERT语句向openGauss数据库写入数据

在创建数据库连接之后,才能使用它来执行SQL语句操作数据。

162.2.1 函数原型

JDBC提供了三个方法,用于创建数据库连接。

  • DriverManager.getConnection(String url);

  • DriverManager.getConnection(String url, Properties info);

  • DriverManager.getConnection(String url, String user, String password);

162.2.2 参数

表 1 数据库连接参数

162.2.3 示例

//以下代码将获取数据库连接操作封装为一个接口,可通过给定用户名和密码来连接数据库。
public static Connection getConnect(String username, String passwd)
  {
       //驱动类。
       String driver = "org.opengauss.Driver";
       //数据库连接描述符。
       String sourceURL = "jdbc:opengauss://10.10.0.13:8000/postgres";
       Connection conn = null;
       
       try
      {
           //加载驱动。
           Class.forName(driver);
      }
       catch( Exception e )
      {
           e.printStackTrace();
           return null;
      }
       
       try
      {
            //创建连接。
           conn = DriverManager.getConnection(sourceURL, username, passwd);
           System.out.println("Connection succeed!");
      }
       catch(Exception e)
      {
           e.printStackTrace();
           return null;
      }
       
       return conn;
  };
// 以下代码将使用Properties对象作为参数建立连接
public static Connection getConnectUseProp(String username, String passwd)
  {
       //驱动类。
       String driver = "org.opengauss.Driver";
       //数据库连接描述符。
       String sourceURL = "jdbc:opengauss://10.10.0.13:8000/postgres?";
       Connection conn = null;
       Properties info = new Properties();
       
       try
      {
           //加载驱动。
           Class.forName(driver);
      }
       catch( Exception e )
      {
           e.printStackTrace();
           return null;
      }
       
       try
      {
            info.setProperty("user", username);
            info.setProperty("password", passwd);
            //创建连接。
            conn = DriverManager.getConnection(sourceURL, info);
            System.out.println("Connection succeed!");
      }
       catch(Exception e)
      {
           e.printStackTrace();
           return null;
      }
       
       return conn;
  };

openGauss数据库支持完整的数据库事务级别的增删改操作。INSERT是最简单的一种数据写入方式,这种方式适合数据写入量不大,并发度不高的场景。


👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!


原文作者:超哥的IT私房菜;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论
专注于IT系统集成、运维、架构二十年的IT职场老兵,拥有CCNA、OCA、OCP、PGCA、PGCE认证证书,公众号:超哥的IT私房菜,知识星球:超哥的IT私房菜,目前是多家IT厂商的咨询顾问。

注册时间:2021-08-27

  • 博文量
    552
  • 访问量
    303730