java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

数据库   发布日期:2025年07月06日   浏览次数:230

出现这个异常原因可能很多:

1.编码问题

2.没有加载类驱动:换一种数据库的连接方式,就忘了这个:Class.forName(driverclass);

 import java.io.IOException;
 import java.io.InputStream;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Properties;
 
 public class JdbcUtil {
     private static String driverclass;
     private static String url;
     private static String user;
     private static String password;
     static{
             try{
                 ClassLoader cl=JdbcUtil.class.getClassLoader();
 
                 InputStream in=cl.getResourceAsStream("dbbcfg.properties");
                 Properties props=new Properties();
                 props.load(in);
 
                 driverclass=props.getProperty("driverClass");
                 url=props.getProperty("url");
                 user=props.getProperty("user");
                 password=props.getProperty("password");
 
 
                 in.close();
             }catch (Exception e){
                 e.printStackTrace();
             }
             try {
                 Class.forName(driverclass);
             }catch (ClassNotFoundException e){
                 e.printStackTrace();
             }
     }
     public static Connection getConnection(){
         try {
             Connection conn = DriverManager.getConnection(url,user,password);
             return conn;
         } catch (Exception e) {
             throw new RuntimeException("链接数据库的url或用户名密码错误,请检查您的配置文件");
         }
     }
     public static void release(ResultSet rs,Statement stmt,Connection conn){
         if(rs!=null){
             try {
                 rs.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             rs = null;
         }
         if(stmt!=null){
             try {
                 stmt.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             stmt = null;
         }
         if(conn!=null){
             try {
                 conn.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             conn = null;
         }
     }
     public static void main(String[] args){
         System.out.println("Test connection");
         try{
             Connection conn=JdbcUtil.getConnection();
             System.out.println(conn);
 
         }catch (Exception e){
             e.printStackTrace();
         }
 
     }
 }

 

以上就是java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/的详细内容,更多关于java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/的资料请关注九品源码其它相关文章!