示例:常用操作

示例1

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

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

4. import java.sql.Connection;
5. import java.sql.DriverManager;
6. import java.sql.PreparedStatement;
7. import java.sql.SQLException;
8. import java.sql.Statement;
9. import java.sql.CallableStatement;
10. import java.sql.Types;

12. public class DBTest {

14. //创建数据库连接。
15. public static Connection GetConnection(String username, String passwd) {
16. String driver = "org.postgresql.Driver";
17. String sourceURL = "jdbc:postgresql://localhost:8000/postgres";
18. Connection conn = null;
19. try {
20. //加载数据库驱动。
21. Class.forName(driver).newInstance();
22. } catch (Exception e) {
23. e.printStackTrace();
24. return null;
25. }

27. try {
28. //创建数据库连接。
29. conn = DriverManager.getConnection(sourceURL, username, passwd);
30. System.out.println("Connection succeed!");
31. } catch (Exception e) {
32. e.printStackTrace();
33. return null;
34. }

36. return conn;
37. };

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

45. //执行普通SQL语句。
46. int rc = stmt
47. .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");

49. stmt.close();
50. } catch (SQLException e) {
51. if (stmt != null) {
52. try {
53. stmt.close();
54. } catch (SQLException e1) {
55. e1.printStackTrace();
56. }
57. }
58. e.printStackTrace();
59. }
60. }

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

66. try {
67. //生成预处理语句。
68. pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
69. for (int i = 0; i < 3; i++) {
70. //添加参数。
71. pst.setInt(1, i);
72. pst.setString(2, "data " + i);
73. pst.addBatch();
74. }
75. //执行批处理。
76. pst.executeBatch();
77. pst.close();
78. } catch (SQLException e) {
79. if (pst != null) {
80. try {
81. pst.close();
82. } catch (SQLException e1) {
83. e1.printStackTrace();
84. }
85. }
86. e.printStackTrace();
87. }
88. }

90. //执行预编译语句,更新数据。
91. public static void ExecPreparedSQL(Connection conn) {
92. PreparedStatement pstmt = null;
93. try {
94. pstmt = conn
95. .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");
96. pstmt.setString(1, "new Data");
97. int rowcount = pstmt.executeUpdate();
98. pstmt.close();
99. } catch (SQLException e) {
100. if (pstmt != null) {
101. try {
102. pstmt.close();
103. } catch (SQLException e1) {
104. e1.printStackTrace();
105. }
106. }
107. e.printStackTrace();
108. }
109. }

112. //执行存储过程。
113. public static void ExecCallableSQL(Connection conn) {
114. CallableStatement cstmt = null;
115. try {
116. // 存储过程TESTPROC需提前创建。
117. cstmt=conn.prepareCall("{? = CALL TESTPROC(?,?,?)}");
118. cstmt.setInt(2, 50);
119. cstmt.setInt(1, 20);
120. cstmt.setInt(3, 90);
121. cstmt.registerOutParameter(4, Types.INTEGER);  //注册out类型的参数,类型为整型。
122. cstmt.execute();
123. int out = cstmt.getInt(4);  //获取out参数
124. System.out.println("The CallableStatment TESTPROC returns:"+out);
125. cstmt.close();
126. } catch (SQLException e) {
127. if (cstmt != null) {
128. try {
129. cstmt.close();
130. } catch (SQLException e1) {
131. e1.printStackTrace();
132. }
133. }
134. e.printStackTrace();
135. }
136. }

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

147. //创建表。
148. CreateTable(conn);

150. //批插数据。
151. BatchInsertData(conn);

153. //执行预编译语句,更新数据。
154. ExecPreparedSQL(conn);

156. //执行存储过程。
157. ExecCallableSQL(conn);

159. //关闭数据库连接。
160. try {
161. conn.close();
162. } catch (SQLException e) {
163. e.printStackTrace();
164. }

166. }

168. }

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

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

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

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

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

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

25. // Close the statement.
26. st.close();
27. conn.close();

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

1. conn.setAutoCommit(true);

示例3 常用数据类型使用示例

1. //bit类型使用示例,注意此处bit类型取值范围[0,1]
2. Statement st = conn.createStatement();
3. String sqlstr = "create or replace function fun_1()\n" +
4. "returns bit AS $$\n" +
5. "select col_bit from t_bit limit 1;\n" +
6. "$$\n" +
7. "LANGUAGE SQL;";
8. st.execute(sqlstr);
9. CallableStatement c = conn.prepareCall("{ ? = call fun_1() }");
10. //注册输出类型,位串类型
11. c.registerOutParameter(1, Types.BIT);
12. c.execute();
13. //使用Boolean类型获取结果
14. System.out.println(c.getBoolean(1));

16. // money类型使用示例
17. // 表结构中包含money类型列的使用示例。
18. st.execute("create table t_money(col1 money)");
19. PreparedStatement pstm = conn.prepareStatement("insert into t_money values(?)");
20. // 使用PGobject赋值,取值范围[-92233720368547758.08,92233720368547758.07]
21. PGobject minMoney = new PGobject();
22. minMoney.setType("money");
23. minMoney.setValue("-92233720368547758.08");
24. pstm.setObject(1, minMoney);
25. pstm.execute();
26. // 使用PGMoney赋值,取值范围[-9999999.99,9999999.99]
27. pstm.setObject(1,new PGmoney(9999999.99));
28. pstm.execute();

30. // 函数返回值为money的使用示例。
31. st.execute("create or replace function func_money() " +
32. "return money " +
33. "as declare " +
34. "var1 money; " +
35. "begin " +
36. " select col1 into var1 from t_money limit 1; " +
37. " return var1; " +
38. "end;");
39. CallableStatement cs = conn.prepareCall("{? = call func_money()}");
40. cs.registerOutParameter(1,Types.DOUBLE);
41. cs.execute();
42. cs.getObject(1);

示例4 获取驱动版本示例

1. Driver.getGSVersion();