Java通过JDBC连接Mysql

2020-10-13  本文已影响0人  yayooo

一、导入驱动mysql-connector-java-5.1.27-bin.jar

驱动 1. Project Structure 2.导入 3.

然后找到驱动jar包,导入。

或者使用pom文件

        <!--jdbc-->
        <!-- MySql 5.5 Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
            <scope>runtime</scope>
        </dependency>

二、代码实现(直接版)

package com.dfjx.chongqing.oracle2mysqlTest.utils;

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

public class MysqlConnection {
    public static void main(String[] args) throws SQLException {
        MysqlConnection mysqlConnection = new MysqlConnection();
        mysqlConnection.getConnection();
    }

    Connection con = null;
    public Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");  System.out.println("数据库驱动加载成功");
        } catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:mysql://192.168.68.102:3306/auth_server_cq?characterEncoding=UTF-8","root","111111");
            System.out.println("数据库连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;

    }

}

三、Java通过获取配置文件连接Mysql
3.1 配置文件:jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.68.102:3306/auth_server_cq?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=111111

3.2连接

package com.dfjx.chongqing.oracle2mysqlTest.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class MysqlConn2 {

    public Connection getConnection(){
        String driver=null;
        String url=null;
        String username=null;
        String password=null;
        Connection conn=null;

        try {
            //从配置文件中获取配置
            Properties properties = new Properties();
            //获取配置文件路径
            String path = Thread.currentThread().getContextClassLoader().getResource("jdbc.properties").getPath();
            //初始化文件输入流
            FileInputStream in = null;
             in = new FileInputStream(path);
             properties.load(in);

            //获取配置文件中的信息,放入到对应的变量中
            driver=properties.getProperty("jdbc.driverClassName");
            url=properties.getProperty("jdbc.url");
            username=properties.getProperty("jdbc.username");
            password=properties.getProperty("jdbc.password");
            System.out.println("获取到的URL:"+url+"\t username:"+username+"\tpassword:"+password);
            //关闭文件输入流
            in.close();

            //加载驱动
            Class.forName(driver);
            System.out.println("开始连接数据库!");
            conn = DriverManager.getConnection(url, username, password);//连接数据库
            System.out.println(conn+ "******************");
        }catch (Exception e) {
            e.printStackTrace();
        }

        return conn;

    }

    public static void main(String[] args) {
        MysqlConn2 mysqlConn2 = new MysqlConn2();
        Connection connection = mysqlConn2.getConnection();
        System.out.println(connection+"========");
    }

}


上一篇下一篇

猜你喜欢

热点阅读