zoukankan      html  css  js  c++  java
  • JDBC-Connection

    新建Maven工程

    pom.xml

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <!-- 指定jdk -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    数据库信息

    driver=com.mysql.cj.jdbc.Driver
    jdbcUrl=jdbc:mysql://192.168.8.136:3306/jdbc
    user=root
    password=root

    获取链接

    Driver

    /**
     * Driver 是一个接口: 可以通过 Driver 的实现类对象获取数据库连接
     */
    @Test
    public void testDriver() throws SQLException {
        // 创建 Driver 实现类对象
        Driver driver = new com.mysql.cj.jdbc.Driver();
    
        // 准备连接数据库信息: url, user, password
        String url = "jdbc:mysql://192.168.8.136:3306/jdbc";
        Properties info = new Properties();
        info.put("user", "root");
        info.put("password", "root");
    
        // 调用 Driver 接口的 connect(url, info) 获取数据库连接
        Connection connection = driver.connect(url, info);
        
        System.out.println(connection);
    }
    
    @Test
    public void getConnection() throws Exception{
        // 读取类路径下的 jdbc.properties 文件
        InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
    
        String driverClass = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
    
        //通过反射 Driver 对象
        Driver driver = (Driver) Class.forName(driverClass).newInstance();
    
        Properties info = new Properties();
        info.put("user", user);
        info.put("password", password);
    
        //通过 Driver 的 connect 方法获取数据库连接
        Connection connection = driver.connect(jdbcUrl, info);
    
        System.out.println(connection);
    }

    DriverManager

    /**
     * DriverManager 是驱动的管理类
     * 1). 可以通过重载的 getConnection() 方法获取数据库连接. 较为方便
     * 2). 可以同时管理多个驱动程序: 若注册了多个数据库连接, 则调用 getConnection()
     * @throws Exception
     */
    @Test
    public void testDriverManager() throws Exception{
        // 准备连接数据库
        String driverClass = "com.mysql.cj.jdbc.Driver";
        String jdbcUrl = "jdbc:mysql://192.168.8.136/jdbc";
        String user = "root";
        String password = "root";
    
        // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块
        Class.forName(driverClass);
        // 通过 DriverManager 的 getConnection() 方法获取数据库连接
        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
        
        System.out.println(connection);
    }
    
    @Test
    public void getConnection2() throws Exception{
        Properties properties = new Properties();
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(in);
    
        String driver = properties.getProperty("driver");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
    
        // 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块)
        Class.forName(driver);
        // 通过 DriverManager 的 getConnection() 方法获取数据库连接.
        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
        
        System.out.println(connection);
    }

    oracle官方JDBC文档

  • 相关阅读:
    Cloudera Manager安装之利用parcels方式安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(CentOS6.5)(五)
    Cloudera Manager安装之利用parcels方式安装单节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(CentOS6.5)(四)
    Cloudera Manager安装之Cloudera Manager 5.3.X安装(三)(tar方式、rpm方式和yum方式)
    Cloudera Manager安装之时间服务器和时间客户端(二)
    Cloudera Manager安装之Cloudera Manager安装前准备(CentOS6.5)(一)
    Cloudera Manager架构原理
    Cloudera Manager是啥?主要是干啥的?
    HDU-1664-Different Digits(BFS)
    AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3參数含义
    c++11 新特性之 autokeyword
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10436018.html
Copyright © 2011-2022 走看看