java.sql.SQLException: No suitable driver found for “路径“JDBC连接数据库访问失败

**

java.sql.SQLException: No suitable driver found for “jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false” 问题解决 JDBC连接数据库访问不到路径

在网上跟着狂神老师学习jdbc连接数据库,一开始的测试程序没有任何问题,只要把路径改为中的安全访问改为false即可

useSSL=false

但是之后使用工具类的时候,刚开始出现的报错是:

java.sql.SQLException: No suitable driver found for "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false"

在网上搜索了都是让检查路径和是否有启动驱动程序
db.properties配置文件一开始如下:

driver=com.mysql.jdbc.Driver
url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false"
username="root"
password="123456"

再三检查也没有发现拼写错误
检查ext中有没有驱动程序mysql-connector-java-5.1.47.jar,检查了也都有
在这里插入图片描述
再检查工具类:

package com.GJW.jdbc02.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            //getResourceAsStream("db.properties")   获取资源
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            // 1 驱动只加载一次
            Class.forName(driver);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接的方法
    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    //释放连接资源的方法
    public static void release(Connection conn, Statement st, ResultSet rs){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

发现问题可能出现在以下代码块:

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

在网上搜索了getProperty()方法的作用:
1、System.out.println(“Java类路径:” + System.getProperty(“java.class.path”)); // Java类路径
2、System.out.println(“Java提供商网站:” + System.getProperty(“java.vendor.url”)); // Java提供商网站

发现问题应该就在这里
getProperty(“java.class.path”),getProperty()方法的标准写法是用双引号("")将路径或url包裹起来
而我的配置文件中的路径和url还有双引号现在使用的url应该是这样的:

url=""jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false""

有两个双引号包裹,于是把路径和url的双引号都去了:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false
username="root"
password="123456"

但还是报错,只不过错误信息不一样了:

java.sql.SQLException: Access denied for user '"root"'@'localhost' (using password: YES)

看到这个问题,一下舒了一口气,因为这是之前初始化数据库时就遇到的错误,用户信息或密码有问题,于是继续进行上述修改(去掉双引号),因为也是同样的问题:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456

修改过后,终于运行成功将数据插入数据库,对于我这样的小萌新真是不容易,决定写一篇博客记录一下,也希望可以帮助到后续出现该问题的程序猿们~
在这里插入图片描述

在这里插入图片描述
最后 佛祖保佑!永无BUG!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绿茵程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值