暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

华为openGauss 示例-常用操作

示例1

此示例将演示如何基于openGauss提供的JDBC接口开发应用程序。

``` //DBtest.java //演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等。

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.CallableStatement;

public class DBTest {

//创建数据库连接。 public static Connection GetConnection(String username, String passwd) { String driver = "org.postgresql.Driver"; String sourceURL = "jdbc:postgresql://localhost:8000/postgres"; Connection conn = null; try { //加载数据库驱动。 Class.forName(driver).newInstance(); } 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;

};

//执行普通SQL语句,创建customer_t1表。 public static void CreateTable(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement();

  //执行普通SQL语句。
  int rc = stmt
      .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");
  stmt.close();
} catch (SQLException e) {
  if (stmt != null) {
    try {
      stmt.close();
    } catch (SQLException e1) {
      e1.printStackTrace();
    }
  }
  e.printStackTrace();
}

}

//执行预处理语句,批量插入数据。 public static void BatchInsertData(Connection conn) { PreparedStatement pst = null;

try {
  //生成预处理语句。
  pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
  for (int i = 0; i < 3; i++) {
    //添加参数。
    pst.setInt(1, i);
    pst.setString(2, "data " + i);
    pst.addBatch();
  }
  //执行批处理。
  pst.executeBatch();
  pst.close();
} catch (SQLException e) {
  if (pst != null) {
    try {
      pst.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
  }
  e.printStackTrace();
}

}

//执行预编译语句,更新数据。 public static void ExecPreparedSQL(Connection conn) { PreparedStatement pstmt = null; try { pstmt = conn .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1"); pstmt.setString(1, "new Data"); int rowcount = pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } }

//执行存储过程。 public static void ExecCallableSQL(Connection conn) { CallableStatement cstmt = null; try {

  cstmt=conn.prepareCall("{? = CALL TESTPROC(?,?,?)}");
  cstmt.setInt(2, 50); 
  cstmt.setInt(1, 20);
  cstmt.setInt(3, 90);
  cstmt.registerOutParameter(4, Types.INTEGER);  //注册out类型的参数,类型为整型。
  cstmt.execute();
  int out = cstmt.getInt(4);  //获取out参数
  System.out.println("The CallableStatment TESTPROC returns:"+out);
  cstmt.close();
} catch (SQLException e) {
  if (cstmt != null) {
    try {
      cstmt.close();
    } catch (SQLException e1) {
      e1.printStackTrace();
    }
  }
  e.printStackTrace();
}

}

/* * 主程序,逐步调用各静态方法。 * @param args / public static void main(String[] args) { //创建数据库连接。 Connection conn = GetConnection("tester", "Password1234");

//创建表。
CreateTable(conn);
//批插数据。
BatchInsertData(conn);
//执行预编译语句,更新数据。
ExecPreparedSQL(conn);
//执行存储过程。
ExecCallableSQL(conn);
//关闭数据库连接。
try {
  conn.close();
} catch (SQLException e) {
  e.printStackTrace();
}

}

} ```

示例2 客户端内存占用过多解决

此示例主要使用setFetchSize来调整客户端内存使用,它的原理是通过数据库游标来分批获取服务器端数据,但它会加大网络交互,可能会损失部分性能。

由于游标事务内有效,故需要先关闭自动提交,最后需要执行手动提交。

``` // 关闭掉自动提交 conn.setAutoCommit(false); Statement st = conn.createStatement();

// 打开游标,每次获取50行数据 st.setFetchSize(50); ResultSet rs = st.executeQuery("SELECT * FROM mytable"); conn.commit(); while (rs.next()) { System.out.print("a row was returned."); } rs.close();

// 关闭服务器游标。 st.setFetchSize(0); rs = st.executeQuery("SELECT * FROM mytable"); conn.commit(); while (rs.next()) { System.out.print("many rows were returned."); } rs.close();

// Close the statement. st.close(); conn.close(); ```

执行完毕后可使用如下命令恢复自动提交:

conn.setAutoCommit(true);

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

文集目录
暂无数据