No suitable driver found for jdbc:mysql://localhost:3306/shop_01
来源:互联网 发布:长整型数据范围 编辑:程序博客网 时间:2024/05/22 12:15
最近头好疼啊,眼睛也好痛! 之前学习的东西,就直接给忘了,好烦啊 ! 好了,今天来记录一下今天遇到的一个问题!
今天,在写一个JDBC的工具类,第一步,获取连接的时候就出现了问题,一直都出现了问题,具体代码如下:
package com.hardstudy.utils;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;public class DBUtils { public static Connection getConnection(){ Properties prop = null; Connection conn = null; try { prop = new Properties(); prop.load(DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties")); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String password = prop.getProperty("password"); System.out.println(url + "\n" + user + "\n" + password); conn = DriverManager.getConnection(url, user, password); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }}
这是我加入的jar文件:
这是我在src(classpath下创建的一个properties配置文件):
url=jdbc:mysql://localhost:3306/shop_01user=rootpassword=123
这是我的测试类:
package com.hardstudy.test;import java.sql.Connection;import org.junit.Test;import com.hardstudy.utils.DBUtils;public class TestJDBC { @Test public void test01(){ Connection conn = DBUtils.getConnection(); if (conn != null) { System.out.println(conn); } else{ System.out.println("it's null!!!"); } }}
这是我的错误代码:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shop_01
现在问题就出来了:没有发现合适的Driver对象!!
当显示的在代码中加入这段代码时,就可以正常的获取到Connection对象了:Class.forName("com.mysql.jdbc.Driver");
看运行结果:
为什么,我使用了DriverManger的getConnection()获取Connection对象,还需要声明Class.forName(“com.mysql.jdbc.Driver”)对象。
最后的解决方法,将之前的jar包换成最新的,不使用Class.forName()也可以正常加载!!!
为什么可以不用使用Class.forName()获取连接?
答:jdbc4已经不需要显式的调用Class.forName了,在jdbc4中,调用getConnection的时候DriverManager会自动去加载合适的驱动。
相关链接:
0.详细解释
1.获取连接的方法
2.JDBC教程
0 0
- No suitable driver found for jdbc:mysql://localhost:3306/shop_01
- No suitable driver found for jdbc:mysql://localhost:3306/ 问题
- No suitable driver found for jdbc:mysql://localhost:3306/stu_mis
- no suitable driver found for jdbc:mysql//localhost:3306/..
- No suitable driver found for jdbc:mysql://localhost:3306/ 问题
- no suitable driver found for jdbc:mysql//localhost:3306/..
- No suitable driver found for jdbc:mysql://localhost:3306/****
- No suitable driver found for jdbc:mysql://localhost:3306/test
- no suitable driver found for jdbc:mysql//localhost:3306/..
- No suitable driver found for jdbc:mysql://localhost/mysql
- No suitable driver found for jdbc:mysql://localhost/shop
- mondrian java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/foodmart
- No suitable driver found for jdbc:mysql://localhost:3306/itat_shop问题解决方式
- java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/airline
- java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
- no suitable driver found for jdbc:mysql//localhost:3306/qzhao 错误__Jemeter
- java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
- No suitable driver found for jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-
- 第五周:[leetcode] Two Sum、3Sum、3Sum Closest、4Sum
- nginx学习笔记2 nginx信号控制和平滑升级
- 卷皮2017春招笔试题
- android 报错Attempt to invoke virtual method
- C++四种强制类型转换
- No suitable driver found for jdbc:mysql://localhost:3306/shop_01
- 002--mysql索引
- 【恒大家装】成功案例
- c/c++实现顺序循环队列和链式队列
- JAVA Swing图形库一些使用心得
- 面试题汇总及解答
- Struts2学习2——Struts配置
- 继续xxx定律
- Android应用安全与防范之数据保护和资源保护