示例:常用操作

示例1

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

  1. //DBtest.java
  2. //演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等。
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.sql.CallableStatement;
  9. public class DBTest {
  10. //创建数据库连接。
  11. public static Connection GetConnection(String username, String passwd) {
  12. String driver = "org.postgresql.Driver";
  13. String sourceURL = "jdbc:postgresql://localhost:8000/postgres";
  14. Connection conn = null;
  15. try {
  16. //加载数据库驱动。
  17. Class.forName(driver).newInstance();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. return null;
  21. }
  22. try {
  23. //创建数据库连接。
  24. conn = DriverManager.getConnection(sourceURL, username, passwd);
  25. System.out.println("Connection succeed!");
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. return null;
  29. }
  30. return conn;
  31. };
  32. //执行普通SQL语句,创建customer_t1表。
  33. public static void CreateTable(Connection conn) {
  34. Statement stmt = null;
  35. try {
  36. stmt = conn.createStatement();
  37. //执行普通SQL语句。
  38. int rc = stmt
  39. .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");
  40. stmt.close();
  41. } catch (SQLException e) {
  42. if (stmt != null) {
  43. try {
  44. stmt.close();
  45. } catch (SQLException e1) {
  46. e1.printStackTrace();
  47. }
  48. }
  49. e.printStackTrace();
  50. }
  51. }
  52. //执行预处理语句,批量插入数据。
  53. public static void BatchInsertData(Connection conn) {
  54. PreparedStatement pst = null;
  55. try {
  56. //生成预处理语句。
  57. pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
  58. for (int i = 0; i < 3; i++) {
  59. //添加参数。
  60. pst.setInt(1, i);
  61. pst.setString(2, "data " + i);
  62. pst.addBatch();
  63. }
  64. //执行批处理。
  65. pst.executeBatch();
  66. pst.close();
  67. } catch (SQLException e) {
  68. if (pst != null) {
  69. try {
  70. pst.close();
  71. } catch (SQLException e1) {
  72. e1.printStackTrace();
  73. }
  74. }
  75. e.printStackTrace();
  76. }
  77. }
  78. //执行预编译语句,更新数据。
  79. public static void ExecPreparedSQL(Connection conn) {
  80. PreparedStatement pstmt = null;
  81. try {
  82. pstmt = conn
  83. .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");
  84. pstmt.setString(1, "new Data");
  85. int rowcount = pstmt.executeUpdate();
  86. pstmt.close();
  87. } catch (SQLException e) {
  88. if (pstmt != null) {
  89. try {
  90. pstmt.close();
  91. } catch (SQLException e1) {
  92. e1.printStackTrace();
  93. }
  94. }
  95. e.printStackTrace();
  96. }
  97. }
  98. //执行存储过程。
  99. public static void ExecCallableSQL(Connection conn) {
  100. CallableStatement cstmt = null;
  101. try {
  102. cstmt=conn.prepareCall("{? = CALL TESTPROC(?,?,?)}");
  103. cstmt.setInt(2, 50);
  104. cstmt.setInt(1, 20);
  105. cstmt.setInt(3, 90);
  106. cstmt.registerOutParameter(4, Types.INTEGER); //注册out类型的参数,类型为整型。
  107. cstmt.execute();
  108. int out = cstmt.getInt(4); //获取out参数
  109. System.out.println("The CallableStatment TESTPROC returns:"+out);
  110. cstmt.close();
  111. } catch (SQLException e) {
  112. if (cstmt != null) {
  113. try {
  114. cstmt.close();
  115. } catch (SQLException e1) {
  116. e1.printStackTrace();
  117. }
  118. }
  119. e.printStackTrace();
  120. }
  121. }
  122. /**
  123. * 主程序,逐步调用各静态方法。
  124. * @param args
  125. */
  126. public static void main(String[] args) {
  127. //创建数据库连接。
  128. Connection conn = GetConnection("tester", "Password1234");
  129. //创建表。
  130. CreateTable(conn);
  131. //批插数据。
  132. BatchInsertData(conn);
  133. //执行预编译语句,更新数据。
  134. ExecPreparedSQL(conn);
  135. //执行存储过程。
  136. ExecCallableSQL(conn);
  137. //关闭数据库连接。
  138. try {
  139. conn.close();
  140. } catch (SQLException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. }

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

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

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

  1. // 关闭掉自动提交
  2. conn.setAutoCommit(false);
  3. Statement st = conn.createStatement();
  4. // 打开游标,每次获取50行数据
  5. st.setFetchSize(50);
  6. ResultSet rs = st.executeQuery("SELECT * FROM mytable");
  7. conn.commit();
  8. while (rs.next())
  9. {
  10. System.out.print("a row was returned.");
  11. }
  12. rs.close();
  13. // 关闭服务器游标。
  14. st.setFetchSize(0);
  15. rs = st.executeQuery("SELECT * FROM mytable");
  16. conn.commit();
  17. while (rs.next())
  18. {
  19. System.out.print("many rows were returned.");
  20. }
  21. rs.close();
  22. // Close the statement.
  23. st.close();
  24. conn.close();

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

  1. conn.setAutoCommit(true);