pg建表和建索引

java 代码:

package com.geo.mine.util;

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

public class CreateTableAndOther {

    // PostgreSQL数据库的JDBC URL
     String jdbcUrl = "jdbc:postgresql://localhost:5433/db_mine1.0";
    // 数据库用户和密码
     String username = "postgres";
     String password = "123";
     Connection connection = null;
     Statement statement = null;

    public Connection connect(){
        try {
            // 建立连接
            connection = DriverManager.getConnection(jdbcUrl, username, password);
        }catch (Exception e) {
            System.out.println("database failed");
            e.printStackTrace();
        } finally {
            return connection;
        }
    }
    public boolean close(){
        boolean result = false;
        try {
            if(null != connection){
                connection.close();
                result = true;
            }
        }catch (Exception e) {
            System.out.println("database failed");
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    public void createTable(String tableName, String sqlText) {
        try
        {
            // 加载并注册JDBC驱动
            Class.forName("org.postgresql.Driver");
            // 创建Statement对象来执行SQL语句
            statement = connection.createStatement();

                // 创建表的SQL语句
                String sql = "CREATE TABLE IF NOT EXISTS tb_ausers (" +
                        "id SERIAL PRIMARY KEY, " +
                        "username VARCHAR(50) NOT NULL, " +
                        "email VARCHAR(50), " +
                        "created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP" +
                        ")";
                sql = "create table " + tableName + " " + sqlText;

                // 执行SQL语句
                statement.execute(sql);
                System.out.println("Table created successfully");

        } catch (ClassNotFoundException e) {
            System.out.println("JDBC driver not found");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Connection to database failed");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("database failed");
            e.printStackTrace();
        }  finally{

           if(null != statement){
              try{
                 statement.close();
              } catch (Exception e){
                 e.printStackTrace();
            }
           }
        }
    }
    public void createIndex(String tableName) {
        // PostgreSQL数据库的JDBC URL
        try
        {
            // 加载并注册JDBC驱动
            Class.forName("org.postgresql.Driver");

            // 创建Statement对象来执行SQL语句
            statement = connection.createStatement();

            // 创建表的SQL语句
            String sql = " CREATE INDEX " + tableName +"_shape_idx5 ON " + tableName  +
                    " USING gist (shape public.gist_geometry_ops_2d) ";
            // 执行SQL语句
            statement.execute(sql);

            System.out.println("Index created successfully");

        } catch (ClassNotFoundException e) {
            System.out.println("JDBC driver not found");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Connection to database failed");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("database failed");
            e.printStackTrace();
        }  finally{

            if(null != statement){
                try{
                    statement.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        CreateTableAndOther c = new CreateTableAndOther();
        c.connect();
        String tablename = "tb_layers_c2db7e24d34440fc997e274cff87265g";
        String sqlText = "(\"uid\" int8 NOT NULL,\"name\" varchar(200),\"shape\" \"public\".\"geometry\",\"style\" text,\"color\" text,PRIMARY KEY (\"uid\"))";
        c.createTable(tablename, sqlText);

        String indexText =  "CREATE INDEX tb_layers_c2db7e24d34440fc997e274cff87265f_shape_idx ON tb_layers_c2db7e24d34440fc997e274cff87265f " +
        "USING gist (shape public.gist_geometry_ops_2d) ";
        c.createIndex(tablename);
        c.close();
    }
}

你可能感兴趣的:(python,开发语言)