示例:通过本地文件导入导出数据

在使用JAVA语言基于openGauss进行二次开发时,可以使用CopyManager接口,通过流方式,将数据库中的数据导出到本地文件或者将本地文件导入数据库中,文件格式支持CSV、TEXT等格式。

样例程序如下,执行时需要加载openGauss jdbc驱动。

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.io.IOException;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.sql.SQLException;
  7. import org.postgresql.copy.CopyManager;
  8. import org.postgresql.core.BaseConnection;
  9. public class Copy{
  10. public static void main(String[] args)
  11. {
  12. String urls = new String("jdbc:postgresql://10.180.155.74:8000/postgres"); //数据库URL
  13. String username = new String("jack"); //用户名
  14. String password = new String("Gauss@123"); //密码
  15. String tablename = new String("migration_table"); //定义表信息
  16. String tablename1 = new String("migration_table_1"); //定义表信息
  17. String driver = "org.postgresql.Driver";
  18. Connection conn = null;
  19. try {
  20. Class.forName(driver);
  21. conn = DriverManager.getConnection(urls, username, password);
  22. } catch (ClassNotFoundException e) {
  23. e.printStackTrace(System.out);
  24. } catch (SQLException e) {
  25. e.printStackTrace(System.out);
  26. }
  27. // 将SELECT * FROM migration_table查询结果导出到本地文件d:/data.txt
  28. try {
  29. copyToFile(conn, "d:/data.txt", "(SELECT * FROM migration_table)");
  30. } catch (SQLException e) {
  31. k
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. //将d:/data.txt中的数据导入到migration_table_1中。
  37. try {
  38. copyFromFile(conn, "d:/data.txt", tablename1);
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. // 将migration_table_1中的数据导出到本地文件d:/data1.txt
  45. try {
  46. copyToFile(conn, "d:/data1.txt", tablename1);
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. // 使用copyIn把数据从文件中导入数据库,
  54. public static void copyFromFile(Connection connection, String filePath, String tableName)
  55. throws SQLException, IOException {
  56. FileInputStream fileInputStream = null;
  57. try {
  58. CopyManager copyManager = new CopyManager((BaseConnection)connection);
  59. fileInputStream = new FileInputStream(filePath);
  60. copyManager.copyIn("COPY " + tableName + " FROM STDIN", fileInputStream);
  61. } finally {
  62. if (fileInputStream != null) {
  63. try {
  64. fileInputStream.close();
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }
  70. }
  71. // 使用copyOut把数据从数据库中导出到文件中
  72. public static void copyToFile(Connection connection, String filePath, String tableOrQuery)
  73. throws SQLException, IOException {
  74. FileOutputStream fileOutputStream = null;
  75. try {
  76. CopyManager copyManager = new CopyManager((BaseConnection)connection);
  77. fileOutputStream = new FileOutputStream(filePath);
  78. copyManager.copyOut("COPY " + tableOrQuery + " TO STDOUT", fileOutputStream);
  79. } finally {
  80. if (fileOutputStream != null) {
  81. try {
  82. fileOutputStream.close();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. }
  89. }