Java:连接mysql数据库的五种方式

文章介绍了五种Java中连接MySQL数据库的方法,包括手动注册Driver、使用Class.forName、DriverManager、配置文件驱动和自动注册的推荐方式。

方式1

public void connect01() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/db02";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "1234");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

方式2

public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/db02";
        Properties properties = new Properties();
        properties.setProperty("user", "root");
        properties.setProperty("password", "1234");
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
    }

方式3

//方式3 使用DriverManager 替代 Diver 进行统一管理 扩展性更好一点
    public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url = "jdbc:mysql://localhost:3306/db02";
        String user = "root";
        String password = "1234";

        DriverManager.registerDriver(driver);//注册Driver驱动
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

方式4

//方式4 使用Class.forName 自动完成注册驱动 简化代码
    //这种方式获取连接是使用最多的,推荐使用
    public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载 Driver类
        //在加载 Driver类时 完成注册
        /*
        * 源码 1. 静态代码块 在类加载的时候 会执行一次
        *       2.DriverManager.registerDriver(new Driver());
        *       3.因此注册Driver的工作已经完成
        *     static{
        *             try{
        *                  DriverManager.registerDriver(new Driver());
        *                }catch(SQLException var1){
        *                  throw new RuntimeException("Can't register driver!");
        *                }
        *               }
        *            }
        * */
        Class.forName("com.mysql.jdbc.Driver");
        //1.mysql驱动5.1.6可以无需Class.forName("com.mysql.jdbc.Driver")
        //2.从jdk1.5以后使用了jdbc4 不再需要显示调用class.forName()注册驱动而是自动调用驱动
        //    jar包下META-INF/services/java.sql.Driver文本中的类名称去注册
        //3.建议还是写上Class.forName("com.mysql.jdbc.Driver"),更加明确
        //创建url 和 user 和 password
        String url = "jdbc:mysql://localhost:3306/db02";
        String user = "root";
        String password = "1234";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }


方式5

//方式5 在方式4的基础上改进 增加配置文件 让连接mysql更加灵活
    public void connect05() throws IOException, ClassNotFoundException, SQLException {
        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值