• 【JDBC】Extra03 PostgreSQL-JDBC


    PostgreSQL的JDBC实现:

    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.14</version>
    </dependency>

    连接Demo:

        public static void originConnection() throws SQLException {
            Connection connection = null;
            try {
                Class.forName("org.postgresql.Driver");
                connection = DriverManager.getConnection(
                        "jdbc:postgresql://127.0.0.1:5432/db01",
                        "postgres",
                        "123456"
                );
                System.out.println(connection);
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(e.getClass().getName()+": "+e.getMessage());
                System.exit(0);
            } finally {
                connection.close();
            }
            // System.out.println("Opened database successfully");
        }

    封装工具类:

    package cn.echo42.util;
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    /**
     * @author DaiZhiZhou
     * @file PostgreSQL-Connector
     * @create 2020-07-25 23:40
     */
    public class PdbcUtil {
    
        private static Properties properties;
    
        static {
            try {
                properties = new Properties();
                InputStream inputStream = PdbcUtil.class.getClassLoader().getResourceAsStream("pdbc.properties");
                properties.load(inputStream);
                Class.forName(properties.getProperty("driver"));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        public static Connection getConnection(){
            try {
                return DriverManager.getConnection(
                        properties.getProperty("url"),
                        properties.getProperty("user"),
                        properties.getProperty("password")
                );
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
            return null;
        }
    
    }

    配置文件:

    driver = org.postgresql.Driver
    url = jdbc:postgresql://49.234.116.100:5432/db01
    user = postgres
    password = 123456

    感觉和MySQL的Jdbc是完全一样的,只需要更换对应的配置信息和驱动包即可,程序的连接对象获取一样

  • 相关阅读:
    BZOJ4269: 再见Xor(线性基)
    Codeforces Round #473 (Div. 2)
    洛谷P3812 【模板】线性基
    CodeChef March Lunchtime 2018 div2
    BZOJ1023: [SHOI2008]cactus仙人掌图(仙人掌dp)
    【Android】Android布局中实现圆角边框
    Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
    mac OSX上eclipse adb无法识别(调试)小米的解决方案
    IOS回调机制——代理,通知中心以及Block
    长沙国储电脑城-学生买电脑-被坑记
  • 原文地址:https://www.cnblogs.com/mindzone/p/13379187.html
Copyright © 2020-2023  润新知