1、JAVA开发

1.1、创建数据表

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. public class createtable {
  5. public static void main( String args[] )
  6. {
  7. Connection c = null;
  8. Statement stmt = null;
  9. try {
  10. Class.forName("org.postgresql.Driver");
  11. c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?currentSchema=public&binaryTransfer=false","tbase", "tbase");
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. String sql = "create table tbase(id int,nickname text) distribute by shard(id) to group default_group" ;
  15. stmt.executeUpdate(sql);
  16. stmt.close();
  17. c.close();
  18. } catch ( Exception e ) {
  19. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  20. System.exit(0);
  21. }
  22. System.out.println("Table created successfully");
  23. }
  24. }

说明:

  • 这里连接的节点为任意CN主节点,后面所有操作,没特别说明,都是连接到CN主节点进行操作。

1.2、使用普通协议插入数据

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. public class insert {
  5. public static void main(String args[]) {
  6. Connection c = null;
  7. Statement stmt = null;
  8. try {
  9. Class.forName("org.postgresql.Driver");
  10. c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?currentSchema=public&binaryTransfer=false","tbase", "tbase");
  11. c.setAutoCommit(false);
  12. System.out.println("Opened database successfully");
  13. stmt = c.createStatement();
  14. String sql = "INSERT INTO tbase (id,nickname) "
  15. + "VALUES (1,'tbase');";
  16. stmt.executeUpdate(sql);
  17. sql = "INSERT INTO tbase (id,nickname) "
  18. + "VALUES (2, 'pgxz' ),(3,'pgxc');";
  19. stmt.executeUpdate(sql);
  20. stmt.close();
  21. c.commit();
  22. c.close();
  23. } catch (Exception e) {
  24. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  25. System.exit(0);
  26. }
  27. System.out.println("Records created successfully");
  28. }
  29. }

1.3、使用扩展协议插入数据

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.*;
  4. import java.util.Random;
  5. public class insert_prepared {
  6. public static void main(String args[]) {
  7. Connection c = null;
  8. PreparedStatement stmt;
  9. try {
  10. Class.forName("org.postgresql.Driver");
  11. c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?currentSchema=public&binaryTransfer=false","tbase", "tbase");
  12. c.setAutoCommit(false);
  13. System.out.println("Opened database successfully");
  14. //插入数据
  15. String sql = "INSERT INTO tbase (id,nickname) VALUES (?,?)";
  16. stmt = c.prepareStatement(sql);
  17. stmt.setInt(1, 9999);
  18. stmt.setString(2, "tbase_prepared");
  19. stmt.executeUpdate();
  20. //插入更新
  21. sql = "INSERT INTO tbase (id,nickname) VALUES (?,?) ON CONFLICT(id) DO UPDATE SET nickname=?";
  22. stmt = c.prepareStatement(sql);
  23. stmt.setInt(1, 9999);
  24. stmt.setString(2, "tbase_prepared");
  25. stmt.setString(3, "tbase_prepared_update");
  26. stmt.executeUpdate();
  27. stmt.close();
  28. c.commit();
  29. c.close();
  30. } catch (Exception e) {
  31. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  32. System.exit(0);
  33. }
  34. System.out.println("Records created successfully");
  35. }
  36. }

1.4、copy from 加载文件到表

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import org.postgresql.copy.CopyManager;
  5. import org.postgresql.core.BaseConnection;
  6. import java.io.*;
  7. public class copyfrom {
  8. public static void main( String args[] )
  9. {
  10. Connection c = null;
  11. Statement stmt = null;
  12. FileInputStream fs = null;
  13. try {
  14. Class.forName("org.postgresql.Driver");
  15. c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?currentSchema=public&binaryTransfer=false","tbase", "tbase");
  16. System.out.println("Opened database successfully");
  17. CopyManager cm = new CopyManager((BaseConnection) c);
  18. fs = new FileInputStream("/data/tbase/tbase.csv");
  19. String sql = "COPY tbase FROM STDIN DELIMITER AS ','";
  20. cm.copyIn(sql, fs);
  21. c.close();
  22. fs.close();
  23. } catch ( Exception e ) {
  24. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  25. System.exit(0);
  26. }
  27. System.out.println("Copy data successfully");
  28. }
  29. }

1.5、copy to 导出数据到文件

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import org.postgresql.copy.CopyManager;
  5. import org.postgresql.core.BaseConnection;
  6. import java.io.*;
  7. public class copyto {
  8. public static void main( String args[] )
  9. {
  10. Connection c = null;
  11. Statement stmt = null;
  12. FileOutputStream fs = null;
  13. try {
  14. Class.forName("org.postgresql.Driver");
  15. c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?currentSchema=public&binaryTransfer=false","tbase", "tbase");
  16. System.out.println("Opened database successfully");
  17. CopyManager cm = new CopyManager((BaseConnection) c);
  18. fs = new FileOutputStream("/data/tbase/tbase.csv");
  19. String sql = "COPY tbase TO STDOUT DELIMITER AS ','";
  20. cm.copyOut(sql, fs);
  21. c.close();
  22. fs.close();
  23. } catch ( Exception e ) {
  24. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
  25. System.exit(0);
  26. }
  27. System.out.println("Copy data successfully");
  28. }
  29. }

1.6、jdbc包下载地址

  1. https://jdbc.postgresql.org/download.html